季节子序列图
每个日历月份的制造业销售额在各年间的变化,附该月均值。

季节子序列图(又称月份图)将月度序列切分为十二条迷你时间序列,每个日历月份一条,各自贯穿所有年份。水平横条为该月的均值,因此从一根横条到下一根横条即可读出季节形态,而每个面板内的折线则显示该月份在不同年份间是在漂移还是在波动。
背景为 Warin (2024, Technology Innovations in Statistics Education),介绍如何在统计学课程中使用加拿大统计局的开放经济数据,月度制造业销售额是典型的教学序列。此处数据为说明用途的模拟数据,并非加拿大统计局的数字:一条类似制造业销售额的序列,单位为十亿元,2005 年 1 月至 2023 年 12 月(228 个月),由含两次下滑(2009 年和 2020 年春)的趋势、固定的月度模式和噪声构成。同一条模拟序列用于 STL、SARIMA 和季节子序列三个示例。副标题报告销售额对月份的单因素方差分析(scipy.stats.f_oneway)及 η²,即季节所解释的总变异比例;其余变异大多来自各面板内的长期趋势。2020 年的下滑被突出显示。
代码及图中文字均为英文。
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy import stats
# Simulated data, set in the context of Warin (2024) [warin2024statcan]
# Data: simulated monthly manufacturing sales, billions of dollars, 2005–2023 (shared generator)
rng = np.random.default_rng(2024)
idx = pd.date_range("2005-01-01", "2023-12-01", freq="MS")
t = np.arange(len(idx))
trend = 48 + 0.06 * t - 7 * np.exp(-((t - 54) / 9) ** 2) - 8 * np.exp(-((t - 183) / 2.5) ** 2)
season = np.tile([-3.0, -0.8, 2.6, 0.6, 1.2, 2.2, -3.4, -1.2, 1.6, 2.0, 0.4, -2.2], len(idx) // 12)
sales = pd.Series(trend + season + rng.normal(0, 0.7, len(idx)), index=idx)
# One row per year, one column per month
years = sales.index.year.unique().to_numpy()
months = sales.index[:12].strftime("%b")
values = sales.to_numpy().reshape(len(years), 12)
# Seasonal effect: one-way ANOVA of sales on month, with eta squared
F, p = stats.f_oneway(*values.T)
eta2 = ((values.mean(0) - values.mean()) ** 2).sum() * len(years) / ((values - values.mean()) ** 2).sum()
fig, ax = plt.subplots(figsize=(10, 5.2))
width = 0.8
x_in = (years - years.min()) / (years.max() - years.min()) * width - width / 2
highlight = {2020: list(months)}
# One mini series per month, plus its mean
for i, m in enumerate(months):
v = values[:, i]
if i % 2 == 0:
ax.axvspan(i - 0.5, i + 0.5, color="#F2F2F7", lw=0, zorder=0)
ax.plot(i + x_in, v, color="#007AFF", lw=0.9, alpha=0.7)
ax.hlines(v.mean(), i - width / 2, i + width / 2, color="#FF9500", lw=2.5, zorder=3)
for yr, ms in highlight.items():
if m in ms:
j = np.where(years == yr)[0][0]
ax.scatter(i + x_in[j], v[j], s=26, color="#FF3B30", edgecolor="white", lw=0.8, zorder=4)
ax.set_xticks(range(12), months)
ax.set_xlim(-0.5, 11.5)
ax.grid(axis="y")
ax.set_ylabel("Manufacturing sales (billions of dollars)")
ax.plot([], [], color="#FF9500", lw=2.5, label="Monthly mean")
ax.plot([], [], color="#007AFF", lw=1, label=f"{years.min()}–{years.max()}, left to right")
ax.scatter([], [], s=26, color="#FF3B30", label="2020 (spring slump)")
ax.legend(loc="upper left", ncols=3)
ax.set_ylim(top=values.max() + 6)
lo_m = sales.index[values.mean(0).argmin()].strftime("%B") # first year runs Jan–Dec
hi_m = sales.index[values.mean(0).argmax()].strftime("%B")
p_txt = "p < .001" if p < 0.001 else f"p = {p:.3f}"
ax.set_title(f"Manufacturing sales bottom out in {lo_m} and peak in {hi_m}", pad=26)
ax.text(0, 1.02, f"Monthly, billions of dollars (simulated) · one-way ANOVA F(11, {values.size - 12}) = {F:.1f}, "
f"{p_txt}, η² = {eta2:.2f} · n = {len(years)} years",
transform=ax.transAxes, fontsize=9.5, color="#6E6E73")
plt.show()
图表使用了可选的 PyStatlas 样式:添加 plt.style.use("pystatlas.mplstyle") 即可完全还原其外观。
数据来源
- Simulated data. Simulated data with a fixed random seed, created for this example.
No real respondents, documents or measurements: the numbers only illustrate how the chart and its statistics work. - Warin (2024), Statistics Canada's open economic data for teaching. Warin, T. (2024). Access Statistics Canada's open economic data for statistics and data science courses. Technology Innovations in Statistics Education, 15(1). https://doi.org/10.5070/t5.1868
The example is set in the context of this paper; unless its description says otherwise, the numbers are simulated and are not Statistics Canada's figures.
这些示例用于说明统计方法。除条目另有说明外,其数值均为模拟数据,图表标题描述的是该图在这些模拟数据中呈现的内容——既不是所引论文的结论,也不是作者对实质问题的立场。关于结果、背景和数据,请以此处引用的论文为准。
如何引用
PyStatlas 由 Thierry Warin 创建并维护。如果它对你的工作有所帮助,敬请引用。
Warin, T. (2026). Seasonal subseries plot. In PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences (Version 1.1.0) [Computer software]. https://warin.ca/pystatlas/time-series/seasonal-subseries/
@misc{warin2026statlas,
author = {Warin, Thierry},
title = {PyStatlas: A Python Gallery of Statistical Graphics for the Social Sciences},
year = {2026},
version = {1.1.0},
howpublished = {\url{https://warin.ca/pystatlas/}},
note = {Source code: https://github.com/warint/pystatlas}
}