[英]Matplotlib colorbar not showing labels for 2 discrete values
With an array of 3 discrete values I can use imshow
to create a colorbar.有了 3 个离散值的数组,我可以使用
imshow
创建一个颜色条。
import numpy as np
from matplotlib import pyplot as plt
img_data1 = np.random.choice([0,1,2], size=100).reshape((10,10))
plt.imshow(img_data1)
plt.colorbar(ticks = [0,1,2], values = [0,1,2])
But if the array only has 2 discrete values, [0,1]
, the colorbar labels are missing.但是,如果数组只有 2 个离散值
[0,1]
,则颜色栏标签将丢失。
img_data2 = np.random.choice([0,1], size=100).reshape((10,10))
plt.imshow(img_data2)
plt.colorbar(ticks = [0,1], values = [0,1])
How can I make the the color bar with 2 values have ticks marks and labels?如何使具有 2 个值的颜色条具有刻度线和标签?
Your code work fine for me:你的代码对我来说很好用:
I am using matplotlib 3.4.2.我正在使用 matplotlib 3.4.2。 Try to update your package if it is not up to date.
如果不是最新的,请尝试更新您的 package。
In any case, you can customize colorbar labels with:在任何情况下,您都可以自定义颜色条标签:
cbar = plt.colorbar(ticks = [0,1], values = [0,1])
cbar.ax.get_yaxis().set_ticks([0, 0.25, 0.5, 0.75, 1]) # pass the array of labels you want
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.