0

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()   
MaanDoabeDa
  • 121
  • 6
  • 2
    Because those artists all have a transform that is listening to the original figures size, not your new figure. If you change the size of the original figure you will find your objects will change size in the new figure (if you trigger a redraw) Matplotlib is not really meant to work this way. – Jody Klymak Apr 17 '23 at 19:07
  • Hmm I see. Well, I went through a ton of effort to format multiple plots, and I just want to reuse those axis objects for my second graph. Is there some way to have them 'listen' to this new figure? – MaanDoabeDa Apr 17 '23 at 19:11
  • Copy and paste the code you used the first time? – Jody Klymak Apr 17 '23 at 19:54
  • Yeah, I didn't want to do that. But I will have to make a plotting function that takes in an axis and populates it with plots... Sad figures and axes aren't separable in matplotlib... – MaanDoabeDa Apr 17 '23 at 20:56

0 Answers0