|
9 | 9 | from numpy.testing import assert_almost_equal, assert_array_equal
|
10 | 10 | import pytest
|
11 | 11 |
|
12 |
| -from matplotlib.patches import Polygon |
13 |
| -from matplotlib.patches import Rectangle |
| 12 | +from matplotlib.patches import Polygon, Rectangle |
14 | 13 | from matplotlib.testing.decorators import image_comparison
|
15 | 14 | import matplotlib.pyplot as plt
|
16 |
| -import matplotlib.patches as mpatches |
17 |
| -import matplotlib.collections as mcollections |
18 |
| -from matplotlib import path as mpath |
19 |
| -from matplotlib import transforms as mtransforms |
20 |
| -import matplotlib.style as mstyle |
| 15 | +from matplotlib import ( |
| 16 | + collections as mcollections, colors as mcolors, patches as mpatches, |
| 17 | + path as mpath, style as mstyle, transforms as mtransforms) |
21 | 18 |
|
| 19 | +from io import BytesIO |
22 | 20 | import sys
|
23 | 21 | on_win = (sys.platform == 'win32')
|
24 | 22 |
|
@@ -410,3 +408,32 @@ def test_contains_points():
|
410 | 408 | expected = path.contains_points(points, transform, radius)
|
411 | 409 | result = ell.contains_points(points)
|
412 | 410 | assert np.all(result == expected)
|
| 411 | + |
| 412 | + |
| 413 | +def test_shadow(): |
| 414 | + xy = np.array([.24, .26]) |
| 415 | + dxy = np.array([.4, .6]) |
| 416 | + # Test image. |
| 417 | + # We need to work around the nonsensical (dpi-dependent) interpretation of |
| 418 | + # offsets by the Shadow class... |
| 419 | + with plt.rc_context({"savefig.dpi": "figure"}): |
| 420 | + f1, a1 = plt.subplots() |
| 421 | + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) |
| 422 | + shadow = mpatches.Shadow(rect, ox=dxy[0], oy=dxy[1]) |
| 423 | + a1.add_patch(rect) |
| 424 | + a1.add_patch(shadow) |
| 425 | + b1 = BytesIO() |
| 426 | + f1.savefig(b1, format="raw") |
| 427 | + # Reference image. |
| 428 | + f2, a2 = plt.subplots() |
| 429 | + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) |
| 430 | + shadow = mpatches.Rectangle( |
| 431 | + xy=xy + f2.dpi / 72 * dxy, width=.5, height=.5, |
| 432 | + fc=np.asarray(mcolors.to_rgb(rect.get_fc())) * .3, |
| 433 | + ec=np.asarray(mcolors.to_rgb(rect.get_ec())) * .3, |
| 434 | + alpha=.5) |
| 435 | + a2.add_patch(shadow) |
| 436 | + a2.add_patch(rect) |
| 437 | + b2 = BytesIO() |
| 438 | + f2.savefig(b2, format="raw") |
| 439 | + assert b1.getvalue() == b2.getvalue() |
0 commit comments