Search code examples
pythonmatplotlibstatsmodels

Why this command cannot change figure size?


Let's consider series

series = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

On which I want to do time series decomposition. Let's import package for this decomposition and for plotting:

from statsmodels.tsa.seasonal import seasonal_decompose
import matplotlib.pyplot as plt

Now by using seasonal decomposition and plotting it we obtain very small plot. I tried to lengthen it with using standard command figsize but it's not working. Do you know how can I extend my plot in this case?

result = seasonal_decompose(series, model='additive', period=1)
result.plot()
plt.figure(figsize = (15, 8))    #This command is completly ignored
plt.show()

Solution

  • DecomposeResult.plot returns the figure instance it uses to plot. You can modify this instance before calling plt.show() as follows:

    result = seasonal_decompose(series, model='additive', period=1)
    fig = result.plot()
    fig.set_size_inches(15, 8)
    plt.show()
    

    This results in a nice figure 15 inches wide, and 8 inches high:

    Seas. decomp.