Plot a Bar Chart with Dates in Matplotlib

When I first started working with Matplotlib in Python, I often needed to create bar charts where the x-axis represented dates.

I’ve worked on multiple projects in the USA where date-based bar charts were essential. For example, tracking daily sales, visualizing monthly website traffic, or analyzing yearly energy consumption.

In this tutorial, I’ll walk you through different methods to plot bar charts with dates in Matplotlib using Python. I’ll share my firsthand experience so you can follow along easily.

Use Dates in Bar Charts

In real-world Python projects, data is rarely just numbers. Most datasets include a time component—daily sales, weekly tasks, or monthly expenses.

By plotting dates on the x-axis, we make the visualization more meaningful and easier to interpret.

Method 1 – Plot a Bar Chart with Dates in Python Matplotlib

The most common situation is when you have a list of dates and corresponding values. Here’s how I use Python with Matplotlib to create a simple bar chart with dates.

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

# Sample data (USA daily sales report)
dates = [
    datetime.date(2025, 9, 1),
    datetime.date(2025, 9, 2),
    datetime.date(2025, 9, 3),
    datetime.date(2025, 9, 4),
    datetime.date(2025, 9, 5)
]
sales = [120, 150, 90, 200, 170]

# Create bar chart
plt.figure(figsize=(8, 5))
plt.bar(dates, sales, color="skyblue")

# Format x-axis to show dates clearly
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%b %d"))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())

plt.title("Daily Sales Report (USA)")
plt.xlabel("Date")
plt.ylabel("Sales ($)")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Plot Bar Chart with Dates in Matplotlib

In this Python code, I used matplotlib.dates to format the x-axis so that the dates appear in a readable format.

Method 2 – Use Python Pandas with Matplotlib for Date Bar Charts

When I work with larger datasets, I often use Pandas with Matplotlib. Pandas make handling dates much easier.

import pandas as pd
import matplotlib.pyplot as plt

# Sample dataset
data = {
    "Date": pd.date_range(start="2025-01-01", periods=6, freq="M"),
    "Visitors": [1200, 1350, 1600, 1800, 1750, 1900]
}

df = pd.DataFrame(data)

# Plot bar chart
plt.figure(figsize=(8, 5))
plt.bar(df["Date"], df["Visitors"], color="orange")

plt.title("Monthly Website Visitors (USA)")
plt.xlabel("Month")
plt.ylabel("Visitors")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Bar Chart with Dates in Matplotlib

With Pandas, the date_range function makes it easy to generate dates. This method is best when working with time series data stored in DataFrames.

Method 3 – Group Data by Week or Month

Sometimes, daily data is too detailed. In one of my projects, I grouped sales data by week to get a clearer picture.

Here’s how I do it in Python using Pandas and Matplotlib.

import pandas as pd
import matplotlib.pyplot as plt

# Sample daily sales data
data = {
    "Date": pd.date_range(start="2025-08-01", periods=30, freq="D"),
    "Sales": [100 + i*2 for i in range(30)]
}
df = pd.DataFrame(data)

# Group by week
df["Week"] = df["Date"].dt.to_period("W").apply(lambda r: r.start_time)
weekly_sales = df.groupby("Week")["Sales"].sum().reset_index()

# Plot weekly sales
plt.figure(figsize=(8, 5))
plt.bar(weekly_sales["Week"], weekly_sales["Sales"], color="green")

plt.title("Weekly Sales Report (USA)")
plt.xlabel("Week")
plt.ylabel("Total Sales ($)")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Bar Chart with Dates in Python Matplotlib

By grouping the data into weeks, the bar chart becomes easier to read and interpret. This method is especially useful for business reports where weekly or monthly summaries matter more than daily details.

Method 4 – Customize Date Formatting in Matplotlib

Sometimes, the default date formatting in Matplotlib doesn’t look great. I often customize it for better readability.

Here’s an example where I format the x-axis to show month and year.

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd

# Sample dataset
data = {
    "Date": pd.date_range(start="2024-01-01", periods=12, freq="M"),
    "Revenue": [5000, 5200, 4800, 6000, 5900, 6100, 6500, 6400, 7000, 7200, 7100, 7500]
}
df = pd.DataFrame(data)

# Plot bar chart
plt.figure(figsize=(10, 6))
plt.bar(df["Date"], df["Revenue"], color="purple")

# Format x-axis
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator())

plt.title("Monthly Revenue (USA)")
plt.xlabel("Month")
plt.ylabel("Revenue ($)")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Matplotlib Plot a Bar Chart with Dates

This method gives the chart a professional look, which is important when presenting data to clients or stakeholders.

Method 5 – Handle Missing Dates in Bar Charts

In real-world Python projects, datasets often have missing dates. If we don’t handle them, the bar chart may look misleading.

import pandas as pd
import matplotlib.pyplot as plt

# Sample dataset with missing dates
data = {
    "Date": pd.to_datetime(["2025-09-01", "2025-09-03", "2025-09-05"]),
    "Orders": [50, 80, 70]
}
df = pd.DataFrame(data)

# Reindex with full date range
full_range = pd.date_range(start="2025-09-01", end="2025-09-05")
df = df.set_index("Date").reindex(full_range, fill_value=0).rename_axis("Date").reset_index()

# Plot bar chart
plt.figure(figsize=(8, 5))
plt.bar(df["Date"], df["Orders"], color="red")

plt.title("Orders with Missing Dates Filled (USA)")
plt.xlabel("Date")
plt.ylabel("Orders")
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()

This ensures that the x-axis shows all dates, even if some had zero values. It’s a small step, but it makes the visualization more accurate.

Additional Tips for Better Date Bar Charts in Python

  • Always rotate the x-axis labels for better readability.
  • Use tight_layout() to prevent labels from overlapping.
  • Choose colors that match the theme of your report or presentation.
  • Group data by week, month, or quarter for clearer insights.

Conclusion

In this tutorial, I showed you how to plot bar charts with dates in Matplotlib using Python.

We covered multiple methods:

  • A simple bar chart with dates
  • Using Pandas for easier handling
  • Grouping data by week or month
  • Customizing date formatting
  • Handling missing dates

These are the exact techniques I use in my day-to-day Python projects when working with time series data.

If you follow these methods, you’ll be able to create professional bar charts that clearly communicate your data story.

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.