@@ -490,6 +490,7 @@ class Figure(mfigure.Figure):
490
490
"To disable it, set pplt.rc['subplots.tight'] to False or pass tight=False "
491
491
'to pplt.subplots(). For details, see fig.auto_layout().'
492
492
)
493
+ _warn_interactive = True # disabled after first warning
493
494
494
495
def __repr__ (self ):
495
496
opts = {}
@@ -585,6 +586,28 @@ def __init__(
585
586
self ._figwidth = units (figwidth , 'in' )
586
587
self ._figheight = units (figheight , 'in' )
587
588
589
+ # Add special consideration for interactive backends
590
+ backend = _not_none (rc .backend , '' )
591
+ backend = backend .lower ()
592
+ interactive = 'nbagg' in backend or 'ipympl' in backend
593
+ if not interactive :
594
+ pass
595
+ elif figwidth is None or figheight is None :
596
+ figsize = rc ['figure.figsize' ] # modified by proplot
597
+ self ._figwidth = figwidth = _not_none (figwidth , figsize [0 ])
598
+ self ._figheight = figheight = _not_none (figheight , figsize [1 ])
599
+ self ._refwidth = self ._refheight = None # critical!
600
+ if self ._warn_interactive :
601
+ Figure ._warn_interactive = False # set class attribute
602
+ warnings ._warn_proplot (
603
+ 'Auto-sized ProPlot figures are not compatible with interactive '
604
+ "backends like '%matplotlib widget' and '%matplotlib notebook'. "
605
+ f'Reverting to the figure size ({ figwidth } , { figheight } ). To make '
606
+ 'auto-sized figures, please consider using the non-interactive '
607
+ '(default) backend. This warning message is shown the first time '
608
+ 'you create a figure without explicitly specifying the size.'
609
+ )
610
+
588
611
# Add space settings
589
612
# NOTE: This is analogous to 'subplotpars' but we don't worry about
590
613
# user mutability. Think it's perfectly fine to ask users to simply
@@ -1303,7 +1326,7 @@ def subplots(self, *args, **kwargs): # shorthand
1303
1326
"""
1304
1327
return self .add_subplots (* args , ** kwargs )
1305
1328
1306
- def auto_layout (self , renderer = None , resize = None , aspect = None , tight = None ):
1329
+ def auto_layout (self , renderer = None , aspect = None , tight = None , resize = None ):
1307
1330
"""
1308
1331
Automatically adjust the figure size and subplot positions. This is
1309
1332
triggered automatically whenever the figure is drawn.
@@ -1312,15 +1335,6 @@ def auto_layout(self, renderer=None, resize=None, aspect=None, tight=None):
1312
1335
----------
1313
1336
renderer : `~matplotlib.backend_bases.RendererBase`, optional
1314
1337
The renderer. If ``None`` a default renderer will be produced.
1315
- resize : bool, optional
1316
- If ``False``, the current figure dimensions are fixed and automatic
1317
- figure resizing is disabled. This is set to ``False`` if the current
1318
- backend is the `interactive ipython notebook backend \
1319
- <https://ipython.readthedocs.io/en/stable/interactive/plotting.html#id1>`__,
1320
- which cannot handle automatic resizing. By default, the figure size may
1321
- change unless both `figwidth` and `figheight` or `figsize` were passed
1322
- to `~Figure.subplots`, `~Figure.set_size_inches` was called manually,
1323
- or the figure was resized manually with an interactive backend.
1324
1338
aspect : bool, optional
1325
1339
Whether to update the figure size based on the reference subplot aspect
1326
1340
ratio. By default, this is ``True``. This only has an effect if the
@@ -1329,6 +1343,12 @@ def auto_layout(self, renderer=None, resize=None, aspect=None, tight=None):
1329
1343
Whether to update the figuer size and subplot positions according to
1330
1344
a "tight layout". By default, this takes on the value of `tight` passed
1331
1345
to `Figure`. If nothing was passed, it is :rc:`subplots.tight`.
1346
+ resize : bool, optional
1347
+ If ``False``, the current figure dimensions are fixed and automatic
1348
+ figure resizing is disabled. By default, the figure size may change
1349
+ unless both `figwidth` and `figheight` or `figsize` were passed
1350
+ to `~Figure.subplots`, `~Figure.set_size_inches` was called manually,
1351
+ or the figure was resized manually with an interactive backend.
1332
1352
"""
1333
1353
# *Impossible* to get notebook backend to work with auto resizing so we
1334
1354
# just do the tight layout adjustments and skip resizing.
@@ -1338,9 +1358,9 @@ def auto_layout(self, renderer=None, resize=None, aspect=None, tight=None):
1338
1358
aspect = True
1339
1359
if tight is None :
1340
1360
tight = self ._autospace
1341
- if resize is None :
1342
- backend = _not_none ( rc . backend , '' ). lower ()
1343
- resize = 'nbagg' not in backend and 'ipympl' not in backend
1361
+ if resize is False : # fix the size
1362
+ self . _figwidth , self . _figheight = self . get_size_inches ()
1363
+ self . _refwidth = self . _refheight = None # critical!
1344
1364
1345
1365
# Helper functions
1346
1366
# NOTE: Have to draw legends and colorbars early (before reaching axes
@@ -1349,7 +1369,7 @@ def auto_layout(self, renderer=None, resize=None, aspect=None, tight=None):
1349
1369
def _draw_content ():
1350
1370
for ax in self ._iter_axes (hidden = False , children = True ):
1351
1371
ax ._draw_guides () # may trigger resizes if panels are added
1352
- def _align_content (): # noqa: E301
1372
+ def _align_content (): # noqa: E306
1353
1373
for axis in 'xy' :
1354
1374
self ._align_axis_label (axis )
1355
1375
for side in ('left' , 'right' , 'top' , 'bottom' ):
@@ -1363,10 +1383,10 @@ def _align_content(): # noqa: E301
1363
1383
if not gs :
1364
1384
return
1365
1385
if aspect :
1366
- gs ._auto_layout_aspect (resize = resize )
1386
+ gs ._auto_layout_aspect ()
1367
1387
_align_content ()
1368
1388
if tight :
1369
- gs ._auto_layout_space (renderer , resize = resize )
1389
+ gs ._auto_layout_space (renderer )
1370
1390
_align_content ()
1371
1391
1372
1392
def format (
0 commit comments