Home > OS >  How to iterate subplots by columns instead of rows in matplotlib
How to iterate subplots by columns instead of rows in matplotlib

Time:03-21

Supposed I have a dataframe of 30 columns, with the first 10 columns being the 'mean' of various collected data, then the next 10 columns being 'median', and then the last 10 columns being 'mode'. For example, the 1st column is height_mean, the 10th column is height_median, and the 20th column is height_mode. The 2nd column is weight_mean, then 11th column is weight_median, and 21st column is weight_median.

height_mean ... height_median ... height_mode ...
56 ... 58 ... 55 ...

I want to create a 10x3 subplot that takes the mean, median, and mode plots for each respective variable and plots them adjacently such that each row of the subplot is a new variable. So the first row of the subplots would have the mean, median, and mode plots for height, and the second row of the subplots would have the mean, median, and mode for weight, and so on.

So far, I have this, which creates the subplot in the order of the columns as they come in the dataframe:

fig, axs = plt.subplots(10, 3, figsize=(20,20))

for count, ax in zip(df.columns, axs.ravel()):
    df_data.loc[:,[count]].boxplot(ax=ax)
plt.show()  

I tried to do something like this, but doesn't work:

for n, column in enumerate(df_columns):
    # add a new subplot iteratively
    ax = plt.subplot(10, 3, n   1)
    df_data.iloc[:,[n]].boxplot(ax=ax)
    ax = plt.subplot(10, 3, n   2)
    df_data.iloc[:,[n 9]].boxplot(ax=ax)
    ax=plt.subplot(10,3,n 3)
    df_data.iloc[:,[n 19]].boxplot(ax=ax)

CodePudding user response:

To iterate the subplots column-wise, it's simplest to just transpose axs before raveling, i.e., axs.T.ravel():

for col, ax in zip(df.columns, axs.T.ravel()):

Minimal example:

headers = ['height_mean', 'weight_mean', 'height_median', 'weight_median', 'height_mode', 'weight_mode']
df = pd.DataFrame(np.random.random((10, 6)), columns=headers)
#    height_mean  weight_mean  height_median  weight_median  height_mode  weight_mode
# 0     0.982351     0.548713       0.559040       0.497497     0.842977     0.738946
# 1     0.832716     0.660227       0.754149       0.286536     0.315156     0.312089
# 2     0.997460     0.290112       0.501917       0.974751     0.097419     0.559542

ncols = 3
nrows = len(df.columns) // ncols

fig, axs = plt.subplots(nrows, ncols, sharey=True, constrained_layout=True)
for col, ax in zip(df.columns, axs.T.ravel()):
    df[[col]].boxplot(ax=ax)

  • Related