|
1 | 1 | import pytest
|
| 2 | +import io |
| 3 | +import numpy as np |
| 4 | +from numpy.testing import assert_allclose |
2 | 5 | from matplotlib.testing.decorators import image_comparison
|
3 | 6 | import matplotlib.pyplot as plt
|
4 | 7 | import matplotlib.patches as mpatches
|
5 | 8 | import matplotlib.lines as mlines
|
6 |
| -from matplotlib.offsetbox import ( |
7 |
| - AnchoredOffsetbox, DrawingArea, _get_packed_offsets) |
| 9 | +import matplotlib.transforms as mtransforms |
| 10 | +from matplotlib.offsetbox import (AnnotationBbox, OffsetImage, |
| 11 | + AnchoredOffsetbox, DrawingArea, |
| 12 | + _get_packed_offsets) |
8 | 13 |
|
9 | 14 |
|
10 | 15 | @image_comparison(baseline_images=['offsetbox_clipping'], remove_text=True)
|
@@ -124,3 +129,79 @@ def test_get_packed_offsets(wd_list, total, sep, mode):
|
124 | 129 | # triggered inside this function when calling higher-level functions
|
125 | 130 | # (e.g. `Axes.legend`).
|
126 | 131 | _get_packed_offsets(wd_list, total, sep, mode=mode)
|
| 132 | + |
| 133 | + |
| 134 | +def test_annotationbbox_extents(): |
| 135 | + plt.rcParams.update(plt.rcParamsDefault) |
| 136 | + fig, ax = plt.subplots(figsize=(4, 3), dpi=100) |
| 137 | + |
| 138 | + ax.axis([0, 1, 0, 1]) |
| 139 | + |
| 140 | + an1 = ax.annotate("Annotation", xy=(.9, .9), xytext=(1.1, 1.1), |
| 141 | + arrowprops=dict(arrowstyle="->"), clip_on=False, |
| 142 | + va="baseline", ha="left") |
| 143 | + |
| 144 | + da = DrawingArea(20, 20, 0, 0, clip=True) |
| 145 | + p = mpatches.Circle((-10, 30), 32) |
| 146 | + da.add_artist(p) |
| 147 | + |
| 148 | + ab3 = AnnotationBbox(da, [.5, .5], xybox=(-0.2, 0.5), xycoords='data', |
| 149 | + boxcoords="axes fraction", box_alignment=(0., .5), |
| 150 | + arrowprops=dict(arrowstyle="->")) |
| 151 | + ax.add_artist(ab3) |
| 152 | + |
| 153 | + im = OffsetImage(np.random.rand(10, 10), zoom=3) |
| 154 | + im.image.axes = ax |
| 155 | + ab6 = AnnotationBbox(im, (0.5, -.3), xybox=(0, 75), |
| 156 | + xycoords='axes fraction', |
| 157 | + boxcoords="offset points", pad=0.3, |
| 158 | + arrowprops=dict(arrowstyle="->")) |
| 159 | + ax.add_artist(ab6) |
| 160 | + |
| 161 | + fig.canvas.draw() |
| 162 | + renderer = fig.canvas.get_renderer() |
| 163 | + |
| 164 | + # Test Annotation |
| 165 | + bb1w = an1.get_window_extent(renderer) |
| 166 | + bb1e = an1.get_tightbbox(renderer) |
| 167 | + |
| 168 | + target1 = [332.9, 242.8, 467.0, 298.9] |
| 169 | + assert_allclose(bb1w.extents, target1, atol=2) |
| 170 | + assert_allclose(bb1e.extents, target1, atol=2) |
| 171 | + |
| 172 | + # Test AnnotationBbox |
| 173 | + bb3w = ab3.get_window_extent(renderer) |
| 174 | + bb3e = ab3.get_tightbbox(renderer) |
| 175 | + |
| 176 | + target3 = [-17.6, 129.0, 200.7, 167.9] |
| 177 | + assert_allclose(bb3w.extents, target3, atol=2) |
| 178 | + assert_allclose(bb3e.extents, target3, atol=2) |
| 179 | + |
| 180 | + bb6w = ab6.get_window_extent(renderer) |
| 181 | + bb6e = ab6.get_tightbbox(renderer) |
| 182 | + |
| 183 | + target6 = [180.0, -32.0, 230.0, 92.9] |
| 184 | + assert_allclose(bb6w.extents, target6, atol=2) |
| 185 | + assert_allclose(bb6e.extents, target6, atol=2) |
| 186 | + |
| 187 | + # Test bbox_inches='tight' |
| 188 | + buf = io.BytesIO() |
| 189 | + fig.savefig(buf, bbox_inches='tight') |
| 190 | + buf.seek(0) |
| 191 | + shape = plt.imread(buf).shape |
| 192 | + targetshape = (350, 504, 4) |
| 193 | + assert_allclose(shape, targetshape, atol=2) |
| 194 | + |
| 195 | + #test tight layout (only rough testing if extends are inside the figure |
| 196 | + # and greater than axes extends) |
| 197 | + fig.canvas.draw() |
| 198 | + fig.tight_layout() |
| 199 | + fig.canvas.draw() |
| 200 | + bbu = mtransforms.Bbox.union((an1.get_window_extent(renderer), |
| 201 | + ab3.get_window_extent(renderer), |
| 202 | + ab6.get_window_extent(renderer))) |
| 203 | + bbx = ax.get_window_extent(renderer) |
| 204 | + |
| 205 | + assert np.all([bbu.x0 > 0, bbu.x0 < bbx.x0, bbu.x1 < 400, bbu.x1 > bbx.x1, |
| 206 | + bbu.y0 > 0, bbu.y0 < bbx.y0, bbu.y1 < 300, bbu.y1 > bbx.y1, |
| 207 | + bbu.width > bbx.width, bbu.height > bbx.height]) |
0 commit comments