From 89846f1a1cf7638dd2aa34685b9ac18f181662be Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 15 Oct 2017 23:30:04 -0700 Subject: [PATCH 1/2] Don't mark a patch transform as set if the parent transform is not set. When updating a patch from another artist, if the other artist's transform is marked as "uninitialized" (`_transformSet == False`), then don't mark the updatee's transform as set either. Typically, this occurs if the updator has not been added to an Axes yet; its transform will be set when that adding occurs. In that case, the updatee's transform also needs to be set when actually being added to an Axes. --- lib/matplotlib/patches.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 2dc4cae18266..0d7efe0e064d 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): """ From f4d158d448f6c3e6fb692d58d67efc5f1f8ec8f3 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 21 Oct 2017 00:32:20 -0700 Subject: [PATCH 2/2] Add tests for Shadow class. --- lib/matplotlib/patches.py | 11 +++----- lib/matplotlib/tests/test_patches.py | 39 ++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/patches.py b/lib/matplotlib/patches.py index 0d7efe0e064d..4f8a55ec96ee 100644 --- a/lib/matplotlib/patches.py +++ b/lib/matplotlib/patches.py @@ -588,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)