简体   繁体   English

熊猫 - 多线图

[英]Pandas - Multiline plot

My data frame is composed by 3 elements: Name, Date and Weight. 我的数据框由3个元素组成:名称,日期和重量。

I would like to plot a line graph where the X axis is the date, the Y is the COUNT OF weights (how many times a give subject weighted himself throughout the day) and each line is a different name. 我想绘制一个折线图,其中X轴是日期,Y是COUNT OF权重(给定主题在一天中加权的次数),每一行是不同的名称。

In order to do that, i gave it a shot: 为了做到这一点,我试了一下:

dtFrame.sort_values(['date'])

dtFrame.groupby(by=["date","name"])
dtFrame.plot.line()
plt.show()

but that was not what I expected... basically what i want is: Select name, date, count(1) FROM myTable Groupby name, date 但这不是我的预期...基本上我想要的是: Select name, date, count(1) FROM myTable Groupby name, date

What am i doing wrong? 我究竟做错了什么?

Edit1: Sample csv data Edit1:示例csv数据

day, name, weight 一天,名字,重量
1,a,5 1,A,5
1,a,5 1,A,5
1,a,7 1,A,7
1,b,5 1,B,5
1,b,5 1,B,5
2,a,5 2,A,5
2,a,5 2,A,5
2,a,7 2,A,7
2,b,5 2,B,5
2,b,5 2,B,5
3,a,5 3,,5
3,a,5 3,,5
3,a,7 3,,7
3,b,5 3,B,5
3,b,5 3,B,5
4,a,5 4,,5
4,a,5 4,,5
4,a,7 4,,7
4,b,5 4,B,5
4,b,5 4,B,5

One way is to just loop through the names and plot each individually 一种方法是循环遍历名称并单独绘制每个名称

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(12, 18))

for name, group in data.groupby('name'):
    group.date.value_counts().plot(ax=ax, label=name)

plt.legend()
plt.show()
res = df.groupby(['name','day']).count().unstack().T
res.index = res.index.droplevel()
res.plot()

在此输入图像描述

i would do it like that 我会这样做的

df.groupby(['day', 'name']).weight.count().unstack().plot()

由此产生的情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM