3

I am trying to create a line graph using the following python pandas information: index which is the date/period, 'student' which is the number of students who have visited per period and region which is where the students are visiting from. Ideally, I would like the number of students on the y axis, the date on the x axis and the region to be a different color or at least a different line associated with each region:

Year    Region    Student
2013    North     1
2013    South     2
2013    East      5
2014    North     3
2014    South     7
2014    East      10

I am having trouble finding examples of being able to utilize a dataframe series (i.e., vs having to list each element explicitly and instead reference the index as the x axis or student as the y axis) by name.

1 Answer 1

2

You can iterate through your groups after you use groupby to group your data by Region, then plot each one. Here is a basic scaffold of code that you can build on:

for region, data in df.groupby('Region'):
    plt.plot(data['Year'], data['Student'], label=region)

plt.xlabel('Year')
plt.ylabel('Number of Students')
plt.legend()
plt.show()

enter image description here

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

2 Comments

hi sacul, while this is probably a bad example, is there a way to turn this chart into a pie graph based on region and sum( students) from that region?
That's a whole different problem. I'd suggest asking a new question with specifics as to how you want the pie graph to look. But you can take a look at matplotlib.org/api/_as_gen/matplotlib.pyplot.pie.html, it might be enough for you to adapt the code above

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.