Setting Different Bar color in Matplotlib

MatplotlibServer Side ProgrammingProgramming

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

raja
Published on 16-Mar-2021 10:54:35
Advertisements