[英]Create multiple plots from Pandas dataframe
I have the following Pandas dataframe: 我有以下熊猫数据框:
data ={'Studentid'['SSmith','SSmith','SSmith','SSmith','PPeters','PPeters','PPeters'],
'LastName':['Smith','Smith','Smith','Smith','Peters','Peters','Peters'],
'FirstName':['S','S','S','S','P','P','P'],
'year':[2012,2013,2014,2014,2015,2015,2015],
'month':[11,7,1,2,10,11,12],
'BookLevel':[1.4,1.5,2.5,3.8,3.0,3.8,3.7],
'BookProgress':[0.0, 0.1, 1.1, 2.4, 0.0, 0.8, 0.7],
'DateProgress':[0.0, .68, 1.21, 1.29, .04, .12, .18],
'PlusMinus':[0.0, -.58, -.11, 1.11, -.04, .68, .52]}
I am trying to create separate plots for Smith and Peters. 我正在尝试为史密斯和彼得斯创建单独的地块。 I have tried many alternatives and the closest I have come is with the following code: 我尝试了许多替代方法,而最接近的方法是以下代码:
x=[(df['DateProgress'])]
y=[(df['BookProgress'])]
for i, group in df.groupby("Studentid"):
plt.figure()
plt.scatter(x,y)
plt.plot(range(4))
plt.xlim(-.5, 3.5)
plt.ylim(-.5, 3.5)
plt.xlabel('DateProgress')
plt.ylabel('BookProgress')
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
The result is two separate plots but they are identical and both contain all of the data points for Smith and Peters. 结果是两个单独的图,但是它们是相同的,并且都包含Smith和Peters的所有数据点。 I am surprised that I was able to get two plots -- even though they are not the plots I want. 令我惊讶的是,我能够获得两个地块-即使它们不是我想要的地块。 I have no clue how to get what I'm looking for...That is one plot for Smith with 4 data points and another separate plot for Peters with 3 data points. 我不知道如何得到我想要的东西...那是史密斯(Smith)有4个数据点的图,而彼得斯(Peters)有3个数据点。 Any help is appreciated 任何帮助表示赞赏
Not tested. 未经测试。 You were very close, you just need to define your x
and y
anew for each group. 您非常接近,您只需要为每个组重新定义x
和y
。
--piRSquared (Now Tested)... it works. --piRSquared(已测试)...有效。
for i, group in df.groupby("Studentid"):
plt.figure()
x=group['DateProgress']
y=group['BookProgress']
plt.scatter(x,y)
plt.plot(range(4))
plt.xlim(-.5, 3.5)
plt.ylim(-.5, 3.5)
plt.xlabel('DateProgress')
plt.ylabel('BookProgress')
plt.gca().set_aspect('equal', adjustable='box')
plt.title(i) #Label each plot
plt.show()
I just want to mention that if you are OK with having both plots in the same axes in different colour, you can use much more elegant approach: http://pandas.pydata.org/pandas-docs/stable/visualization.html 我只想提一下,如果可以在同一轴上以不同的颜色同时绘制两个图,则可以使用更优雅的方法: http : //pandas.pydata.org/pandas-docs/stable/visualization.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.