HelloToEarth
HelloToEarth

Reputation: 2127

Multiple column plotting Python

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?

Upvotes: 0

Views: 69

Answers (1)

jwil
jwil

Reputation: 553

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!

Upvotes: 1

Related Questions