《Pandas传说》时间序列情节只显示“无”

2024-04-24 08:13:06 发布

您现在位置:Python中文网/ 问答频道 /正文

data是一个pandas数据帧,在具有多个属性的条目上具有日期-时间索引。其中一个属性称为STATUS。我试图创建一个按STATUS属性划分的每天条目数的图。在

我第一次尝试使用熊猫.plot公司名称:

for status in data["STATUS"].unique():
    entries = data[data["STATUS"] == status]
    entries.groupby(pandas.TimeGrouper("D")).size().plot(figsize=(16,4), legend=True)

结果是:

enter image description here

我应该如何修改上面的代码,以便图例显示曲线所属的状态?在

另外,可以自由地建议一种不同的方法来实现这种可视化(按时间间隔对时间序列分组,对条目计数,并按条目的属性进行分解)。在


Tags: 数据in名称pandasfordata属性plot
1条回答
网友
1楼 · 发布于 2024-04-24 08:13:06

我相信通过以下代码更改,您将得到您想要的:

fig, ax = plt.subplots()    
for status in data["STATUS"].unique():
        entries = data[data["STATUS"] == status]
        dfPlot = pandas.DataFrame(entries.groupby(pandas.TimeGrouper("D")).size())
        dfPlot.columns=[status]
        dfPlot.plot(ax=ax, figsize=(16,4), legend=True)

结果是size函数的输出提供了一个Series类型,在其列中没有名称。因此,从Series创建一个Dataframe并更改列名就可以做到了。在

相关问题 更多 >