|
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 |
|
@@ -362,3 +360,32 @@ def test_connection_patch():
|
362 | 360 | axesA=ax2, axesB=ax1,
|
363 | 361 | arrowstyle="->")
|
364 | 362 | ax2.add_artist(con)
|
| 363 | + |
| 364 | + |
| 365 | +def test_shadow(): |
| 366 | + xy = np.array([.24, .26]) |
| 367 | + dxy = np.array([.4, .6]) |
| 368 | + # Test image. |
| 369 | + # We need to work around the nonsensical (dpi-dependent) interpretation of |
| 370 | + # offsets by the Shadow class... |
| 371 | + with plt.rc_context({"savefig.dpi": "figure"}): |
| 372 | + f1, a1 = plt.subplots() |
| 373 | + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) |
| 374 | + shadow = mpatches.Shadow(rect, ox=dxy[0], oy=dxy[1]) |
| 375 | + a1.add_patch(rect) |
| 376 | + a1.add_patch(shadow) |
| 377 | + b1 = BytesIO() |
| 378 | + f1.savefig(b1, format="raw") |
| 379 | + # Reference image. |
| 380 | + f2, a2 = plt.subplots() |
| 381 | + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) |
| 382 | + shadow = mpatches.Rectangle( |
| 383 | + xy=xy + f2.dpi / 72 * dxy, width=.5, height=.5, |
| 384 | + fc=np.asarray(mcolors.to_rgb(rect.get_fc())) * .3, |
| 385 | + ec=np.asarray(mcolors.to_rgb(rect.get_ec())) * .3, |
| 386 | + alpha=.5) |
| 387 | + a2.add_patch(shadow) |
| 388 | + a2.add_patch(rect) |
| 389 | + b2 = BytesIO() |
| 390 | + f2.savefig(b2, format="raw") |
| 391 | + assert b1.getvalue() == b2.getvalue() |
0 commit comments