Matplotlib PyPlot – Set Different Color(s) for Bars in Bar Plot
To set different colors for bars in a Bar Plot using Matplotlib PyPlot API, call matplotlib.pyplot.bar()
function, and pass required color values, as list, to color
parameter of bar()
function.
The definition of matplotlib.pyplot.bar() function with color
parameter is
</>
Copy
bar(x, height, color=None)
Of course, there are other named parameters, but for simplicity, only color parameter is given.
where
Parameter | Description |
---|---|
color | The color(s) of the bar which takes color or list of color. |
Example
In the following program, we will draw a bar plot with bars and set different colors for each of the bar in this bar plot.
example.py
</>
Copy
import matplotlib.pyplot as plt
#data
x = [1, 2, 3, 4, 5]
h = [10, 8, 12, 4, 7]
c = ['red', 'yellow', 'black', 'blue', 'orange']
#bar plot
plt.bar(x, height = h, color = c)
plt.show()
Output
Conclusion
In this Matplotlib Tutorial, we learned how to set different colors for bars in bar plot using Matplotlib PyPlot API.