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

Skip to content

Commit dd2cfee

Browse files
committed
Add tests for Shadow class.
1 parent a3de84c commit dd2cfee

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
@@ -627,14 +627,10 @@ def _update(self):
627627
if self.props is not None:
628628
self.update(self.props)
629629
else:
630-
r, g, b, a = colors.to_rgba(self.patch.get_facecolor())
631-
rho = 0.3
632-
r = rho * r
633-
g = rho * g
634-
b = rho * b
635-
636-
self.set_facecolor((r, g, b, 0.5))
637-
self.set_edgecolor((r, g, b, 0.5))
630+
self.set_facecolor(
631+
.3 * np.asarray(colors.to_rgb(self.patch.get_facecolor())))
632+
self.set_edgecolor(
633+
.3 * np.asarray(colors.to_rgb(self.patch.get_edgecolor())))
638634
self.set_alpha(0.5)
639635

640636
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
@@ -10,16 +10,14 @@
1010
from numpy.testing import assert_almost_equal, assert_array_equal
1111
import pytest
1212

13-
from matplotlib.patches import Polygon
14-
from matplotlib.patches import Rectangle
13+
from matplotlib.patches import Polygon, Rectangle
1514
from matplotlib.testing.decorators import image_comparison
1615
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)
2219

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

@@ -350,3 +348,32 @@ def test_units_rectangle():
350348
ax.add_patch(p)
351349
ax.set_xlim([4*U.km, 7*U.km])
352350
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

Comments
 (0)