Skip to content

Commit 956cc02

Browse files
authored
Merge pull request #29910 from tacaswell/doc/BboxImage-get_window_extent
DOC: add warnings about get_window_extent and BboxImage
2 parents 5bd9b9d + d494c31 commit 956cc02

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

lib/matplotlib/artist.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,28 @@ def get_window_extent(self, renderer=None):
323323
"""
324324
Get the artist's bounding box in display space.
325325
326-
The bounding box' width and height are nonnegative.
326+
The bounding box's width and height are non-negative.
327327
328328
Subclasses should override for inclusion in the bounding box
329329
"tight" calculation. Default is to return an empty bounding
330330
box at 0, 0.
331331
332-
Be careful when using this function, the results will not update
333-
if the artist window extent of the artist changes. The extent
334-
can change due to any changes in the transform stack, such as
335-
changing the Axes limits, the figure size, or the canvas used
336-
(as is done when saving a figure). This can lead to unexpected
337-
behavior where interactive figures will look fine on the screen,
338-
but will save incorrectly.
332+
.. warning::
333+
334+
The extent can change due to any changes in the transform stack, such
335+
as changing the Axes limits, the figure size, the canvas used (as is
336+
done when saving a figure), or the DPI.
337+
338+
Relying on a once-retrieved window extent can lead to unexpected
339+
behavior in various cases such as interactive figures being resized or
340+
moved to a screen with different dpi, or figures that look fine on
341+
screen render incorrectly when saved to file.
342+
343+
To get accurate results you may need to manually call
344+
`matplotlib.figure.Figure.savefig` or
345+
`matplotlib.figure.Figure.draw_without_rendering` to have Matplotlib
346+
compute the rendered size.
347+
339348
"""
340349
return Bbox([[0, 0], [0, 0]])
341350

lib/matplotlib/image.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,8 +1387,52 @@ def set_data(self, A):
13871387

13881388

13891389
class BboxImage(_ImageBase):
1390-
"""The Image class whose size is determined by the given bbox."""
1390+
"""
1391+
The Image class whose size is determined by the given bbox.
1392+
1393+
Parameters
1394+
----------
1395+
bbox : BboxBase or Callable[RendererBase, BboxBase]
1396+
The bbox or a function to generate the bbox
1397+
1398+
.. warning ::
1399+
1400+
If using `matplotlib.artist.Artist.get_window_extent` as the
1401+
callable ensure that the other artist is drawn first (lower zorder)
1402+
or you may need to renderer the figure twice to ensure that the
1403+
computed bbox is accurate.
13911404
1405+
cmap : str or `~matplotlib.colors.Colormap`, default: :rc:`image.cmap`
1406+
The Colormap instance or registered colormap name used to map scalar
1407+
data to colors.
1408+
norm : str or `~matplotlib.colors.Normalize`
1409+
Maps luminance to 0-1.
1410+
interpolation : str, default: :rc:`image.interpolation`
1411+
Supported values are 'none', 'auto', 'nearest', 'bilinear',
1412+
'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite',
1413+
'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell',
1414+
'sinc', 'lanczos', 'blackman'.
1415+
origin : {'upper', 'lower'}, default: :rc:`image.origin`
1416+
Place the [0, 0] index of the array in the upper left or lower left
1417+
corner of the Axes. The convention 'upper' is typically used for
1418+
matrices and images.
1419+
filternorm : bool, default: True
1420+
A parameter for the antigrain image resize filter
1421+
(see the antigrain documentation).
1422+
If filternorm is set, the filter normalizes integer values and corrects
1423+
the rounding errors. It doesn't do anything with the source floating
1424+
point values, it corrects only integers according to the rule of 1.0
1425+
which means that any sum of pixel weights must be equal to 1.0. So,
1426+
the filter function must produce a graph of the proper shape.
1427+
filterrad : float > 0, default: 4
1428+
The filter radius for filters that have a radius parameter, i.e. when
1429+
interpolation is one of: 'sinc', 'lanczos' or 'blackman'.
1430+
resample : bool, default: False
1431+
When True, use a full resampling method. When False, only resample when
1432+
the output image is larger than the input image.
1433+
**kwargs : `~matplotlib.artist.Artist` properties
1434+
1435+
"""
13921436
def __init__(self, bbox,
13931437
*,
13941438
cmap=None,
@@ -1401,12 +1445,7 @@ def __init__(self, bbox,
14011445
resample=False,
14021446
**kwargs
14031447
):
1404-
"""
1405-
cmap is a colors.Colormap instance
1406-
norm is a colors.Normalize instance to map luminance to 0-1
14071448

1408-
kwargs are an optional list of Artist keyword args
1409-
"""
14101449
super().__init__(
14111450
None,
14121451
cmap=cmap,
@@ -1422,12 +1461,11 @@ def __init__(self, bbox,
14221461
self.bbox = bbox
14231462

14241463
def get_window_extent(self, renderer=None):
1425-
if renderer is None:
1426-
renderer = self.get_figure()._get_renderer()
1427-
14281464
if isinstance(self.bbox, BboxBase):
14291465
return self.bbox
14301466
elif callable(self.bbox):
1467+
if renderer is None:
1468+
renderer = self.get_figure()._get_renderer()
14311469
return self.bbox(renderer)
14321470
else:
14331471
raise ValueError("Unknown type of bbox")

0 commit comments

Comments
 (0)