Reputation: 153
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. 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. Any help is appreciated
Upvotes: 2
Views: 5410
Reputation: 5976
Not tested. You were very close, you just need to define your x
and y
anew for each group.
--piRSquared (Now Tested)... it works.
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
Upvotes: 4