将两个matplotlib imshow图设置为具有相同的颜色贴图比例

Ohm*_*Ohm 9 python plot matplotlib colorbar imshow

我试图绘制具有相同比例的字段.上面的图像值比下面的图像值高10倍,但它们在imshow中变成了相同的颜色.如何将两者设置为具有相同的颜色比例?

我添加了我正在使用的代码图像..

两个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()
Run Code Online (Sandbox Code Playgroud)

Rys*_*ski 8

首先,您需要定义要使用的颜色范围的最小值和最大值.在此示例中,它是您正在绘制的两个阵列的最小值和最大值.然后使用这些值来设置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()
Run Code Online (Sandbox Code Playgroud)