Setting Different Bar color in Matplotlib


We can first create bars and then, by using set_color, we can set the color of the bars.

Steps

  • Pass two lists consisting of four elements, into the bars method argument.

  • Step 1 returns bars.

  • Return values (0, 1, 2, 3) can be set with different colors, using set_color() method. Green, Black, Red color will be set and one bar will have the default color.

  • To show the figure, use plt.show() method.

Example

from matplotlib import pyplot as plt

bars = plt.bar([1, 2, 3, 4], [1, 2, 3, 4])
bars[0].set_color('green')
bars[1].set_color('black')
bars[2].set_color('red')
plt.show()

Output

Updated on: 16-Mar-2021

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements