【问题标题】:Python pyplot colorbar and colormap have different colorsPython pyplot colorbar和colormap有不同的颜色
【发布时间】:2020-10-13 08:55:32
【问题描述】:

我目前正在尝试创建一个颜色图。但是,我有一个问题,即颜色图颜色和颜色条颜色不同。如何更改颜色条颜色方案,使其与地图的“hot_r”颜色方案相匹配?

代码如下:

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LogNorm
from scipy.interpolate import interp2d

x_list = np.array(X_COORDINATE)
z_list = np.array(Z_COORDINATE)
C_I_list = np.array(C_I)

# f will be a function with two arguments (x and z coordinates),
# but those can be array_like structures too, in which case the
# result will be a matrix representing the values in the grid 
# specified by those arguments
f = interp2d(x_list,z_list,C_I_list,kind="linear")

x_coords = np.arange(min(x_list),max(x_list)+1)
z_coords = np.arange(min(z_list),max(z_list)+1)
c_i = f(x_coords,z_coords)

fig = plt.imshow(c_i,
           extent=[min(x_list),max(x_list),min(z_list),max(z_list)],
           origin="lower", interpolation='spline16', cmap='hot_r')

x_for_horizontal_line = [0,1]
y_for_horizontal_line = [1,1]

x_for_vertical_line = [1,1]
y_for_vertical_line = [0,1]


# Show the positions of the sample points, just to have some reference
fig.axes.set_autoscale_on(False)
plt.scatter(x_list,z_list,400,facecolors='none')
plt.plot(x_for_horizontal_line, y_for_horizontal_line, color='w')
plt.plot(x_for_vertical_line, y_for_vertical_line, color='w')
plt.xlim(left=1,right=5)
plt.xlabel('NF Fracture Center, x (m)')
plt.ylabel('NF Fracture Center, z (m)')
plt.title('C I')
plt.colorbar()
plt.show()

生成的地图和颜色条如下所示(请注意,我无法显示实际数据,因此会出现奇怪的裁剪图像):

enter image description here

【问题讨论】:

    标签: python plot graph colorbar colormap


    【解决方案1】:

    哦,这是一个简单的错误。我没有在 colorbar() 参数中调用 'fig'。

    因此,为了其他人的利益,将倒数第二行代码替换为:

    plt.colorbar(fig)
    

    完成这项工作。

    【讨论】:

    • fig = plt.imshow(...) 是一个非常令人困惑的变量名称。 fig 通常用于图形(包含所有子图的周围图形)。请避免将该名称用于其他名称。最好使用img = plt.imshow()
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    相关资源
    最近更新 更多