How to plot Timeseries based charts using Pandas?
A time series is a collection of data points indexed in time order, typically at equal time intervals. Examples include:
- ECG or EEG signals in healthcare
- Stock prices in finance
- Temperature or rainfall data in meteorology
Analyzing and visualizing this data helps uncover trends, seasonality, and patterns that can inform future predictions. Letâs begin by creating a sample dataset and formatting it as time series data.
import pandas as pd
d = {
'Date': ['2020-01-25', '2020-02-25', '2020-03-25', '2020-04-25',
'2020-05-25', '2020-06-25', '2020-07-25', '2020-08-25',
'2020-09-25', '2020-10-25', '2020-11-25', '2020-12-25',
'2021-01-25', '2021-02-25', '2021-03-25', '2021-04-25'],
'A': [102, 114, 703, 547, 641, 669, 897, 994,
1002, 974, 899, 954, 1105, 1189, 1100, 934],
'B': [1029, 1178, 723, 558, 649, 669, 899, 1000,
1012, 984, 918, 959, 1125, 1199, 1109, 954],
'C': [634, 422, 152, 23, 294, 1452, 891, 990,
924, 960, 874, 548, 174, 49, 655, 914],
'D': [1296, 7074, 3853, 4151, 2061, 1478, 2061, 3853,
6379, 2751, 1064, 6263, 2210, 6566, 3918, 1121],
'E': [10, 17, 98, 96, 85, 89, 90, 92,
86, 84, 78, 73, 71, 65, 70, 60]
}
df = pd.DataFrame(d)
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
Output

Explanation: Weâre creating a sample DataFrame with 5 variables (A to E) and a Date column. By converting the Date to datetime and setting it as the index, the DataFrame becomes time series-friendly for plotting.
Plotting the Time-Series Data
Below are common and insightful methods to visualize and analyze time-series data using Python:
1. Line Chart for Time Series
A line chart is the most basic yet effective way to visualize time series. It helps in understanding the overall trend, fluctuations and patterns in the data over time.
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
plt.figure(figsize=(12, 6))
plt.plot(df['A'], label='A')
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Time Series Plot for A')
plt.legend()
plt.show()
Output

Explanation: This code uses matplotlib to plot a line chart of column 'A' over time. It sets a custom style, draws the line, adds labels and a title and displays the plot.
2. Line Chart with All Columns
When dealing with multivariate time series, plotting all variables as subplots can provide a better understanding of each series independently.
df.plot(subplots=True, figsize=(12, 15), title="Multiple Time Series Subplots")
plt.tight_layout()
plt.show()

Explanation: This uses pandas and plot() with subplots=True to generate separate line plots for each column in df. It adjusts the figure size and layout to neatly show multiple time series side by side.
3. Bar Plot
A bar plot is useful when you want to emphasize individual time points, like monthly or yearly comparisons, rather than trends.
plt.figure(figsize=(15, 6))
plt.bar(df.index, df["A"], width=5)
plt.xlabel("Date")
plt.ylabel("Value")
plt.title("Bar Plot of 'A'")
plt.xticks(rotation=45)
plt.show()
Output

Explanation: Creates a bar chart for column 'A' using plt.bar(), with time on the x-axis and values on the y-axis. Bar width and label rotation are set for readability.
4. Rolling Mean Plot
A rolling mean (moving average) smooths out short-term fluctuations and highlights long-term trends. It's essential for identifying the signal in noisy time series data.
plt.figure(figsize=(12, 6))
plt.plot(df["A"], label="Original A")
plt.plot(df["A"].rolling(window=2, min_periods=1).mean(), label="Rolling Mean (2)")
plt.xlabel("Date")
plt.ylabel("Value")
plt.title("Rolling Mean Plot")
plt.legend()
plt.show()
Output

Explanation: This overlays the original data with a 2-period rolling average using .rolling().mean() to smooth short-term fluctuations.
5. Seasonal Decomposition
Time series can be decomposed into Trend, Seasonality, and Residual components. This decomposition provides a clear structure and is vital for modeling and forecasting.
import statsmodels.api as sm
from pylab import rcParams
decomp_data = df[['A']].copy()
decomp_data.index = pd.to_datetime(decomp_data.index)
decomposition = sm.tsa.seasonal_decompose(decomp_data, model='multiplicative', period=5)
rcParams['figure.figsize'] = 12, 10
decomposition.plot()
plt.suptitle('"A" Value Decomposition', fontsize=16)
plt.show()

Explanation: seasonal_decompose() breaks down the time series into trend, seasonal and residual parts, showing hidden structures.
6. Autocorrelation Plot
Autocorrelation measures how the values of a time series are correlated with previous values. This is important for understanding lag dependencies in time series data.
from pandas.plotting import autocorrelation_plot
autocorrelation_plot(df['A'])
plt.title('Autocorrelation Plot of A')
plt.show()
Output

Explanation: Displays how the time series 'A' correlates with its previous values (lags), indicating repeating patterns or dependencies.
7. Box Plot Analysis (Year and Month Wise)
Box plots allow for statistical comparison across years and months, helping detect seasonal patterns, outliers, and variations over time.
import seaborn as sns
df['Year'] = df.index.year
df['Month'] = df.index.month
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
sns.boxplot(x='Year', y='A', data=df, ax=ax[0])
sns.boxplot(x='Month', y='A', data=df, ax=ax[1])
ax[0].set_title('Year-wise Box Plot for A')
ax[1].set_title('Month-wise Box Plot for A')
plt.tight_layout()
plt.show()
Output

Explanation: Adds Year and Month columns and plots box plots to compare value distribution of 'A' across different years and months.
8. Shift Analysis
A shift operation is used to calculate relative changes over time. This is helpful to identify how much the value changes from one time point to another (e.g., daily or monthly growth ratio).
df['Change'] = df['A'].div(df['A'].shift())
df['Change'].plot(figsize=(14, 6), title="Shift Plot")
plt.xlabel("Date")
plt.ylabel("Value Change Ratio")
plt.show()
Output

Explanation: Calculates the relative change between current and previous values using .shift() and .div(), showing growth or volatility trends.
Related articles