0

I've got data in the form:

Year      Month      State    Value
2001       Jan        AK        80
2001       Feb        AK        40
2001       Mar        AK        60
2001       Jan        LA        70
2001       Feb        LA        79
2001       Mar        LA        69
2001       Jan        KS        65
.
.

This data is only for Year 2001 and Months repeat on each State.

I want a basic graph with this data together in one based off the State with X-Axis being Month and Y-Axis being the Value.

When I plot with:

g = df.groupby('State')

for state, data in g:
    plt.plot(df['Month'], df['Value'], label=state)

plt.show()

I get a very wonky looking graph.

enter image description here

I know based off plotting these individually they aren't extremely different in their behaviour but they are not even close to being this much overlapped.

Is there a way of building more of a continuous plot?

4
  • Because it plots lines of the month, so for example first (Jan, 10), then (Jan, 30), then (Jan, 80), next the line moves to (Feb, 70), etc. Commented Nov 9, 2018 at 18:30
  • Would I need to make some datetime substitute to make it continuous? Commented Nov 9, 2018 at 18:43
  • What is the 'Region' variable you're using for the groupby? This would be useful for reproducibility (or just the original dataset) Commented Nov 9, 2018 at 21:19
  • Sorry I've corrected the proper format. State not Region. This only corrected the question not the problem in the code. Commented Nov 9, 2018 at 21:21

1 Answer 1

1

Your problem is that inside your for loop you're referencing df, which still has the data for all the states. Try:

for state, data in g:
    plt.plot(data['Month'], data['Value'], label = state)
plt.legend()
plt.show()

Hopefully this helps!

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

Comments

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.