【问题标题】:Get colorbar ticklabels获取颜色条刻度标签
【发布时间】:2014-11-19 02:39:05
【问题描述】:

我有一个散点图:

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()

如何获取颜色条刻度标签?我可以想象,使用cbar.ax.get_yticklabels() 我非常接近解决方案。但是,鉴于下图,我想有类似的东西:

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

【问题讨论】:

    标签: python matplotlib colorbar


    【解决方案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.])
    

    【讨论】:

      【解决方案2】:

      试试这个:

      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()
      

      【讨论】:

      • 我得到了这个列表作为答案 [10, 20, 30, 40, 50, 60, 70, 80, 90]
      • 哇。非常感谢。猜猜我最大的问题是阅读文本列表。
      • cool,如果你想要float,你可以用float()代替int()
      猜你喜欢
      • 2016-06-14
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2018-06-05
      • 2022-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多