【问题标题】:How to plot statsmodels timeseries plots side by side and customize x axis in Python如何在 Python 中并排绘制 statsmodels 时间序列图并自定义 x 轴
【发布时间】:2020-10-22 21:45:31
【问题描述】:

我正在创建这些时间序列图,特别是 stl 分解,并且已经设法将所有图合二为一。我遇到的问题是让它们像解决方案here 一样并排显示。我在链接上尝试了解决方案,但它没有用,相反,我一直在顶部得到一个空图。 我有四个时间序列图并设法将它们输出到彼此的底部但是我 想让它们并排或两个并排,最后两个并排在底部。

那么对于 xaxis 上的日期,我已经尝试使用 ax.xaxis.set_major_formatter(DateFormatter('%b %Y')),但它不适用于下面的代码,因为 res.plot 函数不允许它。

我已经到处搜索,但找不到解决问题的方法。我将不胜感激。

数据

      Date     Crime
0   2018-01-01  149
1   2018-01-02  88
2   2018-01-03  86
3   2018-01-04  100
4   2018-01-05  123
... ... ...
664 2019-10-27  142
665 2019-10-28  113
666 2019-10-29  126
667 2019-10-30  120
668 2019-10-31  147

代码

from statsmodels.tsa.seasonal import STL
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.plotting import register_matplotlib_converters 
from matplotlib.dates import DateFormatter

register_matplotlib_converters()
sns.set(style='whitegrid', palette = sns.color_palette('winter'), rc={'axes.titlesize':17,'axes.labelsize':17, 'grid.linewidth': 0.5})
plt.rc("axes.spines", top=False, bottom = False, right=False, left=False)
plt.rc('font', size=13)
plt.rc('figure',figsize=(17,12))

#fig=plt.figure()
#fig, axes = plt.subplots(2, sharex=True)

#fig,(ax,ax2,ax3,ax4) = plt.subplots(1,4,sharey=True)

#fig, ax = plt.subplots()
#fig, axes = plt.subplots(1,3,sharex=True, sharey=True, figsize=(12,5))
#ax.plot([0, 0], [0,1]) 


stl = STL(seatr, seasonal=13)
res = stl.fit()
res.plot()    
plt.title('Seattle', fontsize = 20, pad=670)

stl2 = STL(latr, seasonal=13)
res2 = stl.fit()
res2.plot()  
plt.title('Los Angles', fontsize = 20, pad=670)

stl3 = STL(sftr, seasonal=13)
res3 = stl.fit()
res3.plot()  
plt.title('San Francisco', fontsize = 20, pad=670)

stl4 = STL(phtr, seasonal=13)
res4 = stl.fit()
res4.plot()  
plt.title('Philadelphia', fontsize = 20, pad=670)

#ax.xaxis.set_major_formatter(DateFormatter('%b %Y'))

其中一个情节

整体输出

【问题讨论】:

    标签: python time-series seaborn statsmodels stl-decomposition


    【解决方案1】:

    这是一个使用人工数据的示例。主要思想是将输出分组到DataFrames,然后使用pandas plot 函数绘制它们。

    请注意,我必须将您的代码更改为在拟合时使用 stl2stl3stl4

    from statsmodels.tsa.seasonal import STL
    import matplotlib.pyplot as plt
    import seaborn as sns
    from pandas.plotting import register_matplotlib_converters 
    from matplotlib.dates import DateFormatter
    
    register_matplotlib_converters()
    sns.set(style='whitegrid', palette = sns.color_palette('winter'), rc={'axes.titlesize':17,'axes.labelsize':17, 'grid.linewidth': 0.5})
    plt.rc("axes.spines", top=False, bottom = False, right=False, left=False)
    plt.rc('font', size=13)
    plt.rc('figure',figsize=(17,12))
    
    
    idx = pd.date_range("1-1-2020", periods=200, freq="M")
    seas = 10*np.sin(np.arange(200) * np.pi/12)
    trend = np.arange(200) / 10.0
    seatr = pd.Series(trend + seas + np.random.standard_normal(200), name="Seattle", index=idx)
    latr = pd.Series(trend + seas + np.random.standard_normal(200), name="LA", index=idx)
    sftr = pd.Series(trend + seas + np.random.standard_normal(200), name="SF", index=idx)
    phtr = pd.Series(trend + seas + np.random.standard_normal(200), name="Philly", index=idx)
    
    stl = STL(seatr, seasonal=13)
    res = stl.fit()
    
    stl2 = STL(latr, seasonal=13)
    res2 = stl2.fit()
    
    stl3 = STL(sftr, seasonal=13)
    res3 = stl3.fit()
    
    stl4 = STL(phtr, seasonal=13)
    res4 = stl4.fit()
    
    data = pd.concat([seatr, latr, sftr, phtr], 1)
    trends = pd.concat([res.trend, res2.trend, res3.trend, res4.trend], 1)
    seasonals = pd.concat([res.seasonal, res2.seasonal, res3.seasonal, res4.seasonal], 1)
    resids = pd.concat([res.resid, res2.resid, res3.resid, res4.resid], 1)
    
    fig, axes = plt.subplots(4,1)
    data.plot(ax=axes[0])
    trends.plot(ax=axes[1])
    seasonals.plot(ax=axes[2])
    resids.plot(ax=axes[3])
    

    这会产生:

    【讨论】:

    • 感谢您的回答,有没有办法只使用我已经拥有的地块,但只是将它们并排而不是放在一个地块中?
    • 如果您希望它们并排,那么您可以将每列绘制在自己的轴上,例如for i, col in trends: trends[col].plot(ax=axes[i])。我不完全清楚你期望这个数字是什么样子。你想要一个 4 乘 1 吗?还是 16 x 1 和 4 组?
    • 谢谢,4 x 1,其中每个 stl 分解图并排显示在一行中。就像这里链接中的解决方案:stackoverflow.com/questions/45184055/… 我尝试遵循解决方案但无法获得相同的结果,所以我想要一个更简单的版本。
    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 2018-12-01
    • 2020-09-18
    • 2019-09-23
    • 1970-01-01
    • 2016-06-24
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多