首页 > 解决方案 > plt.colorbar 在绘图之间的刻度不相等

问题描述

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

在此处输入图像描述

请注意,颜色条上的刻度不同。我希望它们都从 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]]))

标签: pythonmatplotlibheatmap

解决方案


您必须明确指定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


推荐阅读