Dervin Thunk
Dervin Thunk

Reputation: 20129

How do I "reset the index" for a matplotlib plot?

I have the following code:

fig, ax = plt.subplots(1, 1)
calls["2016-12-24"].resample("1h").sum().plot(ax=ax)
calls["2016-12-25"].resample("1h").sum().plot(ax=ax)
calls["2016-12-26"].resample("1h").sum().plot(ax=ax)

which generates the following image:

enter image description here

How can I make this so the lines share the x-axis? In other words, how do I make them not switch days?

Upvotes: 0

Views: 1233

Answers (1)

Alessandro Mariani
Alessandro Mariani

Reputation: 1221

If you don't care about using the correct datetime as index, you could just reset the index as you suggested for all the series. This is going to overlap all the time series, if this is what you're trying to achieve.

# the below should
calls["2016-12-24"].resample("1h").sum().reset_index("2016-12-24").plot(ax=ax)
calls["2016-12-25"].resample("1h").sum().reset_index("2016-12-25").plot(ax=ax)
calls["2016-12-26"].resample("1h").sum().reset_index("2016-12-26").plot(ax=ax)

Otherwise you should try as well to resample the three columns at the same time. Have a go with the below but not knowing how your original dataframe look like, I'm not sure this will fit your case. You should post some more information about the input dataframe.

# have a try with the below 
calls[["2016-12-24","2016-12-25","2016-12-26"].resample('1h').sum().plot()

Upvotes: 1

Related Questions