diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 2dc4cae18266..4f8a55ec96ee 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -175,6 +175,9 @@ def update_from(self, other): self._us_dashes = other._us_dashes self.set_linewidth(other._linewidth) # also sets dash properties self.set_transform(other.get_data_transform()) + # If the transform of other needs further initialization, then it will + # be the case for this artist too. + self._transformSet = other.is_transform_set() def get_extents(self): """ @@ -585,14 +588,9 @@ def _update(self): if self.props is not None: self.update(self.props) else: - r, g, b, a = colors.to_rgba(self.patch.get_facecolor()) - rho = 0.3 - r = rho * r - g = rho * g - b = rho * b - - self.set_facecolor((r, g, b, 0.5)) - self.set_edgecolor((r, g, b, 0.5)) + color = .3 * np.asarray(colors.to_rgb(self.patch.get_facecolor())) + self.set_facecolor(color) + self.set_edgecolor(color) self.set_alpha(0.5) def _update_transform(self, renderer): diff --git a/lib/matplotlib/tests/test_patches.py b/lib/matplotlib/tests/test_patches.py index da880df39874..f81e066046d4 100644 --- a/lib/matplotlib/tests/test_patches.py +++ b/lib/matplotlib/tests/test_patches.py @@ -5,15 +5,12 @@ from numpy.testing import assert_almost_equal, assert_array_equal import pytest -from matplotlib.patches import Polygon -from matplotlib.patches import Rectangle -from matplotlib.testing.decorators import image_comparison +from matplotlib.patches import Polygon, Rectangle +from matplotlib.testing.decorators import image_comparison, check_figures_equal import matplotlib.pyplot as plt -import matplotlib.patches as mpatches -import matplotlib.collections as mcollections -from matplotlib import path as mpath -from matplotlib import transforms as mtransforms -import matplotlib.style as mstyle +from matplotlib import ( + collections as mcollections, colors as mcolors, patches as mpatches, + path as mpath, style as mstyle, transforms as mtransforms) import sys on_win = (sys.platform == 'win32') @@ -443,3 +440,29 @@ def test_contains_points(): expected = path.contains_points(points, transform, radius) result = ell.contains_points(points) assert np.all(result == expected) + + +# Currently fails with pdf/svg, probably because some parts assume a dpi of 72. +@check_figures_equal(extensions=["png"]) +def test_shadow(fig_test, fig_ref): + xy = np.array([.2, .3]) + dxy = np.array([.1, .2]) + # We need to work around the nonsensical (dpi-dependent) interpretation of + # offsets by the Shadow class... + plt.rcParams["savefig.dpi"] = "figure" + # Test image. + a1 = fig_test.subplots() + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) + shadow = mpatches.Shadow(rect, ox=dxy[0], oy=dxy[1]) + a1.add_patch(rect) + a1.add_patch(shadow) + # Reference image. + a2 = fig_ref.subplots() + rect = mpatches.Rectangle(xy=xy, width=.5, height=.5) + shadow = mpatches.Rectangle( + xy=xy + fig_ref.dpi / 72 * dxy, width=.5, height=.5, + fc=np.asarray(mcolors.to_rgb(rect.get_fc())) * .3, + ec=np.asarray(mcolors.to_rgb(rect.get_fc())) * .3, + alpha=.5) + a2.add_patch(shadow) + a2.add_patch(rect)