Python Tutorials · Python Matplotlib

Matplotlib Bars

Learn all about Matplotlib Bars in this comprehensive tutorial.

5 min read intermediate
  • With Pyplot, you can use the bar() function to draw bar graphs:
  • If you want the bars to be displayed horizontally instead of vertically, use the barh() function:
  • The bar() and barh() take the keyword argument color to set the color of the bars:
  • The bar() takes the keyword argument width to set the width of the bars:
  • The barh() takes the keyword argument height to set the height of the bars:

Creating Bars

With Pyplot, you can use the bar() function to draw bar graphs:

python

The bar() function takes arguments that describes the layout of the bars.

The categories and their values represented by the first and second argument as arrays.

python

Horizontal Bars

If you want the bars to be displayed horizontally instead of vertically, use the barh() function:

python

Bar Color

The bar() and barh() take the keyword argument color to set the color of the bars:

python

You can use any of the 140 supported color names.

python

Or you can use Hexadecimal color values:

python

Bar Width

The bar() takes the keyword argument width to set the width of the bars:

python

The default width value is 0.8

Note: Note: For horizontal bars, use height instead of width.

Bar Height

The barh() takes the keyword argument height to set the height of the bars:

python

The default height value is 0.8

Module quiz

2 questions
1

Which of the following is true about Matplotlib Bars?

2

What is the most common pitfall when working with Matplotlib Bars?

Answer all questions to submit.