Time Series Plots in Matplotlib¶
Time series plots are essential for visualizing data that changes over time, such as stock prices, weather patterns, or website traffic. It is crucial for analyzing trends, patterns, and changes within data.
Setting Up for Time Series Plotting¶
To begin with time series plotting, let’s import the required libraries and configure a basic style for visual appeal.
In [4]:
Copied!
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
from matplotlib.dates import DateFormatter
# Applying a style for aesthetics
plt.style.use('fivethirtyeight')
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
from matplotlib.dates import DateFormatter
# Applying a style for aesthetics
plt.style.use('fivethirtyeight')
Explanation:
- Libraries:
matplotlib.pyplot
: For plotting.pandas
: Useful for handling datasets with date columns.datetime
: Helps create and manipulate date objects.DateFormatter
: Allows custom date formatting on the x-axis.
- Style: Setting a visual style like "Seaborn" enhances the readability and appearance of the plot.
Creating a Basic Time Series Plot¶
For time series plots, we need data that includes dates and corresponding values.
Step-by-Step Code
- Define Sample Data: We’ll create sample date data and corresponding values.
- Use
plt.plot_date()
: This function is designed to handle dates on the x-axis.
In [5]:
Copied!
# Sample data for dates and values
dates = pd.date_range(start="2023-01-01", periods=10, freq="D")
values = [10, 12, 14, 15, 13, 12, 13, 16, 18, 20]
# Creating the time series plot
plt.plot_date(dates, values, linestyle='solid')
# Basic plot configuration
plt.title("Sample Time Series Plot")
plt.xlabel("Date")
plt.ylabel("Values")
plt.tight_layout()
plt.show()
# Sample data for dates and values
dates = pd.date_range(start="2023-01-01", periods=10, freq="D")
values = [10, 12, 14, 15, 13, 12, 13, 16, 18, 20]
# Creating the time series plot
plt.plot_date(dates, values, linestyle='solid')
# Basic plot configuration
plt.title("Sample Time Series Plot")
plt.xlabel("Date")
plt.ylabel("Values")
plt.tight_layout()
plt.show()
/var/folders/_0/tn3nfnd50992l7rbgrxxlkn40000gn/T/ipykernel_40864/1097747862.py:6: MatplotlibDeprecationWarning: The plot_date function was deprecated in Matplotlib 3.9 and will be removed in 3.11. Use plot instead. plt.plot_date(dates, values, linestyle='solid')
Explanation:
- Dates and Values: We use
pd.date_range()
to create dates and pair them with corresponding values. - Line Style: The default marker style with
plot_date()
is dots; settinglinestyle='solid'
changes it to a continuous line. - Basic Plot Configuration: Adding title and axis labels makes the plot informative.
In [6]:
Copied!
# Automatic rotation for date labels
plt.plot_date(dates, values, linestyle='solid')
plt.gcf().autofmt_xdate() # Automatically formats and rotates date labels
plt.title("Sample Time Series Plot with Auto Formatted Dates")
plt.xlabel("Date")
plt.ylabel("Values")
plt.show()
# Automatic rotation for date labels
plt.plot_date(dates, values, linestyle='solid')
plt.gcf().autofmt_xdate() # Automatically formats and rotates date labels
plt.title("Sample Time Series Plot with Auto Formatted Dates")
plt.xlabel("Date")
plt.ylabel("Values")
plt.show()
/var/folders/_0/tn3nfnd50992l7rbgrxxlkn40000gn/T/ipykernel_40864/142406069.py:2: MatplotlibDeprecationWarning: The plot_date function was deprecated in Matplotlib 3.9 and will be removed in 3.11. Use plot instead. plt.plot_date(dates, values, linestyle='solid')
Custom Date Formats¶
Custom date formats allow for more control over how dates appear, making them easy to interpret at a glance.
In [7]:
Copied!
# Custom date formatting
fig, ax = plt.subplots()
ax.plot(dates, values, linestyle='solid')
# Custom date formatter
date_format = DateFormatter("%b %d, %Y") # e.g., Jan 01, 2023
ax.xaxis.set_major_formatter(date_format)
# Rotating and formatting dates
fig.autofmt_xdate()
plt.title("Sample Time Series Plot with Custom Date Format")
plt.xlabel("Date")
plt.ylabel("Values")
plt.show()
# Custom date formatting
fig, ax = plt.subplots()
ax.plot(dates, values, linestyle='solid')
# Custom date formatter
date_format = DateFormatter("%b %d, %Y") # e.g., Jan 01, 2023
ax.xaxis.set_major_formatter(date_format)
# Rotating and formatting dates
fig.autofmt_xdate()
plt.title("Sample Time Series Plot with Custom Date Format")
plt.xlabel("Date")
plt.ylabel("Values")
plt.show()
Explanation:
- Automatic Formatting:
plt.gcf().autofmt_xdate()
rotates dates to prevent overlap. - Custom Date Format: Using
DateFormatter
allows for precise date format control. The example format ("%b %d, %Y"
) shows dates as "Jan 01, 2023". - Setting the Formatter:
ax.xaxis.set_major_formatter()
applies the custom format to the x-axis.
In [8]:
Copied!
# Creating a DataFrame with unsorted dates
data = {
'Date': ["2023-01-10", "2023-01-05", "2023-01-01", "2023-01-07", "2023-01-03"],
'Value': [20, 12, 15, 18, 14]
}
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date']) # Convert to datetime format
# Sort the DataFrame by date
df.sort_values('Date', inplace=True)
# Plotting the sorted data
plt.plot_date(df['Date'], df['Value'], linestyle='solid')
plt.title("Time Series Plot with Sorted Dates")
plt.xlabel("Date")
plt.ylabel("Values")
plt.gcf().autofmt_xdate() # Automatic rotation for readability
plt.show()
# Creating a DataFrame with unsorted dates
data = {
'Date': ["2023-01-10", "2023-01-05", "2023-01-01", "2023-01-07", "2023-01-03"],
'Value': [20, 12, 15, 18, 14]
}
df = pd.DataFrame(data)
df['Date'] = pd.to_datetime(df['Date']) # Convert to datetime format
# Sort the DataFrame by date
df.sort_values('Date', inplace=True)
# Plotting the sorted data
plt.plot_date(df['Date'], df['Value'], linestyle='solid')
plt.title("Time Series Plot with Sorted Dates")
plt.xlabel("Date")
plt.ylabel("Values")
plt.gcf().autofmt_xdate() # Automatic rotation for readability
plt.show()
/var/folders/_0/tn3nfnd50992l7rbgrxxlkn40000gn/T/ipykernel_40864/3869305569.py:13: MatplotlibDeprecationWarning: The plot_date function was deprecated in Matplotlib 3.9 and will be removed in 3.11. Use plot instead. plt.plot_date(df['Date'], df['Value'], linestyle='solid')
Explanation:
- Unsorted Dates: If dates are out of order, plotting without sorting can result in a confusing plot where lines jump back and forth.
- Sorting by Date:
df.sort_values('Date', inplace=True)
sorts the DataFrame by date to ensure that values are plotted in chronological order.