0Pricing
Pandas & NumPy Academy · Lesson

Monthly Trend Visualisation

Plot monthly revenue as a line chart, overlay a 3-month moving average, and annotate peak and trough months.

Computing Monthly Revenue Totals

Before plotting, aggregate the sales DataFrame to monthly totals. Group by the year_month Period column and sum revenue. Convert the PeriodIndex to a DatetimeIndex with .to_timestamp() so Matplotlib's date formatter renders tick labels correctly. Sorting by index ensures the line chart flows left to right chronologically.

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_parquet('sales_features.parquet')

monthly = df.groupby('year_month')['revenue'].sum().sort_index()
monthly.index = monthly.index.to_timestamp()
print(monthly.head())

Plotting the Monthly Revenue Line Chart

Create a Figure and Axes with plt.subplots(figsize=(12, 5)) and call ax.plot() to draw the monthly revenue trend. Use marker='o' to place a dot at each month so individual data points are visible even when the line is smooth. Set the colour and line width to make the primary trend stand out from any overlays you add later.

fig, ax = plt.subplots(figsize=(12, 5))

ax.plot(monthly.index, monthly.values,
        color='steelblue', linewidth=2, marker='o', markersize=5,
        label='Monthly Revenue')

ax.set_title('Monthly Revenue Trend')
ax.set_xlabel('Month')
ax.set_ylabel('Revenue (USD)')
plt.tight_layout()
plt.show()

All lessons in this course

  1. Loading and Auditing the Sales Dataset
  2. Revenue Calculations and Feature Engineering
  3. GroupBy Analysis by Region and Category
  4. Monthly Trend Visualisation
← Back to Pandas & NumPy Academy