Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a51fd9

Browse files
committed
DOC: add warnings about get_window_extent and BboxImage
Closes #2831
1 parent 5c90c35 commit 8a51fd9

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

lib/matplotlib/artist.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,29 @@ def get_window_extent(self, renderer=None):
329329
"""
330330
Get the artist's bounding box in display space.
331331
332-
The bounding box' width and height are nonnegative.
332+
The bounding box's width and height are non-negative.
333333
334334
Subclasses should override for inclusion in the bounding box
335335
"tight" calculation. Default is to return an empty bounding
336336
box at 0, 0.
337337
338-
Be careful when using this function, the results will not update
339-
if the artist window extent of the artist changes. The extent
340-
can change due to any changes in the transform stack, such as
341-
changing the Axes limits, the figure size, or the canvas used
342-
(as is done when saving a figure). This can lead to unexpected
343-
behavior where interactive figures will look fine on the screen,
344-
but will save incorrectly.
338+
.. warning ::
339+
340+
Be careful when using this function, the results will not update if
341+
the artist window extent of the artist changes.
342+
343+
The extent can change due to any changes in the transform stack, such
344+
as changing the Axes limits, the figure size, the canvas used (as is
345+
done when saving a figure), or the DPI.
346+
347+
This can lead to unexpected behavior where interactive figures will
348+
look fine on the screen, but will save incorrectly.
349+
350+
To get accurate results you may need to manually call
351+
`matplotlib.figure.Figure.savefig` or
352+
`matplotlib.figure.Figure.draw_without_rendering` to have Matplotlib
353+
compute the rendered size.
354+
345355
"""
346356
return Bbox([[0, 0], [0, 0]])
347357

lib/matplotlib/image.py

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

13751375

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

1395-
kwargs are an optional list of Artist keyword args
1396-
"""
13971436
super().__init__(
13981437
None,
13991438
cmap=cmap,
@@ -1409,12 +1448,11 @@ def __init__(self, bbox,
14091448
self.bbox = bbox
14101449

14111450
def get_window_extent(self, renderer=None):
1412-
if renderer is None:
1413-
renderer = self.get_figure()._get_renderer()
1414-
14151451
if isinstance(self.bbox, BboxBase):
14161452
return self.bbox
14171453
elif callable(self.bbox):
1454+
if renderer is None:
1455+
renderer = self.get_figure()._get_renderer()
14181456
return self.bbox(renderer)
14191457
else:
14201458
raise ValueError("Unknown type of bbox")

0 commit comments

Comments
 (0)