我正在尝试绘制具有相同比例的字段。上面的图像值比下面的图像值高 10 倍,但它们在 imshow 中的颜色相同。如何将两者设置为具有相同的颜色比例?
我在图像下方添加了我正在使用的代码..
def show_field(field1,field2):
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
ax.imshow(field1,cmap=plt.cm.YlGn)
ax.set_adjustable('box-forced')
ax.autoscale(False)
ax2 = fig.add_subplot(2, 1, 2)
ax2.set_adjustable('box-forced')
ax2.imshow(field2,cmap=plt.cm.YlGn)
ax2.autoscale(False)
plt.show()
最佳答案
首先,您需要定义要使用的颜色范围的最小值和最大值。在此示例中,它是您正在绘制的两个数组的最小值和最大值。然后使用这些值来设置 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()
关于python - 设置两个 matplotlib imshow 图具有相同的颜色图比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58562043/