Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f99506f

Browse files
committed
Add tests for Shadow class.
1 parent 428bda7 commit f99506f

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

lib/matplotlib/patches.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,10 @@ def _update(self):
615615
if self.props is not None:
616616
self.update(self.props)
617617
else:
618-
r, g, b, a = colors.to_rgba(self.patch.get_facecolor())
619-
rho = 0.3
620-
r = rho * r
621-
g = rho * g
622-
b = rho * b
623-
624-
self.set_facecolor((r, g, b, 0.5))
625-
self.set_edgecolor((r, g, b, 0.5))
618+
self.set_facecolor(
619+
.3 * np.asarray(colors.to_rgb(self.patch.get_facecolor())))
620+
self.set_edgecolor(
621+
.3 * np.asarray(colors.to_rgb(self.patch.get_edgecolor())))
626622
self.set_alpha(0.5)
627623

628624
def _update_transform(self, renderer):

lib/matplotlib/tests/test_patches.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
from numpy.testing import assert_almost_equal, assert_array_equal
1010
import pytest
1111

12-
from matplotlib.patches import Polygon
13-
from matplotlib.patches import Rectangle
12+
from matplotlib.patches import Polygon, Rectangle
1413
from matplotlib.testing.decorators import image_comparison
1514
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)
2118

19+
from io import BytesIO
2220
import sys
2321
on_win = (sys.platform == 'win32')
2422

@@ -410,3 +408,32 @@ def test_contains_points():
410408
expected = path.contains_points(points, transform, radius)
411409
result = ell.contains_points(points)
412410
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

Comments
 (0)