2

According to the following answer I should be able to use subplots with multiindex to show pairwise plots: Pandas Plotting with Multi-Index

But this seems not to work for me. This is an example:

import pandas as pd
import numpy as np
# prepare example data
adf = pd.DataFrame(index = pd.date_range('2019-01-01', periods=30), 
             data= np.random.randint(0,100,(30,2)), columns=['X','Y'])
bdf = pd.DataFrame(index = pd.date_range('2019-01-01', periods=30), 
             data= np.random.randint(0,100,(30,2)), columns=['X','Y'])
df = pd.concat({'a': adf, 'b': bdf}).unstack(level=0)

# plot
_ = df.plot(kind='line', subplots=True, figsize=(10, 10))

The result is 4 plots for each column.

enter image description here

But I want 2 pairwise plots like this:

enter image description here

I can achieve this with the following line:

_ = [df.loc[:,df.columns.get_level_values(0) == c].plot() for c in df.columns.get_level_values(0).unique()]

But should this not be possible with multiindex and subplots feature?

mjspier
  • 6,386
  • 5
  • 33
  • 43

0 Answers0