1

I am having trouble plotting the multi-index datafram below, since I cant use the m04hour.index range value for some reason.

output from m04hour.head()

However, this plot command works fine:

m04hour['consumption (kWh)'].plot(figsize=(12,2))

But this one doesnt:

fig,ax = plt.subplots(figsize=(8,4))
ax.plot(m04hour.index, m04hour['consumption(kWh)'],c='red',lw=1,label='queens')

Because the "m04hour.index" is returning the error:

ValueError: setting an array element with a sequence.

So the question is how to refer to the m04hour.index value for setting the plotting x-axis?

1 Answer 1

1

You index in this m04hour is not pd.MultiIndex. It is a index with tuples. First let's convert that list of tuples into a pd.MultiIndex.

df.index = pd.MultiIndex.from_tuples(df.index)

fig,ax = plt.subplots(figsize=(8,4))
ax.plot(m04hour.index.get_level_values(1), m04hour['consumption(kWh)'],c='red',lw=1,label='queens')

Output:

enter image description here

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

1 Comment

@GilesRichardson If this answer helped you, would you consider accepting. Thank you.

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.