Figure created using axes of another figure not dynamically updating axes

30 Views Asked by At

If you run the below code in ipython, you will find that the first window that pops up can be resized and the plots all adjust as usual.

The second figure is created by trying to copy over the axis objects from the first figure. It seems to work, but the second window that pops up doesn't readjust the size of the plots like the first window did.

What can be done so that the second figures axes update just as the first figures do? In case it's relevant, my particular application will pull axis objects from several different figures and replot them in a new figure. I won't be merely recreating the same plot.

import matplotlib.gridspec as gridspec                                                   
import matplotlib.pyplot as plt                                                           
                                                                                          
fig_temp, axlist = plt.subplots(2, 2)                                                     
axlist[0][0].plot([1, 2, 3])                                                              
axlist[0][1].plot([3, 2, 3])                                                              
axlist[1][0].plot([3, 2, 1])                                                              
axlist[1][1].plot([1, 3, 2])                                                              
                                                                                          
plt.show()                                                                                
                                                                                          
fig = plt.figure(figsize=(16, 16))                                                        
gs = gridspec.GridSpec(2, 2)                                                              
                                                                                          
for i, row in enumerate(axlist):                                                          
    for ax, gs_elem in zip(row, [gs[i, 0], gs[i, 1]]):                                    
        ax.figure = fig                                                                   
        ax.set_position(gs_elem.get_position(fig))                                        
        ax.set_subplotspec(gs_elem)                                                       
        fig.add_axes(ax)                                                                  
                                                                                          
fig.tight_layout()                                                                        
plt.show()   
0

There are 0 best solutions below