简体   繁体   English

GroupBy 和 Pandas 绘图

[英]GroupBy and plot with pandas

I have data.我有数据。 There are some groups of people who participate in the meetings.有一些人参加了会议。 Meetings are divided into speeches.会议分为演讲。 Each meeting and speeches combination has a number of participants.每个会议和演讲组合都有多个参与者。 The number of participants in one meeting does not change.一场会议的参加人数不会改变。 In other words, the number of participants only changes from meeting to meeting.换句话说,参与者的数量只会随着会议的不同而变化。

data = [
 ['group_1', 1, 1, 68],
 ['group_2', 1, 1, 35],
 ['group_1', 1, 2, 68],
 ['group_2', 1, 2, 35],
 ['group_1', 2, 1, 78],
 ['group_2', 2, 1, 25],
 ['group_1', 2, 2, 78], 
 ['group_2', 2, 2, 25],
 ['group_1', 3, 1, 73], 
 ['group_2', 3, 1, 30],
 ['group_1', 3, 2, 73], 
 ['group_2', 3, 2, 30]]
df = pd.DataFrame(data, columns=['group_name', 'meeting', 'present', 'members'])

X is meeting, y is number of participants. X 是会议,y 是参与者的数量。 I want to plot something like this.我想绘制这样的东西。

df.groupby(['group_name']).plot(
         x='meeting', y='members',
         color='#4b0082', linewidth=3,
         marker='h', markerfacecolor='lightgreen', markeredgewidth=1, markersize=9, markevery=1);

在此处输入图片说明

However, I would like to add a title as a group name and sign the y-axis.但是,我想添加一个标题作为组名并在 y 轴上签名。 and I also have a problem when I run this code on all data, for some reason I have extra points on the plot.当我在所有数据上运行此代码时,我也遇到了问题,出于某种原因,我在图中有额外的点。 在此处输入图片说明

On the first graph the count should start from meeting 27 and there is an anomaly in meeting area 40. On the second graph there are anomalies in a 27 meeting area.在第一张图中,计数应从第 27 次会议开始,第 40 次会议区域出现异常。在第二张图中,第 27 次会议区域出现异常。

Since pandas >= 1.1.0 we have the ylabel argument in DataFrame.plot .由于pandas >= 1.1.0我们有ylabel的说法DataFrame.plot Also we will rewrite your groupby a bit so we can access the group name:此外,我们将稍微重写您的 groupby,以便我们可以访问组名:

for grp, d in df.groupby('group_name'):
    d.plot(
        x='meeting',
        y='members',
        color='#4b0082',
        ylabel='members',
        title=grp,
        linewidth=3,
        marker='h',
        markerfacecolor='lightgreen',
        markeredgewidth=1,
        markersize=9,
        markevery=1
    )

地块

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

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