|
1 | 1 | from collections import namedtuple
|
2 | 2 | import numpy.testing as nptest
|
3 | 3 | import pytest
|
| 4 | +import io |
| 5 | +import numpy as np |
| 6 | +from numpy.testing import assert_all
8000
close |
4 | 7 | from matplotlib.testing.decorators import image_comparison
|
5 | 8 | import matplotlib.pyplot as plt
|
6 | 9 | import matplotlib.patches as mpatches
|
7 | 10 | import matplotlib.lines as mlines
|
8 |
| -from matplotlib.offsetbox import ( |
9 |
| - AnchoredOffsetbox, DrawingArea, _get_packed_offsets) |
| 11 | +import matplotlib.transforms as mtransforms |
| 12 | +from matplotlib.offsetbox import (AnnotationBbox, OffsetImage, |
| 13 | + AnchoredOffsetbox, DrawingArea, |
| 14 | + _get_packed_offsets) |
10 | 15 |
|
11 | 16 |
|
12 | 17 | @image_comparison(['offsetbox_clipping'], remove_text=True)
|
@@ -180,3 +185,79 @@ def test_get_packed_offsets_equal(wd_list, total, sep, expected):
|
180 | 185 | def test_get_packed_offsets_equal_total_none_sep_none():
|
181 | 186 | with pytest.raises(ValueError):
|
182 | 187 | _get_packed_offsets([(1, 0)] * 3, total=None, sep=None, mode='equal')
|
| 188 | + |
| 189 | + |
| 190 | +def test_annotationbbox_extents(): |
| 191 | + plt.rcParams.update(plt.rcParamsDefault) |
| 192 |
8000
+ fig, ax = plt.subplots(figsize=(4, 3), dpi=100) |
| 193 | + |
| 194 | + ax.axis([0, 1, 0, 1]) |
| 195 | + |
| 196 | + an1 = ax.annotate("Annotation", xy=(.9, .9), xytext=(1.1, 1.1), |
| 197 | + arrowprops=dict(arrowstyle="->"), clip_on=False, |
| 198 | + va="baseline", ha="left") |
| 199 | + |
| 200 | + da = DrawingArea(20, 20, 0, 0, clip=True) |
| 201 | + p = mpatches.Circle((-10, 30), 32) |
| 202 | + da.add_artist(p) |
| 203 | + |
| 204 | + ab3 = AnnotationBbox(da, [.5, .5], xybox=(-0.2, 0.5), xycoords='data', |
| 205 | + boxcoords="axes fraction", box_alignment=(0., .5), |
| 206 | + arrowprops=dict(arrowstyle="->")) |
| 207 | + ax.add_artist(ab3) |
| 208 | + |
| 209 | + im = OffsetImage(np.random.rand(10, 10), zoom=3) |
| 210 | + im.image.axes = ax |
| 211 | + ab6 = AnnotationBbox(im, (0.5, -.3), xybox=(0, 75), |
| 212 | + xycoords='axes fraction', |
| 213 | + boxcoords="offset points", pad=0.3, |
| 214 | + arrowprops=dict(arrowstyle="->")) |
| 215 | + ax.add_artist(ab6) |
| 216 | + |
| 217 | + fig.canvas.draw() |
| 218 | + renderer = fig.canvas.get_renderer() |
| 219 | + |
| 220 | + # Test Annotation |
| 221 | + bb1w = an1.get_window_extent(renderer) |
| 222 | + bb1e = an1.get_tightbbox(renderer) |
| 223 | + |
| 224 | + target1 = [332.9, 242.8, 467.0, 298.9] |
| 225 | + assert_allclose(bb1w.extents, target1, atol=2) |
| 226 | + assert_allclose(bb1e.extents, target1, atol=2) |
| 227 | + |
| 228 | + # Test AnnotationBbox |
| 229 | + bb3w = ab3.get_window_extent(renderer) |
| 230 | + bb3e = ab3.get_tightbbox(renderer) |
| 231 | + |
| 232 | + target3 = [-17.6, 129.0, 200.7, 167.9] |
| 233 | + assert_allclose(bb3w.extents, target3, atol=2) |
| 234 | + assert_allclose(bb3e.extents, target3, atol=2) |
| 235 | + |
| 236 | + bb6w = ab6.get_window_extent(renderer) |
| 237 | + bb6e = ab6.get_tightbbox(renderer) |
| 238 | + |
| 239 | + target6 = [180.0, -32.0, 230.0, 92.9] |
| 240 | + assert_allclose(bb6w.extents, target6, atol=2) |
| 241 | + assert_allclose(bb6e.extents, target6, atol=2) |
| 242 | + |
| 243 | + # Test bbox_inches='tight' |
| 244 | + buf = io.BytesIO() |
| 245 | + fig.savefig(buf, bbox_inches='tight') |
| 246 | + buf.seek(0) |
| 247 | + shape = plt.imread(buf).shape |
| 248 | + targetshape = (350, 504, 4) |
| 249 | + assert_allclose(shape, targetshape, atol=2) |
| 250 | + |
| 251 | + #test tight layout (only rough testing if extends are inside the figure |
| 252 | + # and greater than axes extends) |
| 253 | + fig.canvas.draw() |
| 254 | + fig.tight_layout() |
| 255 | + fig.canvas.draw() |
| 256 | + bbu = mtransforms.Bbox.union((an1.get_window_extent(renderer), |
| 257 | + ab3.get_window_extent(renderer), |
| 258 | + ab6.get_window_extent(renderer))) |
| 259 | + bbx = ax.get_window_extent(renderer) |
| 260 | + |
| 261 | + assert np.all([bbu.x0 > 0, bbu.x0 < bbx.x0, bbu.x1 < 400, bbu.x1 > bbx.x1, |
| 262 | + bbu.y0 > 0, bbu.y0 < bbx.y0, bbu.y1 < 300, bbu.y1 > bbx.y1, |
| 263 | + bbu.width > bbx.width, bbu.height > bbx.height]) |
0 commit comments