Search code examples
pythonplotstatsmodels

How to Change Color of Line graphs in Statsmodel Decomposition Plots


The default color of the seasonal decomposition graphs is a light blue. 1. How can you change the colors so that each of the lines is a different color? 2. If each plot can not have a separate color, how would I change all of the colors to say red?

I've tried adding arguments to decomposition.plot(color = 'red') and searching the documentation for clues.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# I want 7 days of 24 hours with 60 minutes each
periods = 7 * 24 * 60
tidx = pd.date_range('2016-07-01', periods=periods, freq='D')
np.random.seed([3,1415])

# This will pick a number of normally distributed random numbers
# where the number is specified by periods
data = np.random.randn(periods)

ts = pd.Series(data=data, index=tidx, name='TimeSeries')

decomposition = sm.tsa.seasonal_decompose(ts, model ='additive')
fig = decomposition.plot()
plt.show()

A decomposition plot in which each graph is a different color. enter image description here


Solution

  • The decomposition object in the code you posted uses pandas in the plotting method. I don't see a way of passing colors directly to the plot method, and it doesn't take **kwargs.

    A work around would be to call the pandas plotting code directly on the object:

    fig, axes = plt.subplots(4, 1, sharex=True)
    
    decomposition.observed.plot(ax=axes[0], legend=False, color='r')
    axes[0].set_ylabel('Observed')
    decomposition.trend.plot(ax=axes[1], legend=False, color='g')
    axes[1].set_ylabel('Trend')
    decomposition.seasonal.plot(ax=axes[2], legend=False)
    axes[2].set_ylabel('Seasonal')
    decomposition.resid.plot(ax=axes[3], legend=False, color='k')
    axes[3].set_ylabel('Residual')
    

    enter image description here