Bar Chart from a DataFrame in Python Matplotlib

I have been working with Python and data visualization for more than a decade. I have often needed to create quick bar charts directly from Pandas DataFrames.

At first, I used to export my data to Excel and create charts there. But once I realized how powerful Matplotlib is, I switched to plotting directly in Python. This saved me a lot of time and gave me more control over formatting.

In this tutorial, I will show you step by step how to plot a bar chart from a DataFrame using Python Matplotlib. I will cover multiple methods so you can choose whichever feels most comfortable.

What is a Bar Chart in Python Matplotlib?

A bar chart is one of the most common ways to visualize categorical data. It uses rectangular bars to represent values.

In Python, Matplotlib makes it very easy to create bar charts. You can either plot them directly from a Pandas DataFrame or by passing lists/arrays.

Since most of us already work with DataFrames in Pandas, I’ll focus on plotting bar charts directly from them.

Dataset Example – U.S. State Sales Data

To make this tutorial practical, I created a small dataset of sales numbers for different U.S. states.

Here is the dataset in a Pandas DataFrame:

import pandas as pd

# Sample sales data for U.S. states
data = {
    "State": ["California", "Texas", "New York", "Florida", "Illinois"],
    "Sales": [250000, 180000, 200000, 150000, 120000]
}

df = pd.DataFrame(data)
print(df)

When you run this code, you will see:

        State   Sales
0  California  250000
1       Texas  180000
2    New York  200000
3     Florida  150000
4    Illinois  120000

This dataset will be the foundation for our bar chart examples.

Method 1 – Use Pandas .plot() in Python

The easiest way to plot a bar chart from a DataFrame is by using Pandas’ built‑in .plot() method. This internally uses Matplotlib.

import matplotlib.pyplot as plt

# Plot bar chart using Pandas
df.plot(x="State", y="Sales", kind="bar", legend=False)

plt.title("Sales by State (USA)")
plt.xlabel("State")
plt.ylabel("Sales in USD")
plt.show()

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

Bar Chart from a DataFrame in Python Matplotlib

This method is quick and requires very little code. The .plot(kind=”bar”) option tells Pandas to create a bar chart.

Method 2 – Plot Bar Chart Using Matplotlib plt.bar()

Sometimes, I need more control over the chart. In that case, I use Matplotlib’s plt.bar() function directly.

# Plot bar chart using Matplotlib directly
plt.bar(df["State"], df["Sales"], color="skyblue")

plt.title("Sales by State (USA)")
plt.xlabel("State")
plt.ylabel("Sales in USD")
plt.xticks(rotation=45)
plt.show()

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

Python Matplotlib Bar Chart from a DataFrame

This method gives me full control over colors, labels, and formatting. For example, I rotated the x‑axis labels so they don’t overlap.

Method 3 – Horizontal Bar Chart

In some cases, horizontal bar charts are easier to read, especially when state names are long.

# Horizontal bar chart
plt.barh(df["State"], df["Sales"], color="orange")

plt.title("Sales by State (USA)")
plt.xlabel("Sales in USD")
plt.ylabel("State")
plt.show()

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

Bar Chart from a DataFrame in Matplotlib

I prefer horizontal bar charts when dealing with long labels or when I want to emphasize ranking.

Method 4 – Grouped Bar Chart from DataFrame

Let’s say we have sales data for two different years. We can plot them side by side using a grouped bar chart.

# Sample dataset with two years
data = {
    "State": ["California", "Texas", "New York", "Florida", "Illinois"],
    "2023": [250000, 180000, 200000, 150000, 120000],
    "2024": [270000, 190000, 210000, 160000, 130000]
}

df2 = pd.DataFrame(data)

# Plot grouped bar chart
x = range(len(df2["State"]))
plt.bar([p - 0.2 for p in x], df2["2023"], width=0.4, label="2023")
plt.bar([p + 0.2 for p in x], df2["2024"], width=0.4, label="2024")

plt.xticks(x, df2["State"], rotation=45)
plt.title("Sales by State (USA) – 2023 vs 2024")
plt.xlabel("State")
plt.ylabel("Sales in USD")
plt.legend()
plt.show()

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

Matplotlib Bar Chart from a DataFrame in Python

This method is very useful when I want to compare multiple categories side by side.

Method 5 – Stacked Bar Chart

Another way to compare categories is by stacking bars on top of each other.

# Stacked bar chart
df2.set_index("State")[["2023", "2024"]].plot(kind="bar", stacked=True)

plt.title("Stacked Sales by State (USA)")
plt.xlabel("State")
plt.ylabel("Sales in USD")
plt.show()

Stacked bar charts are helpful when I want to show the total as well as the contribution of each category.

Add Labels to Bars in Matplotlib

Sometimes, I want to display the exact sales numbers on top of each bar. This makes the chart more informative.

# Bar chart with labels
bars = plt.bar(df["State"], df["Sales"], color="green")

plt.title("Sales by State (USA)")
plt.xlabel("State")
plt.ylabel("Sales in USD")

# Add labels
for bar in bars:
    plt.text(bar.get_x() + bar.get_width()/2, bar.get_height() - 10000,
             f'{bar.get_height():,}', ha='center', va='bottom', color="white", fontweight="bold")

plt.show()

Adding labels makes it easier for readers to understand the chart at a glance without guessing the values.

Style the Bar Chart in Python Matplotlib

Matplotlib allows me to style the chart in many ways:

  • Change bar colors
  • Add gridlines
  • Customize font sizes
  • Use different themes
plt.style.use("ggplot")  # Apply ggplot style

plt.bar(df["State"], df["Sales"], color="purple")

plt.title("Styled Sales by State (USA)")
plt.xlabel("State")
plt.ylabel("Sales in USD")
plt.grid(True, axis="y", linestyle="--", alpha=0.7)
plt.show()

I often use styles like “ggplot” or “seaborn” to make the chart look modern and professional.

When to Use Each Method?

  • Pandas .plot() → Quick and simple bar chart
  • Matplotlib plt.bar() → Full customization
  • Horizontal bar chart → Best for long labels
  • Grouped bar chart → Compare categories across years
  • Stacked bar chart → Show totals and contributions

Choosing the right method depends on the story you want to tell with your data.

Creating bar charts from a DataFrame in Python Matplotlib is straightforward once you know the different methods.

I personally start with Pandas’ .plot() when I want something quick. But when I need more control, I switch to Matplotlib’s plt.bar() or grouped/stacked charts.

You can now confidently plot bar charts directly from your DataFrame and customize them for your needs. With these techniques, you’ll save time and create professional‑looking charts for your reports or dashboards.

You may also 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.