Python Matplotlib图形以奇怪的方式显示聚合函数
在尝试使用Matplotlib显示数据帧中的数据时,我遇到了以下问题。这个想法是建立一个线性图,其中Y轴是每个玩家得分的平均值,X轴是执行的射击次数。我已经对数据帧中的数据应用了聚合函数,但生成的图看起来并不像我预期的那样。 以下是我迄今为止所做的工作: 数据帧Python Matplotlib图形以奇怪的方式显示聚合函数,python,pandas,matplotlib,dataframe,Python,Pandas,Matplotlib,Dataframe,在尝试使用Matplotlib显示数据帧中的数据时,我遇到了以下问题。这个想法是建立一个线性图,其中Y轴是每个玩家得分的平均值,X轴是执行的射击次数。我已经对数据帧中的数据应用了聚合函数,但生成的图看起来并不像我预期的那样。 以下是我迄今为止所做的工作: 数据帧 Score Gamer Shots a 5.0 gamer1 7 b 3.0 gamer2 2 c 2.5 gam
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
情节
plt.title('Plot 1', size=14)
plt.xlabel('Number of Shots', size=14)
plt.ylabel('Mean Score', size=14)
plt.grid(b=True, which='major', color='g', linestyle='-')
x = df[['gamer','shots']].groupby(['gamer']).count()
y = df[['gamer','score']].groupby(['gamer']).mean()
plt.plot(x, y)
IIUC,你需要这样的东西:
In [52]: df.groupby('Gamer').agg({'Score':'mean','Shots':'count'}).plot()
Out[52]: <matplotlib.axes._subplots.AxesSubplot at 0xb41e710>
更新:
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>]
我只需要一个单线图来显示均值的相关性
玩家在射击次数(X轴)上的得分(Y轴)
[90]中的:df.groupby('Gamer').agg({'Score':'mean','Shots':'count')).set_index('Shots').plot()
出[90]:
更新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>]
[155]中的:g=df.groupby('Gamer').agg({'Score':'mean','Shots':'count')。排序u值('Shots'))
在[156]中:x,y=g['Shots',g['Score']
In[157]:plt.plot(x,y)
Out[157]:[]
我只需要一个单线图来显示玩家的平均得分(Y轴)与射击次数(X轴)之间的依赖关系。当试图显示由
df.groupby('gamer').agg生成的表格的图形时({'score':'mean','shots':'count'))
我想使用mathplotlib设计,就像我的文章一样。然后我做:plt.plot(x='Shots',y='Score')并得到一个空的plt。
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>]