Plot a Horizontal Bar Chart in Python Matplotlib

I was working on a project where I had to compare the average cost of living across major U.S. cities. I wanted a chart that would make the differences easy to spot at a glance.

The problem was, a regular vertical bar chart didn’t look clean enough for this dataset. That’s when I realized a horizontal bar chart in Python Matplotlib would be the perfect solution.

In this tutorial, I’ll share the exact steps I used to create horizontal bar charts in Python. I’ll also cover multiple methods, small tips, and customizations that make your chart more professional.

What is a Horizontal Bar Chart?

A horizontal bar chart is similar to a vertical bar chart, except the bars run from left to right instead of bottom to top.

This type of chart is especially useful when you have long category names (like city names) or when comparing values across many categories.

Python’s Matplotlib library makes it simple to create horizontal bar charts with just a few lines of code.

Method 1 – Create a Simple Horizontal Bar Chart in Python

Let me start with the most basic example. I’ll plot a horizontal bar chart showing the average monthly rent in a few U.S. cities.

This is a very common dataset that people in the USA can relate to, and it makes the example practical.

import matplotlib.pyplot as plt

# Data for average monthly rent (USD)
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
rent = [3500, 2800, 2200, 1800, 1600]

# Create horizontal bar chart
plt.barh(cities, rent, color="skyblue")

# Add labels and title
plt.xlabel("Average Rent (USD)")
plt.ylabel("City")
plt.title("Average Monthly Rent in Major U.S. Cities")

# Show chart
plt.show()

You can refer to the screenshot below to see the output.

Horizontal Bar Chart in Python Matplotlib

When you run this code, you’ll see a clean horizontal bar chart with city names on the vertical axis.

Method 2 – Add Values to Each Bar

Often, it’s useful to display the actual values on the bars. This makes the chart more informative without needing to read the axis carefully.

Here’s how I usually add the rent values directly on the bars.

import matplotlib.pyplot as plt

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
rent = [3500, 2800, 2200, 1800, 1600]

plt.barh(cities, rent, color="lightgreen")

# Add labels and title
plt.xlabel("Average Rent (USD)")
plt.title("Average Monthly Rent in Major U.S. Cities")

# Add values on bars
for index, value in enumerate(rent):
    plt.text(value + 50, index, str(value))

plt.show()

You can refer to the screenshot below to see the output.

Plot Horizontal Bar Chart Python Matplotlib

In this chart, the rent values appear next to each bar. I find this especially helpful when presenting data to a non-technical audience.

Method 3 – Sort the Bars for Better Readability

Sometimes, the categories look messy if they’re not sorted. I usually sort the values so the chart flows from smallest to largest.

This makes the chart easier to read and more professional.

import matplotlib.pyplot as plt

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
rent = [3500, 2800, 2200, 1800, 1600]

# Sort data
sorted_data = sorted(zip(rent, cities))
rent_sorted, cities_sorted = zip(*sorted_data)

plt.barh(cities_sorted, rent_sorted, color="orange")

plt.xlabel("Average Rent (USD)")
plt.title("Sorted Average Monthly Rent in Major U.S. Cities")

plt.show()

You can refer to the screenshot below to see the output.

Python Matplotlib Horizontal Bar Chart

The result is a horizontal bar chart with bars ordered from lowest to highest rent. This small step makes a big difference in readability.

Method 4 – Customize Colors and Styles

One of the best things about Matplotlib is how customizable it is. For example, I can use different colors for each bar to highlight differences.

import matplotlib.pyplot as plt

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
rent = [3500, 2800, 2200, 1800, 1600]

colors = ["red", "blue", "green", "purple", "brown"]

plt.barh(cities, rent, color=colors)

plt.xlabel("Average Rent (USD)")
plt.title("Average Monthly Rent by City (Custom Colors)")

plt.show()

You can refer to the screenshot below to see the output.

Horizontal Bar Chart Python Matplotlib

This chart uses a unique color for each city, making it visually engaging. When I’m preparing a report for clients, I often use this approach to make charts stand out.

Method 5 – Horizontal Bar Chart from a Pandas DataFrame

In real-world Python projects, I often work with data stored in Pandas DataFrames. Here’s how you can quickly create a horizontal bar chart from a DataFrame.

import matplotlib.pyplot as plt
import pandas as pd

# Create DataFrame
data = {
    "City": ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"],
    "Rent": [3500, 2800, 2200, 1800, 1600]
}
df = pd.DataFrame(data)

# Plot horizontal bar chart
df.plot(kind="barh", x="City", y="Rent", color="teal", legend=False)

plt.xlabel("Average Rent (USD)")
plt.title("Average Monthly Rent in Major U.S. Cities (DataFrame Example)")

plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Plot Horizontal Bar Chart Python

Using Pandas makes the code shorter and easier to maintain when dealing with larger datasets. This is my go-to method when I’m working with CSVs or databases.

Method 6 – Add Gridlines for Better Comparison

Adding gridlines helps the reader compare values more easily.

Here’s how to add horizontal gridlines to the chart.

import matplotlib.pyplot as plt

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
rent = [3500, 2800, 2200, 1800, 1600]

plt.barh(cities, rent, color="steelblue")

plt.xlabel("Average Rent (USD)")
plt.title("Average Monthly Rent in Major U.S. Cities")

# Add gridlines
plt.grid(axis="x", linestyle="--", alpha=0.7)

plt.show()

Now the chart has dotted gridlines, making it easier to compare across cities. This is a small but powerful improvement for presentations.

Method 7 – Horizontal Bar Chart with Negative Values

Sometimes, the dataset includes both positive and negative values.

For example, let’s say we’re comparing job growth rates in different U.S. cities.

import matplotlib.pyplot as plt

cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
growth = [2.5, -1.2, 1.8, -0.5, 3.0]

plt.barh(cities, growth, color="coral")

plt.xlabel("Job Growth Rate (%)")
plt.title("Job Growth in Major U.S. Cities")

plt.axvline(0, color="black")  # Add vertical line at 0

plt.show()

This chart makes it clear which cities had positive growth and which had negative growth. I find this especially useful in business or economic reports.

Things to Keep in Mind

  • Use horizontal bar charts when category labels are long.
  • Always add axis labels and a title for clarity.
  • Consider sorting the bars for better readability.
  • Add values or gridlines when presenting to non-technical audiences.
  • Use consistent colors unless you want to highlight differences.

So that’s how I create horizontal bar charts in Python using Matplotlib.

I showed you multiple methods: from a simple chart to sorted bars, custom colors, DataFrame integration, and even handling negative values.

Whenever I work with categorical data, I find horizontal bar charts to be one of the most effective ways to present insights.

You may like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.