Multiple figure in python using seaborn and matplotlib

So I would like to create a loop where it will generate multiple figures while each fig has 4 subplots.
Could someone please help me regarding it?

If you want the loop to generate multiple figures, then you’ll be including the fig = plt.figure() line inside the loop.

Something like the following should work:

for sp in range(0,2):
    
    # Create figure in each iteration of loop. 
    # Change figsize if you must.
    fig = plt.figure()
    
    # Add your sub-plots
    ax = fig.add_subplot(1,2,1)
    ax.plot(x_axis_list, list_1)
    
    ax2 = fig.add_subplot(1,2,2)
    ax2.plot(x_axis_list, list_2)

Above, each figure generates only 2 subplots. To change it to 4 subplots, I’d suggest altering the fig.add_subplot() lines in the following way:

ax = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)

This specifies that your figure will have subplots in 2 rows and 2 columns, and the “1” argument at the end specifies the position of that subplot in the figure. I.e. ax would be in position 1, ax2 would be in position 2, ax3 in position 3, etc.

The for loop is iterating over sp in range(0,2), which means I am only iterating twice. The sp variable can be used creatively to help refer to whichever list or series you want to use as the axis.

For instance, let’s say I make a list (above and outside the loop) that specifies 2 column names from a dataframe, df:

column_names = [column_1, column_2]

# So column_names[sp] refers to column_1 when sp is 0,
# and column_names[sp] refers to column_2 when sp is 1

In the for loop, you could refer to these column_names via indexing with the sp variable, like so:

ax.plot(x_axis_list, df[column_names[sp]])

Or use subplots and loop through it.

Here is a good article https://medium.com/@rayheberer/generating-matplotlib-subplots-programmatically-cc234629b648

Thanks @blueberrypudding85 , I was able to loop through it and add miltiple figures also seaborn Facetgrid is also a great option.