首页 > 解决方案 > 如何更改 Statsmodel 分解图中折线图的颜色

问题描述

季节性分解图的默认颜色是浅蓝色。1. 如何改变颜色,让每条线都是不同的颜色?2.如果每个情节不能有单独的颜色,我将如何将所有颜色更改为红色?

我尝试向分解.plot(color = 'red') 添加参数并在文档中搜索线索。

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()

分解图,其中每张图都是不同的颜色。 在此处输入图像描述

标签: pythonplotstatsmodels

解决方案


您发布的代码中的decomposition对象在绘图方法中使用熊猫。我没有看到将颜色直接传递给方法的plot方法,并且不需要 **kwargs。

一种解决方法是直接在对象上调用 pandas 绘图代码:

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')

在此处输入图像描述


推荐阅读