How do I give a hexadecimal color to different bars in a bar chart?

Viewed 1411

How do I make it so that different bars of my bar chart have different colors using HTML hexadecimal values? I already know that I can make the whole bar chart one color via hexadecimal values. It's done like this:

import pylab as plt
plt.xlim(0, 10)
plt.barh([1, 2, 3], [1, 2, 3], align='center', color='#ff0000')
plt.show()

Which produces this figure: enter image description here But how can I make it, so that matplotlib produces a figure using hexadecimal values for each different bar, as follows: enter image description here

1 Answers

Try using color as a list:

import pylab as plt
plt.xlim(0, 10)
plt.barh([1, 2, 3], [1, 2, 3], align='center', color=['#ff0000','#000000','#0000ff'])
plt.show()

Output:

enter image description here

From Docs:

color : scalar or array-like, optional
The colors of the bar faces.

Related