Python 将两个matplotlib imshow打印设置为具有相同的颜色贴图比例
我正试图以相同的比例绘制出字段。上面的图像值比下面的图像值高10倍,但它们在imshow中的颜色相同。如何设置两者的颜色比例相同 我在图片下方添加了我正在使用的代码Python 将两个matplotlib imshow打印设置为具有相同的颜色贴图比例,python,matplotlib,plot,colorbar,imshow,Python,Matplotlib,Plot,Colorbar,Imshow,我正试图以相同的比例绘制出字段。上面的图像值比下面的图像值高10倍,但它们在imshow中的颜色相同。如何设置两者的颜色比例相同 我在图片下方添加了我正在使用的代码 首先,需要定义要使用的颜色范围的最小值和最大值。在本例中,它是正在打印的两个阵列的最小值和最大值。然后使用这些值设置imshow颜色代码的范围 import numpy as np def show_field(field1,field2): combined_data = np.array([field1,f
首先,需要定义要使用的颜色范围的最小值和最大值。在本例中,它是正在打印的两个阵列的最小值和最大值。然后使用这些值设置imshow颜色代码的范围
import numpy as np
def show_field(field1,field2):
combined_data = np.array([field1,field2])
#Get the min and max of all your data
_min, _max = np.amin(combined_data), np.amax(combined_data)
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
#Add the vmin and vmax arguments to set the color scale
ax.imshow(field1,cmap=plt.cm.YlGn, vmin = _min, vmax = _max)
ax.set_adjustable('box-forced')
ax.autoscale(False)
ax2 = fig.add_subplot(2, 1, 2)
ax2.set_adjustable('box-forced')
#Add the vmin and vmax arguments to set the color scale
ax2.imshow(field2,cmap=plt.cm.YlGn, vmin = _min, vmax = _max)
ax2.autoscale(False)
plt.show()
为了补充公认的答案,这里有一个函数,可以生成任意数量的imshow绘图,这些绘图共享相同的颜色贴图:
def show_fields(fields):
combined_data = np.array(fields)
#Get the min and max of all your data
_min, _max = np.amin(combined_data), np.amax(combined_data)
fig = plt.figure()
for i in range(len(fields)):
ax = fig.add_subplot(len(fields), 1, i+1)
#Add the vmin and vmax arguments to set the color scale
ax.imshow(fields[i],cmap=plt.cm.YlGn, vmin = _min, vmax = _max)
ax.set_adjustable('box-forced')
ax.autoscale(False)
plt.show()
用法:
show_fields([field1,field2,field3])
您正在查找
vmin
和vmax
参数。(旁注,这是一个重复的问题,尽管我目前找不到规范版本…)是的,我也没有找到这个问题,尽管我确信它被提出了很多次…我相当确定有一个比我标记为的问题更精确的重复。。。如果您或其他人遇到它,请随意更改它!Blerg,好吧,我不能用正确的重复问题来结束它,但这里有一个更准确的重复问题:@JoeKington那么你说,我应该删除我的问题吗?我不想连接两个非常大的矩阵来获得组合的数据,所以我只做了\u min=min(field1.min(),field2.min())
。
show_fields([field1,field2,field3])