Home > Software engineering >  Statsmodels.api.tsa.seasonal_decompose plot figsize
Statsmodels.api.tsa.seasonal_decompose plot figsize

Time:12-17

I am using statsmodels.api.tsa.seasonal_decompose to do some seasonal analysis of a time series.

I set it up using

decomp_viz = sm.tsa.seasonal_decompose(df_ts['NetConsumption'], period=48*180)

and then try and visualise it using

decomp_viz.plot()

The output was tiny so I tried to use the standard matplotlib command of

decomp_viz.plot(figsize=(20,20))

However, this got the warning:

TypeError: plot() got an unexpected keyword argument 'figsize'

The documentation says that a matplotlib.figure.Figure is returned from DecomposeResult.plot so I am unsure as to why this error is happening.

My version of statsmodels is 0.13.1 and I am aware that the documentation is for 0.14.0, but conda says that that version does not exist and that I cannot update to it.

Any thoughts would be appreciated.

CodePudding user response:

DecomposeResult.plot doesn't pass keyword arguments. You can change the figure size after you create it:

import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt

PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y  = g.standard_normal(y.shape)

decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
fig = decomp_viz.plot()
fig.set_size_inches((16, 9))
# Tight layout to realign things
fig.tight_layout()
plt.show()

Decompose plot with size 16, 9

Alternatively, you can do the same by altering the MPL rc.

import statsmodels.api as sm
import numpy as np
import matplotlib.pyplot as plt
# Change default figsize
plt.rc("figure",figsize=(20,20))

PERIOD = 48*180
g = np.random.default_rng(20211225)
y = np.cos(2 * np.pi * np.linspace(0, 10.0, 10*PERIOD))
y  = g.standard_normal(y.shape)

decomp_viz = sm.tsa.seasonal_decompose(y, period=PERIOD)
decomp_viz.plot()
plt.show()

which produces (cropped as too big for my screen)

Decompose result plot with size 20, 20

  • Related