How do I stack multiple matplotlib figures in one figure and plot it?

Viewed 46

I have a function that plots a graph with 5 columns, and I call this function

# Seaborn prints warnings if histogram has small values. We can ignore them for now
import warnings
warnings.filterwarnings('ignore')
import numpy as np
ax_arr = []
fig_arr = []
## Create a plot for every activation function
# enumerate enumerates between index and dictionary key
i = 0
for k, act_fns in act_fn_by_name.items():
    act_fn = act_fns()
    set_seed(42) # Setting the seed ensures that we have the same weight initialization for each activation function
    net_actfn = BaseNetwork(act_fn=act_fn).to(device)
    fig, ax = visualize_gradients(net_actfn, color=f"C{i}") # plotting function
    
    ax_arr.append(ax)
    fig_arr.append(fig)
    i+=1

Instead of plotting them all individually within the function, I want to plot all subplots in a 6 by 5 figure.

I thought of collecting a list of figures and axes from the function and using the matplotlib.figure.Figure and array of AxesSubplot to reconstruct the plots.

Does matplotlib work in that way at all?

List of figure objects

List of subplots

0 Answers
Related