我有以下数据框:
print(df)
dt_op Product Quantity
2018-12-15 3 10
2018-12-15 2 8
2018-12-16 3 5
(...)
我想绘制一个季节性分解图,其中多个产品表示为不同颜色的线,使用:
from pylab import rcParams
import statsmodels.api as sm
import matplotlib.pyplot as plt
y = df.set_index(["dt_op"])['Quantity'].resample('W').sum()
rcParams['figure.figsize'] = 11, 9
decomposition = sm.tsa.seasonal_decompose(y, model='additive')
fig = decomposition.plot()
plt.show()
我尝试过:
codes = np.unique(df.cod_id)
for i in codes:
y = df.loc[(df.cod_id == i)].set_index(["dt_op"])['Quantity'].resample('W').sum()
print(i)
rcParams['figure.figsize'] = 11, 9
decomposition = sm.tsa.seasonal_decompose(y, model='additive')
fig = decomposition.plot()
plt.show()
但是它不会不生成单个图,而是针对n个产品生成n个图。 如何创建具有多个折线并在公共日期x轴上的单个图表?