时间序列季节性分解中的小刻度轴和大刻度轴 matplotlib 格式 [英] Minor tick axis and major tick axis matplotlib formatting in Time series seasonal decomposition

查看:122
本文介绍了时间序列季节性分解中的小刻度轴和大刻度轴 matplotlib 格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

df

Date        Col1   Col2   Col3
2016-11-1    12     13     14
2016-10-1    2      3      1
2016-03-01   2      1      1 
and so on

用于分解时间序列以获得季节性、趋势、观察值和残差值的代码:

Code to decompose time series to get seasonality, trends, observed and residual values:

from statsmodels.tsa.seasonal import seasonal_decompose
from matplotlib import dates as mdates
years = mdates.YearLocator()    # only print label for the years
months = mdates.MonthLocator()  # mark months as ticks
years_fmt = mdates.DateFormatter('%Y')
fmt = mdates.DateFormatter('%b')

df = df.set_index('Date')


s_dec_multiplicative = seasonal_decompose(df['Col1'], model = "multiplicative")
s_dec_multiplicative.plot()

s_dec_multiplicative.xaxis.set_major_locator(years)
s_dec_multiplicative.xaxis.set_minor_locator(months)
s_dec_multiplicative.xaxis.set_major_formatter(years_fmt)
s_dec_multiplicative.xaxis.set_minor_formatter(fmt)
plt.show()

问题:我想要所有月份的JAN,FEB,MAR等的股票行情自动收录器.应提及年份,如 2016、2017 等,月份之间应有小刻度.

Problem: I want tickers for JAN,FEB, MAR etc like that for all months. Years should be mentioned like 2016, 2017 and so on and months should be in between with small ticks.

错误:

---> 12 s_dec_multiplicative.xaxis.set_major_locator(years)
AttributeError: 'DecomposeResult' object has no attribute 'xaxis'

推荐答案

您的问题是您正在尝试更改 DecomposeResult 对象的属性,而您应该在 ax 上工作 对象.

Your problem is that you're trying to change attribute of DecomposeResult object whereas you're supposed to work on ax object.

让我们检索一些玩具时间序列数据:

Let's retrieve some toy time series data:

from pandas_datareader import data
goog = data.DataReader("GOOG", "yahoo")["Adj Close"]
goog.plot();

现在让我们进行所需的分解并将结果放入Pandas' df:

Now let's do the desired decomposition and put the results into Pandas' df:

from statsmodels.tsa.seasonal import seasonal_decompose
s_dec_multiplicative = seasonal_decompose(goog, model = "multiplicative", freq=12)

observed = s_dec_multiplicative.observed
seasonal = s_dec_multiplicative.seasonal
residual = s_dec_multiplicative.resid

df = pd.DataFrame({"observed":observed, "seasonal":seasonal,"residual":residual}

最后,我们准备绘制:

from matplotlib import dates as mdates
years = mdates.YearLocator()    # only print label for the years
months = mdates.MonthLocator()  # mark months as ticks
years_fmt = mdates.DateFormatter('%Y-%b')
fmt = mdates.DateFormatter('%b')

_, axes = plt.subplots(nrows=3,ncols=1, figsize=(20, 10))
for i, ax in enumerate(axes):
    ax = df.iloc[:,i].plot(ax=ax)
    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(years_fmt)
    ax.xaxis.set_minor_locator(months)
    ax.xaxis.set_minor_formatter(fmt)
    ax.set_ylabel(df.iloc[:,i].name)
    plt.setp(ax.xaxis.get_minorticklabels(), rotation=90)
    plt.setp(ax.xaxis.get_majorticklabels(), rotation=90)

这篇关于时间序列季节性分解中的小刻度轴和大刻度轴 matplotlib 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆