Matplotlib plot_date for Scatter and Multiple Line Charts

Recently, I was working on a project where I needed to visualize sales data across different dates for multiple stores in the USA. I wanted to see how sales trends changed over time and also highlight specific points on those dates.

At first, I tried using the regular plt.plot() function, but I quickly realized that handling date-based data in Python can be tricky. That’s when I turned to Matplotlib’s plot_date() function, which is designed specifically for plotting time-series data with dates on the x-axis.

In this tutorial, I’ll show you exactly how I use the Matplotlib plot_date() function in Python to create scatter charts and multiple line charts that look professional and are easy to interpret.

Method 1 – Create a Scatter Chart Using Matplotlib plot_date() in Python

When I first started visualizing date-based data, my goal was to highlight individual data points clearly. Scatter charts are perfect for this because they emphasize each observation on a timeline.

The plot_date() function in Matplotlib works beautifully for scatter-type visualizations when you specify the linestyle=’None’ argument. This tells Matplotlib to plot only points instead of connecting them with lines.

Below is the step-by-step process I follow to create a scatter chart using plot_date() in Python.

Step 1: Import the Required Python Libraries

Before we start, we need to import Matplotlib and Python’s built-in datetime module. These libraries will help us plot and handle date objects efficiently.

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
import random

I always import matplotlib.dates because it gives me better control over date formatting on the x-axis.

Step 2: Create Sample Date and Value Data

Here, I’ll generate a list of dates and random sales values to simulate daily sales data for a store in New York City.

# Generate 15 days of sales data
base_date = datetime(2025, 9, 20)
dates = [base_date + timedelta(days=i) for i in range(15)]
sales = [random.randint(200, 500) for _ in range(15)]

In a real-world scenario, you could replace this with data from a CSV file or a database query.

Step 3: Plot the Scatter Chart Using plot_date()

Now, let’s use the plot_date() function to create a scatter plot.

plt.figure(figsize=(10, 6))
plt.plot_date(dates, sales, linestyle='None', marker='o', color='teal')

plt.title("Daily Sales Scatter Chart - New York Store", fontsize=14)
plt.xlabel("Date", fontsize=12)
plt.ylabel("Sales (in USD)", fontsize=12)

# Format the x-axis to show readable dates
plt.gcf().autofmt_xdate()
plt.grid(True)
plt.show()

You can see the output in the screenshot below.

Use Matplotlib plot_date for Scatter and Multiple Line Charts

This code creates a clean scatter chart where each point represents a specific day’s sales.

Step 4: Customize the Scatter Chart

To make the chart more readable, I often format the date labels using DateFormatter.

plt.figure(figsize=(10, 6))
plt.plot_date(dates, sales, linestyle='None', marker='o', color='darkorange')

# Format x-axis
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=2))

plt.title("Daily Sales Scatter Chart - New York Store", fontsize=14)
plt.xlabel("Date", fontsize=12)
plt.ylabel("Sales (in USD)", fontsize=12)
plt.grid(True)
plt.show()

You can see the output in the screenshot below.

Use Matplotlib plot_date for Scatter, Multiple Line Charts

This version formats the x-axis to show abbreviated month names and day numbers, making it easy for USA-based users to read.

Why I Prefer plot_date() for Scatter Charts

The main reason I prefer plot_date() for scatter charts is that it automatically handles date objects without requiring you to convert them into numerical values. It’s simple, fast, and gives you full control over date formatting.

Method 2 – Create Multiple Line Charts Using Matplotlib plot_date() in Python

After mastering scatter charts, I wanted to compare multiple datasets, for example, sales trends for different stores across the USA. This is where multiple line charts come in handy.

With plot_date(), it’s easy to plot multiple lines on the same chart by calling the function multiple times with different datasets.

Below, I’ll show you how I use this approach in Python to compare sales data from New York, Los Angeles, and Chicago stores.

Step 1: Import the Python Libraries

Just like before, we’ll start by importing Matplotlib and datetime.

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime, timedelta
import random

I usually keep my imports consistent across different Python scripts for readability.

Step 2: Create Date and Multiple Store Data

Here, we’ll simulate 15 days of sales data for three USA stores.

# Base date
base_date = datetime(2025, 9, 20)
dates = [base_date + timedelta(days=i) for i in range(15)]

# Generate random sales data for three stores
ny_sales = [random.randint(200, 500) for _ in range(15)]
la_sales = [random.randint(150, 450) for _ in range(15)]
chicago_sales = [random.randint(180, 470) for _ in range(15)]

This gives us three lists of sales values corresponding to the same date range.

Step 3: Plot Multiple Lines Using plot_date()

Now, let’s plot all three datasets on one chart.

plt.figure(figsize=(12, 7))

plt.plot_date(dates, ny_sales, linestyle='solid', marker='o', color='blue', label='New York')
plt.plot_date(dates, la_sales, linestyle='solid', marker='s', color='green', label='Los Angeles')
plt.plot_date(dates, chicago_sales, linestyle='solid', marker='^', color='red', label='Chicago')

plt.title("Sales Trends Comparison - USA Stores", fontsize=16)
plt.xlabel("Date", fontsize=12)
plt.ylabel("Sales (in USD)", fontsize=12)
plt.legend()

# Format x-axis
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=2))

plt.grid(True)
plt.gcf().autofmt_xdate()
plt.show()

You can see the output in the screenshot below.

Matplotlib plot_date for Scatter and Multiple Line Charts

This chart visually compares the sales performance of three major stores across the same time period.

Step 4: Add Styling and Improve Readability

When I create charts for reports, I like to make them visually consistent. You can adjust line styles, colors, and markers to make the chart look more professional.

plt.figure(figsize=(12, 7))

plt.plot_date(dates, ny_sales, linestyle='-', marker='o', color='navy', label='New York')
plt.plot_date(dates, la_sales, linestyle='--', marker='s', color='darkgreen', label='Los Angeles')
plt.plot_date(dates, chicago_sales, linestyle='-.', marker='^', color='crimson', label='Chicago')

plt.title("USA Store Sales Trends - Multi-Line Chart", fontsize=16)
plt.xlabel("Date", fontsize=12)
plt.ylabel("Sales (in USD)", fontsize=12)
plt.legend(loc='upper left')

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=2))

plt.grid(True, linestyle='--', alpha=0.6)
plt.gcf().autofmt_xdate()
plt.tight_layout()
plt.show()

You can see the output in the screenshot below.

Matplotlib plot_date for Scatter, Multiple Line Charts

This final version gives a polished look, ideal for business presentations or dashboards.

Why I Use plot_date() for Multiple Line Charts

The biggest advantage of plot_date() is its flexibility with date objects. You can easily overlay multiple datasets without converting dates to numbers. It also integrates smoothly with Matplotlib’s date locators and formatters, giving you complete control over how your timeline appears.

Common Tips for Using plot_date() in Python

Here are a few tips I’ve learned over the years while using plot_date() in Python:

  • Always use autofmt_xdate() to automatically rotate and format date labels.
  • Use DayLocator, MonthLocator, or DateFormatter to customize how dates appear.
  • Combine plot_date() with tight_layout() to prevent label overlaps.
  • If you’re working with pandas DataFrames, you can directly pass the date column to plot_date().

These small adjustments can make a big difference in chart readability.

Using Matplotlib’s plot_date() function in Python has completely changed how I handle date-based visualizations. Whether I’m creating a scatter chart to highlight individual data points or a multiple line chart to compare trends across different cities, plot_date() gives me the flexibility and precision I need.

It’s simple, efficient, and integrates seamlessly with other Matplotlib features. If you regularly work with time-series data in Python, I highly recommend mastering this function; it will save you time and make your visualizations look professional.

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