I am trying to append rows of subplots to a Matplotlib figure inside a loop.
This works:
from sklearn.datasets import load_iris 
import numpy as np 
import pandas as pd 
iris_data = load_iris() 
join_pd_df = pd.DataFrame( 
  data = np.c_[ 
    iris_data['data'], 
    iris_data['target'], 
  ], 
  columns = iris_data['feature_names'] + ['target'] 
) 
import matplotlib.pyplot as plt 
import seaborn as sns 
list_of_features = [ 
  "sepal length (cm)", 
  "sepal width (cm)", 
  "petal length (cm)", 
] 
### I want to avoid this bit of pre-allocation
number_of_charts = 2 
number_of_features = len(list_of_features) 
arbitrarily_large_number_of_inches = 10 
fig, axes = plt.subplots( 
  number_of_features, 
  number_of_charts, 
  figsize=(arbitrarily_large_number_of_inches, arbitrarily_large_number_of_inches) 
) 
###:end I want to avoid this bit of pre-allocation
for iteration, feature in enumerate(list_of_features): 
  sns.regplot(x="target", y=feature, data=join_pd_df, ax=axes[iteration, 0]) 
  sns.boxplot(x=feature, y="target", data=join_pd_df, ax=axes[iteration, 1]) 
plt.subplots_adjust( 
  left = 0.1, 
  right = 0.9, 
  top = 0.9, 
  bottom = 0.1, 
  wspace = .4, 
  hspace = .4, 
) 
But I want to avoid pre-allocating the number of subplots and instead just append a row subplots to the bottom of the figure, so something along the lines of this:
from sklearn.datasets import load_iris 
import numpy as np 
import pandas as pd 
iris_data = load_iris() 
join_pd_df = pd.DataFrame( 
  data = np.c_[ 
    iris_data['data'], 
    iris_data['target'], 
  ], 
  columns = iris_data['feature_names'] + ['target'] 
) 
import matplotlib.pyplot as plt 
import seaborn as sns 
list_of_features = [ 
  "sepal length (cm)", 
  "sepal width (cm)", 
  "petal length (cm)", 
] 
arbitrarily_large_number_of_inches = 10 
fig = plt.figure( 
  figsize=(arbitrarily_large_number_of_inches, arbitrarily_large_number_of_inches) 
) 
for iteration, feature in enumerate(list_of_features, start=1): 
  ### I can't figure out what I'm doing wrong here because the subplots does not display properly
  correlation_chart_axes = fig.add_subplot(1, 2, 1) 
  sns.regplot(x="target", y=feature, data=join_pd_df, ax=correlation_chart_axes) 
  box_chart_axes = fig.add_subplot(1, 2, 2) 
  sns.boxplot(x=feature, y="target", data=join_pd_df, ax=box_chart_axes) 
  ###:end I can't figure out what I'm doing wrong here because the subplots does not display properly
plt.subplots_adjust( 
  left = 0.1, 
  right = 0.9, 
  top = 0.9, 
  bottom = 0.1, 
  wspace = .4, 
  hspace = .4, 
) 
Any tips or pointers on where to look for a newbie? Most of the articles I've found pre-allocate the number of rows and columns of subplots. Is appending to a Matplotlib figure something that just isn't done?
This post here: Dynamically add/create subplots in matplotlib suggests this bit of code:
number_of_subplots=3 # I want to avoid this preallocation
...
ax1 = subplot(number_of_subplots,1,v)
ax1.plot(x,y)
But it only adds subplots of 1 single column. I would like to add rows of subplots with 2 or more columns.
Thank you for your time


