使用matplotlib或seaborn在许多子图中绘制移动平均值

时间:2020-06-07 01:16:02

标签: python matplotlib plot seaborn

我正在使用以下代码为美国所有州生成COVID-19案件时间序列。我想在每个时间序列/子图中添加7天和14天移动平均线。

下面是一些代码:

#calculate daily change in covid cases per state with each state as a column in a time series
us_daily_change = united_states.diff()


#calculate the 7 and 14 day moving average per state
short_window = 7
long_window = 14
us_rolling_short_mean = us_daily_change.rolling(short_window).mean()
us_rolling_long_mean = us_daily_change.rolling(long_window).mean()

#this works to plot each state's cases but I can't incorporate the moving averages
us_daily_change.plot(subplots=True, layout=(6,9), figsize=(50,25))


#here's what i have tried. doesn't come close.
for i,j,k in zip(us_daily_change, us_rolling_short_mean, us_rolling_long_mean):
    plt.plot([us_daily_change[i], us_rolling_short_mean[j], us_rolling_long_mean[k]])
    plt.show()
    plt.close()

0 个答案:

没有答案