【问题标题】:Fill in horizontal barplot rectangles based on value in Matplotlib [duplicate]根据 Matplotlib 中的值填充水平条形图矩形 [重复]
【发布时间】:2019-12-10 16:56:13
【问题描述】:

我想根据我拥有的颜色图中反映的值对水平条形图的矩形进行着色。有没有办法遍历每个矩形并使颜色在颜色图上反映其值?

如果您有兴趣,这就是我正在使用的内容

我有我的 x 和 y 值并将它们放入一个正常的水平条形图中,就像这样,我在最后有一些标签表示这样的值:

plt.barh(x, y)

for i, v in enumerate(y):
    ax.text(v + 1, i - .15 , str(v))

水平条形图如下所示:

这是我要使用的颜色图:

fig, ax = plt.subplots(figsize=(0.5, 16))
colorBar = plt
fig.subplots_adjust(bottom=0.5)

cmap = mpl.cm.YlGn
norm = mpl.colors.Normalize(vmin=1, vmax=100)

cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
                                norm=norm,
                                orientation='vertical')
fig.show()

对不起,它是巨大的。

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    好的,根据这篇文章中的答案,我想通了:

    vary the color of each bar in bargraph using particular value

    cmap = mpl.cm.YlGn
    norm = mpl.colors.Normalize(vmin=1, vmax=100)
    ax = fig.add_axes([.1,.1,1,1])
    plt.barh(x, y, color=cmap(norm(y)))
    
    for i, v in enumerate(y):
        ax.text(v + 1, i - .15 , str(v))
    

    【讨论】:

      猜你喜欢
      • 2016-10-13
      • 2018-07-22
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 2016-07-14
      • 2020-09-17
      • 2013-05-15
      • 2015-01-13
      相关资源
      最近更新 更多