Bar charts with Matplotlib

Learn to build beautiful bar charts in Python using Matplotlib.

Bar charts with Matplotlib

Topics Covered

  1. What are Bar charts?
  2. Syntax of bar function in matplotlib
  3. Plotting a simple bar chart
  4. Plotting horizontal bar charts
  5. Change width of bar charts
  6. Different colors for your bar charts
  7. Stacked bar charts


What are Bar charts?

Bar charts are used to compare numerical values of categorical data using bars. The height or length of those bars are proportional to their respective values.

You can plot bar charts in Python using the bar() function from the Pyplot class in Matplotlib.


Syntax of bar function in matplotlib

matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)

Parameters:

  • x - The x coordinates of the bars.
  • height - The height(s) of the bars.
  • width - The width(s) of the bars, default is 0.8.
  • bottom - The y coordinate(s) of the bottom side(s) of the bars, default is 0.
  • align - {'center', 'edge'}, default: 'center'
    Alignment of the bars to the x coordinates:
    'center': Center the base on the x positions.
    'edge': Align the left edges of the bars with the x positions.
    To align the bars on the right edge pass a negative width and align='edge'

Plotting a simple bar chart

Let's plot a bar chart showing the total sales of a company over the last 4 quarters for FY 2022-23.

import matplotlib.pyplot as plt
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
sales = [42,50,60,24]
plt.bar(quarters,sales)
plt.ylabel("Sales in million $")
plt.show()
Simple bar chart in Matplotlib

Plotting horizontal bar charts

Instead of vertical bar charts, you can also plot horizontal bar charts by using the barh function.

import matplotlib.pyplot as plt
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
sales = [42,50,60,24]
plt.barh(quarters,sales)
plt.xlabel("Sales in million $")
plt.show()

Horizontal bar chart in Matplotlib

Change width of bar charts

You can alter the width of your bars by applying a bar parameter for vertical bars and height parameter for horizontal bars.

import matplotlib.pyplot as plt
quarters = ['Q1', 'Q2', 'Q3', 'Q4']
sales = [42,50,60,24]
plt.barh(quarters, sales, height=0.5)
plt.xlabel("Sales in million $")
plt.show()
Horizontal bar charts at 50% height in Matplotlib

Different colors for your bar charts

To apply a new color to your bar charts, use the color parameter.

Changing the colors of the bar charts in Matplotlib

You can use any Hexadecimal color values, any of the 140 supported color names, or one of these shortcuts:

'r' - Red
'g' - Green
'b' - Blue
'c' - Cyan
'm' - Magenta
'y' - Yellow
'k' - Black
'w' - White


Stacked bar charts

Stacked bar charts are used to show a comparison between categories on the same plot. The bar lengths are proportional to their values.

You can use the bottom parameter to draw stacker bar charts in Matplotlib.

import matplotlib.pyplot as plt
x = ['Q1', 'Q2', 'Q3', 'Q4']
y1 = [24, 35, 17, 64]
y2 = [20, 30, 10, 90]
 
# plot bars in stack manner
plt.bar(x, y1, color='r', label='2022')
plt.bar(x, y2, bottom=y1, color='g', label='2021')
plt.legend()
plt.show()
Stacked bar charts in Matplotlib

Pylenin has a dedicated Youtube playlist for Matplotlib Tutorial. Check out our entire Matplotlib playlist here.

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe