python - 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]]))
解决方案
您必须明确指定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。
推荐阅读
- dart - 空的 const 构造函数重要吗?
- angular - Angular mat-select在编辑时不显示区域名称
- flutter - Flutter:如何平滑 PageView onPageChanged 动画
- linux - 无法从 kubernetes pod 挂载 azure 文件共享(在本地机器上工作正常)
- php - Soap BAD_REQUEST 无法解析模板
- firebase - 如何过滤带有文档 ID 的流?
- c# - 使用 C# 在 MongoDB 中读取字段的值
- flutter - 如何使 FloatingActionButton 动画化?
- swift - 如何使用 Swift 5 中结构的键和值过滤字典?
- php - 动态多级下拉菜单php sql