I'm willing to generate such a plot:
I have troubles defining the colormap as a color for each bar. How Can I do that? I have written the following code:
import matplotlib.pyplot as plt
quiz=[1,2,0,4,8,10]
plt.barh(range(len(quiz)), quiz, align='center', alpha=0.5, color='blue')
It works but all the bars are just of one color(blue here). How to use a colormap as a color?
color=plt.cm.get_cmap('bwr')
gives an
Error: TypeError: object of type 'ListedColormap' has no len()
Edit: This post gives some hints, but doesn't tell me how to make the color gradient correspond to the x values: How to fill matplotlib bars with a gradient?
grad = np.atleast_2d(np.linspace(0,1,256)).T
, to scale the256
according to the length of your bars. E.g. for the longest bar you would use 256 and for the others a smaller number. For instance if the longest bar is 10, a bar of length 8 would useint(8*256/10)
.