Matplotlib图在一个奇怪的mann中显示聚合函数

2024-06-16 12:51:57 发布

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

我在尝试用Matplotlib显示数据帧中的数据时遇到了以下问题。这个想法是建立一个线性图,其中Y轴是每个玩家得分的平均值,X轴是执行的投篮次数。我已经对数据帧中的数据应用了聚合函数,但是结果图看起来不像我预期的那样。 以下是我目前所做的:

数据帧

    Score      Gamer       Shots
a    5.0       gamer1        7   
b    3.0       gamer2        2  
c    2.5       gamer1        8   
d    7.1       gamer3        9
e    1.8       gamer3        2
f    2.2       gamer3        1

情节

^{pr2}$

enter image description here


Tags: 数据函数matplotlib玩家线性次数平均值score
1条回答
网友
1楼 · 发布于 2024-06-16 12:51:57

IIUC,你需要这样的东西:

In [52]: df.groupby('Gamer').agg({'Score':'mean','Shots':'count'}).plot()
Out[52]: <matplotlib.axes._subplots.AxesSubplot at 0xb41e710>

enter image description here

对应数据:

^{pr2}$

更新:

I need just a single line plot for displaying the dependency of mean score of a gamer (Y-axis) on the number of shots(X-axis)

In [90]: df.groupby('Gamer').agg({'Score':'mean','Shots':'count'}).set_index('Shots').plot()
Out[90]: <matplotlib.axes._subplots.AxesSubplot at 0xbe749b0>

enter image description here

更新2:

In [155]: g = df.groupby('Gamer').agg({'Score':'mean','Shots':'count'}).sort_values('Shots')

In [156]: x,y = g['Shots'], g['Score']

In [157]: plt.plot(x, y)
Out[157]: [<matplotlib.lines.Line2D at 0xbdbf668>]

enter image description here

相关问题 更多 >