2

This is my multiindex data.

Month   Hour    Hi
1       9       84.39
       10       380.41
       11       539.06
       12       588.70
       13       570.62
       14       507.42
       15       340.42
       16       88.91
2       8       69.31
        9       285.13
       10       474.95
       11       564.42
       12       600.11
       13       614.36
       14       539.79
       15       443.93
       16       251.57
       17       70.51

I want to make subplot where each subplot represent the Month. x axis is hour, y axis is Hi of the respective month. This gives a beautiful approach as follow:

levels = df.index.levels[0]
fig, axes = plt.subplots(len(levels), figsize=(3, 25))

for level, ax in zip(levels, axes):
    df.loc[level].plot(ax=ax, title=str(level))
plt.tight_layout()

enter image description here

I want to make 1x2 subplot instead of vertically arranged as above. Later, with larger data, I want to make 3x4 subplot or even larger dimension.
How to do it?

1
  • fig, axes = plt.subplots(1, len(levels), figsize=(3, 25)) Commented May 16, 2019 at 16:28

2 Answers 2

3

You can do it in pandas

df.Hi.unstack(0).fillna(0).plot(kind='line',subplots=True, layout=(1,2))

enter image description here

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

Comments

3

Pass the rows and columns arguments to plt.subplots

levels = df.index.levels[0]
#         Number of rows v
fig, axes = plt.subplots(1, len(levels), figsize=(6, 3))

for level, ax in zip(levels, axes):
    df.loc[level].plot(ax=ax, title=str(level))
plt.tight_layout()

enter image description here

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.