3

I am plotting a multi-index columns DataFrame.

What is the syntax to specify the column(s) to be plotted on secondary_y using the .plot method of pandas DataFrame?

Setup

import numpy as np
import pandas as pd

mt_idx = pd.MultiIndex.from_product([['A', 'B'], ['first', 'second']])
df = pd.DataFrame(np.random.randint(0, 10, size=(20, len(mt_idx))), columns=mt_idx)

My Attempts

df.plot(secondary_y=('B', 'second'))
df.plot(secondary_y='(B, second)')

None of the above worked, as all the lines were plotted on the principal y-axis.

FLab
  • 7,136
  • 5
  • 36
  • 69
  • According to the documentation you should be able to user your form of passing a tuple or a list. But I tried it also and it doesn't. – MrLeeh May 23 '18 at 08:49

2 Answers2

2

One possible solution would be to plot each column, then specify secondary=True. Doing it the following way requires you to specifiy the axes to which they will be plotted:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

mt_idx = pd.MultiIndex.from_product([['A', 'B'], ['first', 'second']])
df = pd.DataFrame(np.random.randint(0, 10, size=(20, len(mt_idx))), columns=mt_idx)

df.A.plot(ax=ax)
df.B.plot(ax=ax, secondary_y=True)

plt.show()

enter image description here

DavidG
  • 24,279
  • 14
  • 89
  • 82
2

You might drop the upper column index level. If you don't want to modify the original dataframe, this could be done on a copy of it.

df2 = df.copy()
df2.columns = df2.columns.map('_'.join)

df2.plot(secondary_y=('B_second'))

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712