Skip to content

Commit 3d19f94

Browse files
authored
Merge pull request #9426 from anntzer/shadow-transform
FIX: Don't mark a patch transform as set if the parent transform is not set.
2 parents c8a48b8 + f4d158d commit 3d19f94

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

lib/matplotlib/patches.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def update_from(self, other):
175175
self._us_dashes = other._us_dashes
176176
self.set_linewidth(other._linewidth) # also sets dash properties
177177
self.set_transform(other.get_data_transform())
178+
# If the transform of other needs further initialization, then it will
179+
# be the case for this artist too.
180+
self._transformSet = other.is_transform_set()
178181

179182
def get_extents(self):
180183
"""
@@ -584,14 +587,9 @@ def _update(self):
584587
if self.props is not None:
585588
self.update(self.props)
586589
else:
587-
r, g, b, a = colors.to_rgba(self.patch.get_facecolor())
588-
rho = 0.3
589-
r = rho * r
590-
g = rho * g
591-
b = rho * b
592-
593-
self.set_facecolor((r, g, b, 0.5))
594-
self.set_edgecolor((r, g, b, 0.5))
590+
color = .3 * np.asarray(colors.to_rgb(self.patch.get_facecolor()))
591+
self.set_facecolor(color)
592+
self.set_edgecolor(color)
595593
self.set_alpha(0.5)
596594

597595
def _update_transform(self, renderer):

lib/matplotlib/tests/test_patches.py

+31-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
from numpy.testing import assert_almost_equal, assert_array_equal
66
import pytest
77

8-
from matplotlib.patches import Polygon
9-
from matplotlib.patches import Rectangle
10-
from matplotlib.testing.decorators import image_comparison
8+
from matplotlib.patches import Polygon, Rectangle
9+
from matplotlib.testing.decorators import image_comparison, check_figures_equal
1110
import matplotlib.pyplot as plt
12-
import matplotlib.patches as mpatches
13-
import matplotlib.collections as mcollections
14-
from matplotlib import path as mpath
15-
from matplotlib import transforms as mtransforms
16-
import matplotlib.style as mstyle
11+
from matplotlib import (
12+
collections as mcollections, colors as mcolors, patches as mpatches,
13+
path as mpath, style as mstyle, transforms as mtransforms)
1714

1815
import sys
1916
on_win = (sys.platform == 'win32')
@@ -443,3 +440,29 @@ def test_contains_points():
443440
expected = path.contains_points(points, transform, radius)
444441
result = ell.contains_points(points)
445442
assert np.all(result == expected)
443+
444+
445+
# Currently fails with pdf/svg, probably because some parts assume a dpi of 72.
446+
@check_figures_equal(extensions=["png"])
447+
def test_shadow(fig_test, fig_ref):
448+
xy = np.array([.2, .3])
449+
dxy = np.array([.1, .2])
450+
# We need to work around the nonsensical (dpi-dependent) interpretation of
451+
# offsets by the Shadow class...
452+
plt.rcParams["savefig.dpi"] = "figure"
453+
# Test image.
454+
a1 = fig_test.subplots()
455+
rect = mpatches.Rectangle(xy=xy, width=.5, height=.5)
456+
shadow = mpatches.Shadow(rect, ox=dxy[0], oy=dxy[1])
457+
a1.add_patch(rect)
458+
a1.add_patch(shadow)
459+
# Reference image.
460+
a2 = fig_ref.subplots()
461+
rect = mpatches.Rectangle(xy=xy, width=.5, height=.5)
462+
shadow = mpatches.Rectangle(
463+
xy=xy + fig_ref.dpi / 72 * dxy, width=.5, height=.5,
464+
fc=np.asarray(mcolors.to_rgb(rect.get_fc())) * .3,
465+
ec=np.asarray(mcolors.to_rgb(rect.get_fc())) * .3,
466+
alpha=.5)
467+
a2.add_patch(shadow)
468+
a2.add_patch(rect)

0 commit comments

Comments
 (0)