Plot a Bar Chart from a Dictionary in Python Matplotlib

I have been working with Python for many years, and one of the most common tasks I face is creating quick visualizations from data.

Many times, the data I receive is in the form of a Python dictionary. Instead of converting it into a DataFrame or CSV, I often directly plot it using Matplotlib bar charts.

In this tutorial, I will show you step by step how to plot a bar chart from a dictionary in Python Matplotlib. I’ll cover multiple methods so you can pick the one that fits your workflow best.

Methods to Use a Python Dictionary for Bar Charts

I like using dictionaries because they are easy to create and manage. For example, if I want to compare the average salaries of different professions in the USA, I can store them in a dictionary like this:

salaries = {
    "Software Engineer": 120000,
    "Data Scientist": 115000,
    "Teacher": 60000,
    "Nurse": 75000,
    "Accountant": 70000
}

This makes it very convenient to directly pass the keys and values into a Matplotlib bar chart.

Method 1 – Use plt.bar() in Python

The simplest way to plot a bar chart from a Python dictionary is by using the plt.bar() function from Matplotlib.

Here is the complete code:

import matplotlib.pyplot as plt

# Sample dictionary with average salaries in the USA
salaries = {
    "Software Engineer": 120000,
    "Data Scientist": 115000,
    "Teacher": 60000,
    "Nurse": 75000,
    "Accountant": 70000
}

# Extract keys and values
professions = list(salaries.keys())
income = list(salaries.values())

# Create bar chart
plt.bar(professions, income, color="skyblue")

# Add labels and title
plt.xlabel("Profession")
plt.ylabel("Average Salary (USD)")
plt.title("Average Salaries in the USA by Profession")

# Rotate x-axis labels for better readability
plt.xticks(rotation=30)

# Show chart
plt.show()

I have executed the above example code and added the screenshot below.

Bar Chart from a Dictionary in Python Matplotlib

This method is my go-to when I want a quick visualization without much customization. The plt.bar() function directly takes the keys as labels and values as heights.

Method 2 – Horizontal Bar Chart from Python Dictionary

Sometimes, when the dictionary has long keys, a horizontal bar chart looks much better.

Here is the code:

import matplotlib.pyplot as plt

# Sample dictionary with population of US states
population = {
    "California": 39500000,
    "Texas": 29000000,
    "Florida": 21500000,
    "New York": 19500000,
    "Illinois": 12500000
}

# Extract keys and values
states = list(population.keys())
pop_values = list(population.values())

# Create horizontal bar chart
plt.barh(states, pop_values, color="lightgreen")

# Add labels and title
plt.xlabel("Population (in millions)")
plt.ylabel("States")
plt.title("Top 5 US States by Population")

# Show chart
plt.show()

I have executed the above example code and added the screenshot below.

Plot Bar Chart from a Dictionary in Python Matplotlib

I often use this method when the labels are long or when I want to emphasize the comparative values more clearly.

Method 3 – Sort Python Dictionary Before Plotting

Sometimes, the dictionary data is unordered, and I want to plot the bars in ascending or descending order.

Here’s how I do it in Python:

import matplotlib.pyplot as plt

# Sample dictionary with US car sales
car_sales = {
    "Ford": 1800000,
    "Toyota": 2200000,
    "Chevrolet": 1750000,
    "Honda": 1400000,
    "Nissan": 1200000
}

# Sort dictionary by values (descending order)
sorted_sales = dict(sorted(car_sales.items(), key=lambda item: item[1], reverse=True))

brands = list(sorted_sales.keys())
sales = list(sorted_sales.values())

# Create bar chart
plt.bar(brands, sales, color="orange")

# Add labels and title
plt.xlabel("Car Brand")
plt.ylabel("Units Sold")
plt.title("Car Sales in the USA (Sorted by Sales)")

# Show chart
plt.show()

I have executed the above example code and added the screenshot below.

Python Matplotlib Bar Chart from a Dictionary

Sorting makes the visualization much more meaningful, especially when comparing performance across categories.

Method 4 – Add Values on Top of Bars

When presenting data, I often want to display the actual values on top of each bar.

Here’s how I add them:

import matplotlib.pyplot as plt

# Sample dictionary with monthly expenses
expenses = {
    "Rent": 1500,
    "Groceries": 600,
    "Utilities": 200,
    "Transport": 300,
    "Entertainment": 250
}

categories = list(expenses.keys())
amounts = list(expenses.values())

# Create bar chart
plt.bar(categories, amounts, color="purple")

# Add labels and title
plt.xlabel("Expense Category")
plt.ylabel("Amount (USD)")
plt.title("Monthly Expenses Breakdown")

# Add values on top of bars
for i, value in enumerate(amounts):
    plt.text(i, value + 20, str(value), ha="center")

plt.show()

I have executed the above example code and added the screenshot below.

Plot a Bar Chart from a Dictionary in Python Matplotlib

This makes the chart more professional and easier to interpret at a glance.

Method 5 – Use pandas with Matplotlib in Python

Although dictionaries are simple, sometimes I convert them into a pandas DataFrame for more flexibility.

Here’s how I do it:

import matplotlib.pyplot as plt
import pandas as pd

# Sample dictionary with average temperatures in US cities
temperatures = {
    "New York": 55,
    "Los Angeles": 65,
    "Chicago": 50,
    "Houston": 70,
    "Phoenix": 75
}

# Convert dictionary to DataFrame
df = pd.DataFrame(list(temperatures.items()), columns=["City", "Temperature"])

# Plot bar chart using pandas
df.plot(kind="bar", x="City", y="Temperature", color="teal", legend=False)

# Add title and labels
plt.title("Average Annual Temperatures in Major US Cities")
plt.ylabel("Temperature (°F)")

plt.show()

I use this method when I want to combine pandas with Matplotlib for more advanced analysis.

Bonus Tip – Customize Colors

Sometimes I want each bar to have a different color. Here’s a quick example:

import matplotlib.pyplot as plt

# Sample dictionary with fruit sales
fruit_sales = {
    "Apples": 500,
    "Bananas": 400,
    "Oranges": 300,
    "Grapes": 200,
    "Mangoes": 100
}

fruits = list(fruit_sales.keys())
sales = list(fruit_sales.values())

colors = ["red", "yellow", "orange", "purple", "green"]

plt.bar(fruits, sales, color=colors)

plt.title("Fruit Sales in a Local Market")
plt.xlabel("Fruit")
plt.ylabel("Units Sold")

plt.show()

This method is excellent when I want to make the chart visually engaging.

Creating a bar chart from a Python dictionary in Matplotlib is simple and powerful.

I showed you different methods:

  • Directly using plt.bar()
  • Horizontal bar charts with plt.barh()
  • Sorting dictionary values before plotting
  • Adding values on top of bars
  • Using pandas for flexibility
  • Customizing colors for better visuals

I personally use these techniques almost daily in my Python projects, whether for quick analysis or professional presentations.

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