Python Matplotlib timeseries plot,groupby函数应用于df
目前我有八个不同扬声器的df:Python Matplotlib timeseries plot,groupby函数应用于df,python,pandas,matplotlib,group-by,time-series,Python,Pandas,Matplotlib,Group By,Time Series,目前我有八个不同扬声器的df: raw_score Speaker date Allison 2012-10-31 0.796908 2012-11-30 1.792649 2012-12-31 0.668619 Warsh 2015-03-31 NaN 2015-04-30 NaN
raw_score
Speaker date
Allison 2012-10-31 0.796908
2012-11-30 1.792649
2012-12-31 0.668619
Warsh 2015-03-31 NaN
2015-04-30 NaN
2015-05-31 -0.094364
2015-06-30 0.349691
我使用此代码创建了我当前的df:
dropped = df.drop(['Unnamed: 0', 'text'], axis=1)
dropped['date'] = pd.to_datetime(dropped['date'], format='%m/%d/%y')
dropped.index = dropped['date']
del dropped['date']
dropped = dropped.groupby('Speaker')
dropped = dropped.resample('M').mean()
我想制作一个时间序列图,y轴上有“原始分数”,x轴上有日期(目前是df的索引),每个发言者在图上有一条单独的线
我尝试了以下方法,但没有得到我想要的:
dropped.plot()
试试这个:
In [47]: df.reset_index(level=0).pivot_table(index='date', columns='Speaker', values='raw_score')
Out[47]:
Speaker Allison Warsh
date
2012-10-31 0.796908 NaN
2012-11-30 1.792649 NaN
2012-12-31 0.668619 NaN
2015-03-31 NaN NaN
2015-04-30 NaN NaN
2015-05-31 NaN -0.094364
2015-06-30 NaN 0.349691
In [48]: df.reset_index(level=0).pivot_table(index='date', columns='Speaker', values='raw_score').plot()
Out[48]: <matplotlib.axes._subplots.AxesSubplot at 0xc804710>
[47]中的:df.reset_index(level=0).pivot_表(index='date',columns='Speaker',values='raw_score')
出[47]:
议长埃里森·沃什
日期
2012-10-31 0.796908南
2012-11-30 1.792649南
2012-12-31 0.668619南
2015-03-31楠楠
2015-04-30楠楠
2015-05-31南-0.094364
2015-06-30南0.349691
在[48]中:df.reset_index(level=0).pivot_表(index='date',columns='Speaker',values='raw_score').plot()
出[48]:
试试这个:
In [47]: df.reset_index(level=0).pivot_table(index='date', columns='Speaker', values='raw_score')
Out[47]:
Speaker Allison Warsh
date
2012-10-31 0.796908 NaN
2012-11-30 1.792649 NaN
2012-12-31 0.668619 NaN
2015-03-31 NaN NaN
2015-04-30 NaN NaN
2015-05-31 NaN -0.094364
2015-06-30 NaN 0.349691
In [48]: df.reset_index(level=0).pivot_table(index='date', columns='Speaker', values='raw_score').plot()
Out[48]: <matplotlib.axes._subplots.AxesSubplot at 0xc804710>
[47]中的:df.reset_index(level=0).pivot_表(index='date',columns='Speaker',values='raw_score')
出[47]:
议长埃里森·沃什
日期
2012-10-31 0.796908南
2012-11-30 1.792649南
2012-12-31 0.668619南
2015-03-31楠楠
2015-04-30楠楠
2015-05-31南-0.094364
2015-06-30南0.349691
在[48]中:df.reset_index(level=0).pivot_表(index='date',columns='Speaker',values='raw_score').plot()
出[48]:
输出:
输出:
谢谢!我编辑了我的问题,以包含代码生成的图形!有没有办法添加平滑、移动图例和添加零线?谢谢我喜欢这个答案,并想接受它。只是想先让图表看起来更漂亮一点:p@GrahamStreich,我想-他不是吗?;-)谢谢我编辑了我的问题,以包含代码生成的图形!有没有办法添加平滑、移动图例和添加零线?谢谢我喜欢这个答案,并想接受它。只是想先让图表看起来更漂亮一点:p@GrahamStreich,我想-他不是吗?;-)@Gramstreich您希望应用哪种平滑?ax=df.rolling('360D').mean().plot()如何将此
ax=df.rolling('360D').mean().plot()合并到图形中?谢谢@Gramstreich您希望应用哪种平滑?ax=df.rolling('360D').mean().plot()如何将此ax=df.rolling('360D').mean().plot()合并到图形中?谢谢