如何随着时间的推移按类别绘制 [英] How to plot by category over time

查看:77
本文介绍了如何随着时间的推移按类别绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绘制两列,类别和年份.我试图将每年每个类别的总和创建一个多类时间序列图.

I have two columns, categorical and year, that I am trying to plot. I am trying to take the sum total of each categorical per year to create a multi-class time series plot.

ax = data[data.categorical=="cat1"]["categorical"].plot(label='cat1')
data[data.categorical=="cat2"]["categorical"].plot(ax=ax, label='cat3')
data[data.categorical=="cat3"]["categorical"].plot(ax=ax, label='cat3')
plt.xlabel("Year")
plt.ylabel("Number per category")
sns.despine()

但是出现错误,指出没有要绘制的数字数据.我正在寻找与上述类似的东西,也许使用data[data.categorical=="cat3"]["categorical"].lambda x : (1 for x in data.categorical)

But am getting an error stating no numeric data to plot. I am looking for something similar to the above, perhaps with data[data.categorical=="cat3"]["categorical"].lambda x : (1 for x in data.categorical)

我将使用以下列表作为示例.

I will use the following lists as examples.

categorical = ["cat1","cat1","cat2","cat3","cat2","cat1","cat3","cat2","cat1","cat3","cat3","cat3","cat2","cat1","cat2","cat3","cat2","cat2","cat3","cat1","cat1","cat1","cat3"]

year = [2013,2014,2013,2015,2014,2014,2013,2014,2014,2015,2015,2013,2014,2014,2013,2014,2015,2015,2015,2013,2014,2015,2013]

我的目标是获得类似于下图的内容

My goal is to obtain something similar to the following picture

推荐答案

我不愿意将其称为解决方案",因为它基本上只是对Pandas基本功能的总结,在您发现该文档的同一文档中对此进行了解释.您在帖子中放置的时间序列图.但是,看到groupby和绘图存在一些混淆,演示可能会帮助您清除问题.

I'm hesitant to call this a "solution", as it's basically just a summary of basic Pandas functionality, which is explained in the same documentation where you found the time series plot you've placed in your post. But seeing as there's some confusion around groupby and plotting, a demo may help clear things up.

我们可以使用两个呼叫groupby().
使用count汇总,第一个groupby()每年获取类别外观的计数.
第二个groupby()用于绘制每个类别的时间序列.

We can use two calls to groupby().
The first groupby() gets a count of category appearances per year, using the count aggregation.
The second groupby() is used to plot the time series for each category.

首先,生成一个示例数据帧:

To start, generate a sample data frame:

import pandas as pd
categorical = ["cat1","cat1","cat2","cat3","cat2","cat1","cat3","cat2",
               "cat1","cat3","cat3","cat3","cat2","cat1","cat2","cat3",
               "cat2","cat2","cat3","cat1","cat1","cat1","cat3"]
year = [2013,2014,2013,2015,2014,2014,2013,2014,2014,2015,2015,2013,
        2014,2014,2013,2014,2015,2015,2015,2013,2014,2015,2013]
df = pd.DataFrame({'categorical':categorical,
                   'year':year})

   categorical  year
 0        cat1  2013
 1        cat1  2014
                 ...
21        cat1  2015
22        cat3  2013

现在每年获得每个类别的计数:

Now get counts per category, per year:

# reset_index() gives a column for counting, after groupby uses year and category
ctdf = (df.reset_index()
          .groupby(['year','categorical'], as_index=False)
          .count()
          # rename isn't strictly necessary here, it's just for readability
          .rename(columns={'index':'ct'})
       )

   year categorical  ct
0  2013        cat1   2
1  2013        cat2   2
2  2013        cat3   3
3  2014        cat1   5
4  2014        cat2   3
5  2014        cat3   1
6  2015        cat1   1
7  2015        cat2   2
8  2015        cat3   4

最后,按颜色绘制每个类别的时间序列:

Finally, plot time series for each category, keyed by color:

from matplotlib import pyplot as plt
fig, ax = plt.subplots()

# key gives the group name (i.e. category), data gives the actual values
for key, data in ctdf.groupby('categorical'):
    data.plot(x='year', y='ct', ax=ax, label=key)

这篇关于如何随着时间的推移按类别绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆