2

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

4
  • Thanks! kinda humbling when I put three days in and came so close...you answered almost immediately Commented Jun 10, 2016 at 17:15
  • Special favor? How do I label each plot with the Studentid represented in the plot? First tries aren't working Commented Jun 10, 2016 at 17:52
  • See edit, no problem Commented Jun 10, 2016 at 20:19
  • Thanks...I was making it more complicated than necessary...sometimes the simplest is best. Commented Jun 10, 2016 at 20:28

1 Answer 1

4

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()

enter image description here

enter image description here

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

Sign up to request clarification or add additional context in comments.

1 Comment

NP, glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.