1

I have generated a list of matplotlib AxesSubplot object and I would like to combine them into one figure with a subplot for each AxesSubplot object. Let's say I have 9 AxesSubplot object stored in a python list called "axlist" and I want to display them together in a 3x3 grid:

def random_figure_generator(n):
    """
    Create n random plots
    """
    axlist = list()
    for i in range(n):
        fig, ax = plt.subplots()
        x = sorted(np.random.random(100))
        ax.plot(np.linspace(0,100,100), np.random.random(100))
        plt.show(fig)
        plt.close(fig)
        axlist.append(ax)
    return axlist

#Create a list of axes object 
axlist = random_figure_generator(9)

#Attempt to combine them
axes = []
fig, axes = plt.subplots(3,3)
axes = axes.flatten()
for i in range(len(axlist)):
    axes[i].axes = axlist[i] #try to assign each AxesSubplot object to one subplot in the new figure
plt.show()

And here is what I get: Figure with 3x3 subplots

Jxb
  • 11
  • 2
  • This is not useful. Better create the plot inside the figure which should contain the plot. – ImportanceOfBeingErnest Feb 22 '18 at 15:56
  • It is indeed easier to create the plots inside the figure, but I need to look at individual plots earlier in my analysis and wanted to reuse them instead of recreating all the plots a second time. – Jxb Feb 23 '18 at 14:21
  • Any solution of "moving" axes around between figures is much more brittle and a lot more complicated, than just using a function which can be reused to draw the same plot on several axes. – ImportanceOfBeingErnest Feb 23 '18 at 14:23

0 Answers0