How to make a horizontal bar chart with Matplotlib using Python

1 min

A horizontal bar chart can be a good way to display values that belongs to categorical variables.

Why? Simply because displaying text on a vertical axis is not optimal.

Let me show you how to make a horizontal bar plot

import pandas as pd
import matplotlib.pyplot

# We create our sample dataset
df = pd.DataFrame({'country': {0: 'Switzerland',
                               1: 'Norway',
                               2: 'USA',
                               3: 'Sweden',
                               4: 'Israel',
                               5: 'Canada',
                               6: 'Belgium',
                               7: 'Estonia',
                               8: 'Ireland',
                               9: 'Lithuania'},
                   'big_mac': {0: 7.03,
                               1: 5.83,
                               2: 5.67,
                               3: 5.63,
                               4: 5.41,
                               5: 5.24,
                               6: 4.73,
                               7: 4.64,
                               8: 4.64,
                               9: 4.64}})

# We create our canvas
fig, axes = plt.subplots(1,
                         1,
                         figsize=(8,6))

# We plot the bar plot
axes.barh(df["country"], df["big_mac"], align="center")

# We invert the y labels, so descending values
axes.invert_yaxis()

# We set x labels
axes.set_xlabel('Dollar per big mac')

# We the title
axes.set_title('Big mac index')

fig.tight_layout()
plt.show()
How to make a horizontal bar chart with Matplotlib using Python
Here is the top 10 big mac index

Here you are! You now know how to make a horizontal bar chart with Matplotlib using Python.

More on Matplotlib

If you like what you've just read and want to know more about the Matplotlib library (e.g. how to add labels, plot different types of plots, etc...) check out the other articles I wrote on the topic, just here :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.