Pandas:按时间分组,然后绘制每组密度

Pandas: Group by Time, then plot density per group

我正在尝试按时间对数据帧进行分组,然后绘制每个子组的变量密度。我在做:

myDf.groupby(pd.TimeGrouper('AS'))['myVar'].plot(kind='density')

结果如下:

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

和情节:

如何向图中添加正确的图例?现在无法知道哪一行对应哪一年。

legend 参数设置为 True:

myDf.groupby(pd.TimeGrouper('AS'))['myVar'].plot(kind='density', legend=True)