获取colorbar ticklabels

时间:2014-11-18 14:54:42

标签: python matplotlib colorbar

我有散点图:

x,y,c,s = np.random.rand(100), np.random.rand(100), np.random.rand(100)*100, np.random.rand(100)*100
plt.scatter(x,y,c=c,s=s,cmap='YlGnBu', alpha=0.3)
cbar = plt.colorbar()

如何获取colorbar ticklabels?我可以想象,cbar.ax.get_yticklabels()我接近解决方案。但是,如下图所示,我希望有类似的内容:

array([ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90.])

enter image description here

2 个答案:

答案 0 :(得分:1)

这将为您提供颜色条y轴刻度的刻度,该刻度具有限制(0.0,1.0)

cbar.ax.get_yticks()

这就是你需要的:

np.interp(cbar.ax.get_yticks(), cbar.ax.get_ylim(), cbar.get_clim())

结果是:

array([ 10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90.])

答案 1 :(得分:0)

试试这个:

x,y,c,s = np.random.rand(100), np.random.rand(100), np.random.rand(100)*100, np.random.rand(100)*100
b = plt.scatter(x,y,c=c,s=s,cmap='YlGnBu', alpha=0.3)
cbar = plt.colorbar(b)
label_list = []
for i in cbar.ax.get_yticklabels():
    a = int(i.get_text())
    label_list.append(a)
print label_list
plt.show()