Foo*_*Bar 2 python matplotlib pandas
我试图按时间对数据帧进行分组,然后绘制每个子组的变量密度。我正在做:
myDf.groupby(pd.TimeGrouper('AS'))['myVar'].plot(kind='density')
Run Code Online (Sandbox Code Playgroud)
结果如下:
date
2006-01-01 Axes(0.125,0.1;0.775x0.8)
2007-01-01 Axes(0.125,0.1;0.775x0.8)
2008-01-01 Axes(0.125,0.1;0.775x0.8)
2009-01-01 Axes(0.125,0.1;0.775x0.8)
2010-01-01 Axes(0.125,0.1;0.775x0.8)
Name: myVar, dtype: object
Run Code Online (Sandbox Code Playgroud)
和情节:

如何将正确的图例添加到图中?现在无法知道哪一行对应哪一年。
将legend参数设置为True:
myDf.groupby(pd.TimeGrouper('AS'))['myVar'].plot(kind='density', legend=True)
Run Code Online (Sandbox Code Playgroud)