|
10 | 10 | from numpy.testing import assert_almost_equal, assert_array_equal
|
11 | 11 | import pytest
|
12 | 12 |
|
13 |
| -from matplotlib.patches import Polygon |
14 |
| -from matplotlib.patches import Rectangle |
| 13 | +from matplotlib.patches import Polygon, Rectangle |
15 | 14 | from matplotlib.testing.decorators import image_comparison
|
16 | 15 | import matplotlib.pyplot as plt
|
17 |
| -import matplotlib.patches as mpatches |
18 |
| -import matplotlib.collections as mcollections |
19 |
| -from matplotlib import path as mpath |
20 |
| -from matplotlib import transforms as mtransforms |
21 |
| -import matplotlib.style as mstyle |
| 16 | +from matplotlib import ( |
| 17 | + collections as mcollections, colors as mcolors, patches as mpatches, |
| 18 | + path as mpath, style as mstyle, transforms as mtransforms) |
22 | 19 |
|
| 20 | +from io import BytesIO |
23 | 21 | import sys
|
24 | 22 | on_win = (sys.platform == 'win32')
|
25 | 23 |
|
@@ -350,3 +348,32 @@ def test_units_rectangle():
|
350 | 348 | ax.add_patch(p)
|
351 | 349 | ax.set_xlim([4*U.km, 7*U.km])
|
352 | 350 | ax.set_ylim([5*U.km, 9*U.km])
|
| 351 | + |
| 352 | + |
| 353 | +def test_shadow(): |
| 354 | + xy = np.array([.24, .26]) |
| 355 | + dxy = np.array([.4, .6]) |
| 356 | + # Test image. |
| 357 | + # We need to work around the nonsensical (dpi-dependent) interpretation of |
| 358 | + # offsets by the Shadow class... |
| 359 | + with plt.rc_context({"savefig.dpi": "figure"}): |
| 360 | + f1, a1 = plt.subplots() |
| 361 | + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) |
| 362 | + shadow = mpatches.Shadow(rect, ox=dxy[0], oy=dxy[1]) |
| 363 | + a1.add_patch(rect) |
| 364 | + a1.add_patch(shadow) |
| 365 | + b1 = BytesIO() |
| 366 | + f1.savefig(b1, format="raw") |
| 367 | + # Reference image. |
| 368 | + f2, a2 = plt.subplots() |
| 369 | + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) |
| 370 | + shadow = mpatches.Rectangle( |
| 371 | + xy=xy + f2.dpi / 72 * dxy, width=.5, height=.5, |
| 372 | + fc=np.asarray(mcolors.to_rgb(rect.get_fc())) * .3, |
| 373 | + ec=np.asarray(mcolors.to_rgb(rect.get_ec())) * .3, |
| 374 | + alpha=.5) |
| 375 | + a2.add_patch(shadow) |
| 376 | + a2.add_patch(rect) |
| 377 | + b2 = BytesIO() |
| 378 | + f2.savefig(b2, format="raw") |
| 379 | + assert b1.getvalue() == b2.getvalue() |
0 commit comments