时间序列分析:Python 中的季节性图和周期图
from pathlib import Path
from warnings import simplefilter
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.linear_model import LinearRegression
from statsmodels.tsa.deterministic import CalendarFourier, DeterministicProcess
simplefilter('ignore')
# Set Matplotlib defaults
plt.style.use('seaborn-whitegrid')
plt.rc('figure', autolayout=True, figsize=(11, 5))
plt.rc(
'axes',
labelweight='bold',
labelsize='large',
titleweight='bold',
titlesize=16,
titlepad=10,
)
plot_params = dict(
color='0.75',
style='.-',
markeredgecolor='0.25',
markerfacecolor='0.25',
legend=False,
)
%config InlineBackend.figure_format = 'retina'
# annotations: https://stackoverflow.com/a/49238256/5769929
def seasonal_plot(X, y, period, freq, ax=None):
if ax is None:
_, ax = plt.subplots()
palette = sns.color_palette('husl', n_colors=X[period].nunique(),)
ax = sns.lineplot(
x=freq,
y=y,
hue=period,
data=X,
ci=False,
ax=ax,
palette=palette,
legend=False,
)
ax.set_title(f'Seasonal Plot ({period}/{freq})')
for line, name in zip(ax.lines, X[period].unique()):
y_ = line.get_ydata()[-1]
ax.annotate(
name,
xy=(1, y_),
xytext=(6, 0),
color=line.get_color(),
xycoords=ax.get_yaxis_transform(),
textcoords='offset points',
size=14,
va='center',
)
return ax
def plot_periodogram(ts, detrend='linear', ax=None):
from scipy.signal import periodogram
fs = pd.Timedelta('1Y') / pd.Timedelta('1D')
freqencies, spectrum = periodogram(
ts,
fs=fs,
detrend=detrend,
window='boxcar',
scaling='spectrum',
)
if ax is None:
_, ax = plt.subplots()
ax.step(freqencies, spectrum, color='purple')
ax.set_xscale('log')
ax.set_xticks([1, 2, 4, 6, 12, 26, 52, 104])
ax.set_xticklabels(
[
'Annual (1)',
'Semiannual (2)',
'Quarterly (4)',
'Bimonthly (6)',
'Monthly (12)',
'Biweekly (26)',
'Weekly (52)',
'Semiweekly (104)',
],
rotation=30,
)
ax.ticklabel_format(axis='y', style='sci', scilimits=(0, 0))
ax.set_ylabel('Variance')
ax.set_title('Periodogram')
return ax
data_dir = Path('../input/ts-course-data')
tunnel = pd.read_csv(data_dir / 'tunnel.csv', parse_dates=['Day'])
tunnel = tunnel.set_index('Day').to_period('D')
# `plt.rc('figure', autolayout=True, figsize=(11, 5))` 代码的意思是:
* **`plt.rc('figure', autolayout=True)`**: 设置 Matplotlib 图形的自动布局为 True,表示自动调整子图的大小和位置,使其适应图形区域。
* **`plt.rc('figure', figsize=(11, 5))`**: 设置 Matplotlib 图形的默认尺寸为 11 英寸宽,5 英寸高。
此外,`plt.rc('axes', ...)` 设置坐标轴的默认参数,包括标签加粗、字号为大号、标题加粗、字号为 16、标题与子图之间距离为 10。
原文地址: https://www.cveoy.top/t/topic/nUyG 著作权归作者所有。请勿转载和采集!