plt.colorbar在图之间打勾不相等

时间:2018-08-27 18:21:40

标签: python matplotlib heatmap

我正在使用matplotlib绘制热图,如下所示:

enter image description here

请注意,颜色条上的刻度不同。我希望它们都从0开始到1结束。我认为问题在于每个数组的最小值(x_array绘制在左侧,y_array绘制在右侧):

np.min(x_array)
0.10800000000000001
np.min(y_array)
4.679256262324e-310

(完整的数组如下所示)

但是,我在两个图中对刻度线使用了相同的规格:

plt.colorbar(ticks = np.linspace(0, 1, num = 5))

任何帮助解决此问题的方法。

我在Jupyter上使用Python 2.7。完整的代码和数组是:

plt.figure(figsize = (12,5))
plt.subplots_adjust(left= 0, right= 1, top = .9, bottom = 0.1, wspace = 0.001)
ticks = np.arange(0, 1, step = 0.1)

# left plot
plt.subplot(1, 2, 1)
plt.xticks(ticks), plt.yticks(ticks)
plt.imshow(x_array, cmap=plt.cm.gray, origin = "lower", extent = (0,.9,0,.9))
plt.colorbar(ticks = np.linspace(0, 1, num = 5))

# right plot
plt.subplot(1, 2, 2)
plt.xticks(ticks), plt.yticks(ticks)
plt.imshow(y_array, cmap=plt.cm.gray, origin = "lower", extent = (0,.9,0,.9))
plt.colorbar(ticks = np.linspace(0, 1, num = 5))

 x_array
 (array([[1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [0.64 , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [0.37 , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [0.27 , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [0.21 , 0.47 , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [0.17 , 0.36 , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [0.108, 0.29 , 0.46 , 1.   , 1.   , 1.   , 1.   , 1.   , 1.   ],
    [0.108, 0.24 , 0.37 , 0.52 , 1.   , 1.   , 1.   , 1.   , 1.   ]]),

 y_array
 array([[1.00000000e-002, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 4.67925626e-310],
    [4.67925626e-310, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002],
    [1.00000000e+000, 4.67925626e-310, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002],
    [1.00000000e+000, 1.00000000e-002, 4.67925626e-310,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002],
    [1.00000000e+000, 1.00000000e-002, 1.00000000e-002,
     4.67925626e-310, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002],
    [1.00000000e+000, 1.00000000e+000, 1.00000000e-002,
     1.00000000e-002, 4.67925626e-310, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002],
    [1.00000000e+000, 1.00000000e+000, 1.00000000e-002,
     1.00000000e-002, 1.00000000e-002, 4.67925626e-310,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002],
    [1.00000000e+000, 1.00000000e+000, 1.00000000e+000,
     1.00000000e-002, 1.00000000e-002, 1.00000000e-002,
     4.67925626e-310, 1.00000000e-002, 1.00000000e-002],
    [1.00000000e+000, 1.00000000e+000, 1.00000000e+000,
     1.00000000e+000, 1.00000000e-002, 1.00000000e-002,
     1.00000000e-002, 4.67925626e-310, 1.00000000e-002]]))

1 个答案:

答案 0 :(得分:1)

您必须为左图明确指定vmin

plt.imshow(x_array, cmap=plt.cm.gray, origin = "lower", extent = (0,.9,0,.9), vmin=0) 

有关详细信息,请参见https://matplotlib.org/2.1.2/api/_as_gen/matplotlib.pyplot.imshow.html