How to make a horizontal bar chart with Matplotlib using Python

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

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.
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.