@@ -2336,6 +2336,23 @@ def __init__(self,
2336
2336
# list of child gridspecs for this figure
2337
2337
self ._gridspecs = []
2338
2338
2339
+ def _check_layout_engines_compat (self , old , new ):
2340
+ """
2341
+ Helper for set_layout engine
2342
+
2343
+ If the figure has used the old engine and added a colorbar then the
2344
+ value of colorbar_gridspec must be the same on the new engine.
2345
+ """
2346
+ if old is None or old .colorbar_gridspec == new .colorbar_gridspec :
2347
+ return True
2348
+ # colorbar layout different, so check if any colorbars are on the
2349
+ # figure...
2350
+ for ax in self .axes :
2351
+ if hasattr (ax , '_colorbar' ):
2352
+ # colorbars list themselvs as a colorbar.
2353
+ return False
2354
+ return True
2355
+
2339
2356
def set_layout_engine (self , layout = None , ** kwargs ):
2340
2357
"""
2341
2358
Set the layout engine for this figure.
@@ -2359,14 +2376,21 @@ def set_layout_engine(self, layout=None, **kwargs):
2359
2376
self ._layout_engine = None
2360
2377
return
2361
2378
if layout == 'tight' :
2362
- self . _layout_engine = TightLayoutEngine (** kwargs )
2379
+ new_layout_engine = TightLayoutEngine (** kwargs )
2363
2380
elif layout == 'constrained' :
2364
- self . _layout_engine = ConstrainedLayoutEngine (** kwargs )
2381
+ new_layout_engine = ConstrainedLayoutEngine (** kwargs )
2365
2382
elif isinstance (layout , LayoutEngine ):
2366
- self . _layout_engine = layout
2383
+ new_layout_engine = layout
2367
2384
else :
2368
2385
raise ValueError (f"Invalid value for 'layout': { layout !r} " )
2369
- self ._layout_engine .set_figure (self )
2386
+
2387
+ if self ._check_layout_engines_compat (self ._layout_engine ,
2388
+ new_layout_engine ):
2389
+ self ._layout_engine = new_layout_engine
2390
+ else :
2391
+ _api .warn_external ('Colorbar layout of new layout engine not '
2392
+ 'compatible with old engine, and a colorbar '
2393
+ 'has been created. Engine not changed.' )
2370
2394
2371
2395
def get_layout_engine (self ):
2372
2396
return self ._layout_engine
0 commit comments