#!/usr/bin/env python # coding: utf-8 # ### Answering https://stackoverflow.com/questions/73144911/subplotting-using-for-loops # # The MRE has numerous errors before you even get to the 'no luck' block. I was able to fix some but maybe lost the idea of the desired layout? # In[1]: import numpy as np import pandas as pd from scipy import stats import matplotlib.pyplot as plt label=['hc','svppa','nfvppa','lvppa'] test_df_data ={"id":list(range(1,21,1)), "label": list(np.repeat(label, 5)), "col1":list(np.random.normal(100,10,size=20)), "col2":list(np.random.normal(100,10,size=20)), "col3":list(np.random.normal(100,10,size=20)), "col4":list(np.random.normal(100,10,size=20)), "col5":list(np.random.normal(100,10,size=20)), "col6":list(np.random.normal(100,10,size=20)), "col7":list(np.random.normal(100,10,size=20))} df = pd.DataFrame(test_df_data) # In[2]: df # In[3]: # What OP had, I think labels = set(df.label.to_list()) columns = df.columns[2:].to_list() for col in columns: for label in labels: stats.probplot(df[df['label']==label][col], dist='norm', plot=plt) plt.title("Probability plot " + col + " - " + label) plt.show() # ### Solution # # Based on https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.probplot.html # and method 2 at https://engineeringfordatascience.com/posts/matplotlib_subplots/ # In[5]: # based on https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.probplot.html # and method 2 at https://engineeringfordatascience.com/posts/matplotlib_subplots/ plt.figure(figsize=(45, 12)) plt.subplots_adjust(hspace=0.5) for n, label in enumerate(labels): for m,col in enumerate(columns): ax = plt.subplot(len(labels), len(columns), ((len(columns)-1)*n)+(m+(n+1))) b = stats.probplot(df[df['label']==label][col], dist='norm', plot=ax) plt.title("Probability plot " + col + " - " + label) plt.subplots_adjust(top=1.15) # based on https://www.statology.org/matplotlib-subplot-spacing/ to fix title of subplot overlapping with x-axes label plt.savefig('test_save.png', bbox_inches="tight") # based on https://stackoverflow.com/a/47956856/8508004 # **Double click on the file `test_save.png` in the file navigator pane to view the plots in their full glory if you are using JupyterLab.**