简体   繁体   English

在 Pandas / Matplotlib 中使用 GroupBy 函数绘制多条线

[英]Plotting Multiple Lines using GroupBy Function in Pandas / Matplotlib

I'm trying to plot a pandas dataframe using matplotlib however having issues with the grouping.我正在尝试使用 matplotlib 绘制 pandas 数据框,但是分组存在问题。 The dataframe contains statistics for a player in each round of the season.数据框包含本赛季每一轮球员的统计数据。 My dataframe is much larger however for this example I have simplified it:我的数据框要大得多,但是对于此示例,我已对其进行了简化:

Desc    Round 1 Round 2 Round 3 Round 4 Round 5 Round 6 Round 7 Round 8 Round 9 Round 10
Ben     22.3    33.3    21.5    27.7    31.3    43      33.5    20      29.7    22.7
Tom             28.2    29.2    23.1    25      21.4    22.3    26.2    25.3    19.6
Jack    21.3    30.4    20.8    18      24.5    28.3    32.6    17      25.1    23.7

However when I simply try to plot this using:但是,当我只是尝试使用以下方法进行绘制时:

df.plot()
plt.show()

The lines are grouped by the round number instead of the player's name and it appears the Y values are actually the player's row index.这些行是按轮数而不是玩家姓名分组的,看起来 Y 值实际上是玩家的行索引。 Here is the plot it outputs.这是它输出的图。

Matplotlib 线图输出

So I believe maybe the pandas dataframe isn't corrected indexed for rows / columns thus causing this problem.所以我相信也许熊猫数据框没有为行/列更正索引,从而导致这个问题。 I've looked into using the df.groupby but can't find a solution.我已经研究过使用 df.groupby 但找不到解决方案。

I can easily create the line graph I'm after using MS Excel - Here is the output I would like:使用 MS Excel 后,我可以轻松创建折线图 - 这是我想要的输出:

MS Excel 线图输出

Does anyone have a solution on what I can do to either my dataframe or plot code to get the desired outcome?有没有人可以解决我可以对我的数据框或绘图代码做些什么以获得预期的结果? I have already made sure I have set the row Index's to the players name using:我已经确保我已使用以下方法将行索引设置为玩家名称:

df.set_index('Desc')

However this hasn't fixed the issue.但是,这并没有解决问题。

Use set_index then transpose:使用 set_index 然后转置:

Creating data创建数据

colNames = ['Desc', 'Round1', 'Round2', 'Round3', 'Round4', 'Round5', 'Round6', 'Round7', 'Round8', 'Round9', 'Round10']
df = pd.DataFrame(columns = colNames)
df.loc[len(df)] = ['Ben', '22.3', '33.3', '21.5', '27.7', '31.3', '43', '33.5', '20', '29.7', '22.7']
df.loc[len(df)] = ['Tom', '', '28.2', '29.2', '23.1', '25', '21.4', '22.3', '26.2', '25.3', '19.6']
df.loc[len(df)] = ['Jack', '21.3', '30.4', '20.8', '18', '24.5', '28.3', '32.6', '17', '25.1', '23.7']

Pre-processing预处理

df.set_index("Desc", inplace = True)
df = df.apply(pd.to_numeric, errors='coerce')

Plotting the data绘制数据

df.T.plot()
plt.show()

This gives us expected graph :这给了我们预期的图表:

在此处输入图像描述

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

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