0

I want to color the rectangles of a horizontal bar plot based on their value as reflected in a colormap I have. Is there a way to iterate over each rectangle and make the color reflect its value on the colormap?

Here is what I'm working with if you're interested

I have my x and y values and put them into a normal horizontal barplot like so, and I have some labels at the end denoting values like so:

plt.barh(x, y)

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

The horizontal bar graph looks like this: enter image description here

Here is the colormap I want to use:

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

Sorry it's gigantic. enter image description here

m00saca
  • 363
  • 1
  • 7
  • 20

1 Answers1

0

Okay I figured it out, based on the answer in this post:

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

enter image description here

m00saca
  • 363
  • 1
  • 7
  • 20