At the moment, the subplots
parameter in df.plot()
only supports boolean values: Generate either as many plots as there are columns or generate a single plot with all columns.
It would be nice if subplots
understood level specifications and generate one plot for each unique value in the corresponding level(s) of a multi-index type columns
index.
Code Sample
Let
import numpy as np
import pandas as pd
df = pd.DataFrame([np.sin(np.linspace(0,y,10))+z for y in [1,2,3] for z in [10,20]]).T
df.columns = pd.MultiIndex.from_tuples([(y,z) for y in ['Y1','Y2','Y3'] for z in ['Z1','Z2']],names=['Y','Z'])
Y Y1 Y2 Y3
Z Z1 Z2 Z1 Z2 Z1 Z2
0 10.000000 20.000000 10.000000 20.000000 10.000000 20.000000
1 10.110883 20.110883 10.220398 20.220398 10.327195 20.327195
2 10.220398 20.220398 10.429956 20.429956 10.618370 20.618370
3 10.327195 20.327195 10.618370 20.618370 10.841471 20.841471
4 10.429956 20.429956 10.776372 20.776372 10.971938 20.971938
5 10.527415 20.527415 10.896192 20.896192 10.995408 20.995408
6 10.618370 20.618370 10.971938 20.971938 10.909297 20.909297
7 10.701698 20.701698 10.999884 20.999884 10.723086 20.723086
8 10.776372 20.776372 10.978656 20.978656 10.457273 20.457273
9 10.841471 20.841471 10.909297 20.909297 10.141120 20.141120
The proposed feature would allow
[df.xs(val,level='Z',axis=1).plot().set_title(val) for val in df.columns.levels[1]]
to be replaced by
df.plot(subplots='Z')
More generally, df.plot(subplots=['Z','Y'])
would draw one plot for each combination of values Z and Y. For the particular dataframe defined above, where 'Z' and 'Y' are the only column levels, this would be equivalent to subplots=True, though it would be a nice bonus feature if the subplots were generated on a grid, with Y varying in the horizontal grid direction, and Z varying the vertical grid direction.
Comment From: mroeschke
Thanks for the suggestion, but since this request hasn't gotten much interest from the community or core devs in a while going to close. Can reopen if there's renewed interest