Contents
Time Series Visualization
Time-based data needs special care in charts. Dates, seasons, and events all play a role. This topic shows how to display time series clearly and bring out trends and patterns.
a. Plotting Time Series Data
Start by making sure your date column is a real date type, and set it as the index. Pandas can then plot time series directly, and it will handle the date labels well.
df["date"] = pd.to_datetime(df["date"])
df = df.set_index("date")
df["sales"].plot(figsize=(12, 5), title="Daily Sales")Place time on the horizontal axis, moving from left to right. Choose a line chart for continuous data. For counts per period, such as sales per month, a bar chart can also work.
If the data has gaps, check them. A line can hide missing dates by connecting the points on either side, so your chart may look smoother than reality.
b. Showing Trends and Seasonality
Real time series often have a long-term trend and repeating seasonal patterns. To see these clearly, you can aggregate the data to a coarser level, such as monthly totals.
monthly = df["sales"].resample("ME").sum()
monthly.plot(marker="o")You can also compare the same period across years by plotting each year as a separate line. This makes seasonal patterns obvious, such as a spike every December.
For a deeper view, the statsmodels library can split a series into trend, seasonal, and leftover parts, and plot each separately.
c. Rolling Averages in Charts
Daily data is often noisy, with many small ups and downs. A rolling average smooths these out and reveals the underlying trend. It is the average of the last few values, recalculated at each step.
df["rolling_7"] = df["sales"].rolling(window=7).mean()
df[["sales", "rolling_7"]].plot(figsize=(12, 5))Plot the original data in a light color and the rolling average in a stronger one. This shows both the detail and the trend together.
A longer window gives a smoother line but reacts more slowly to change. Choose a window that matches the cycle in your data, such as seven days for weekly patterns.
d. Highlighting Events and Date Ranges
Time series charts often become more meaningful when you mark important events, such as a product launch or a holiday sale. Use vertical lines or shaded areas to point them out.
ax = df["sales"].plot(figsize=(12, 5))
ax.axvline(pd.to_datetime("2026-03-01"), color="red", linestyle="--")
ax.axvspan(pd.to_datetime("2026-06-01"), pd.to_datetime("2026-06-15"),
color="orange", alpha=0.2)
ax.text(pd.to_datetime("2026-03-02"), df["sales"].max(), "Launch")The axvline function draws a vertical line, and axvspan shades a range of dates. Short text labels explain what each mark means.
Use these marks sparingly. A few clear highlights help tell the story, but too many make the chart cluttered.
Data Visualization Tutorial
Wesline Rinisha








