Matplotlib Bars

Matplotlib Bars

Creating Bars

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

Example

Draw 4 bars:

Result:

Try it Yourself »

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.

Example

Try it Yourself »


Horizontal Bars

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

Example

Draw 4 horizontal bars:

Result:

Try it Yourself »


Bar Color

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

Example

Draw 4 red bars:

Result:

Try it Yourself »

Color Names

You can use any of the 140 supported color names.

Example

Draw 4 “hot pink” bars:

Result:

Try it Yourself »

Color Hex

Or you can use Hexadecimal color values:

Example

Draw 4 bars with a beautiful green color:

Result:

Try it Yourself »


Bar Width

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

Example

Draw 4 very thin bars:

Result:

Try it Yourself »

The default width value is 0.8

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:

Example

Draw 4 very thin bars:

Result:

Try it Yourself »

The default height value is 0.8

ArmenianEnglish