8000 Merge pull request #23199 from oscargus/donotsetnewclippath · matplotlib/matplotlib@a844eca · GitHub
[go: up one dir, main page]

Skip to content

Commit a844eca

Browse files
authored
Merge pull request #23199 from oscargus/donotsetnewclippath
Do not set clip path if it exists
2 parents 83afb15 + 4632fac commit a844eca

File tree

6 files changed

+47
-7
lines changed

6 files changed

+47
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,8 @@ def text(self, x, y, s, fontdict=None, **kwargs):
687687
**kwargs,
688688
}
689689
t = mtext.Text(x, y, text=s, **effective_kwargs)
690-
t.set_clip_path(self.patch)
690+
if t.get_clip_path() is None:
691+
t.set_clip_path(self.patch)
691692
self._add_text(t)
692693
return t
693694

@@ -700,7 +701,7 @@ def annotate(self, text, xy, xytext=None, xycoords='data', textcoords=None,
700701
textcoords=textcoords, arrowprops=arrowprops,
701702
annotation_clip=annotation_clip, **kwargs)
702703
a.set_transform(mtransforms.IdentityTransform())
703-
if 'clip_on' in kwargs:
704+
if kwargs.get('clip_on', False) and a.get_clip_path() is None:
704705
a.set_clip_path(self.patch)
705706
self._add_text(a)
706707
return a

lib/matplotlib/axes/_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,8 @@ def add_artist(self, a):
22152215
self._children.append(a)
22162216
a._remove_method = self._children.remove
22172217
self._set_artist_props(a)
2218-
a.set_clip_path(self.patch)
2218+
if a.get_clip_path() is None:
2219+
a.set_clip_path(self.patch)
22192220
self.stale = True
22202221
return a
22212222

@@ -2423,7 +2424,8 @@ def add_table(self, tab):
24232424
_api.check_isinstance(mtable.Table, tab=tab)
24242425
self._set_artist_props(tab)
24252426
self._children.append(tab)
2426-
tab.set_clip_path(self.patch)
2427+
if tab.get_clip_path() is None:
2428+
tab.set_clip_path(self.patch)
24272429
tab._remove_method = self._children.remove
24282430
return tab
24292431

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def add_artist(self, artist, clip=False):
505505
if not artist.is_transform_set():
506506
artist.set_transform(self.transSubfigure)
507507

508-
if clip:
508+
if clip and artist.get_clip_path() is None:
509509
artist.set_clip_path(self.patch)
510510

511511
self.stale = True

lib/matplotlib/patheffects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
368368
self.patch.set_transform(affine + self._offset_transform(renderer))
369369
self.patch.set_clip_box(gc.get_clip_rectangle())
370370
clip_path = gc.get_clip_path()
371-
if clip_path:
371+
if clip_path and self.patch.get_clip_path() is None:
372372
self.patch.set_clip_path(*clip_path)
373373
self.patch.draw(renderer)
374374

Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import matplotlib
2020
import matplotlib as mpl
21-
from matplotlib import rc_context
21+
from matplotlib import rc_context, patheffects
2222
from matplotlib._api import MatplotlibDeprecationWarning
2323
import matplotlib.colors as mcolors
2424
import matplotlib.dates as mdates
@@ -8441,6 +8441,43 @@ def test_zorder_and_explicit_rasterization():
84418441
fig.savefig(b, format='pdf')
84428442

84438443

8444+
@image_comparison(["preset_clip_paths.png"], remove_text=True, style="mpl20")
8445+
def test_preset_clip_paths():
8446+
fig, ax = plt.subplots()
8447+
8448+
poly = mpl.patches.Polygon(
8449+
[[1, 0], [0, 1], [-1, 0], [0, -1]], facecolor="#ddffdd",
8450+
edgecolor="#00ff00", linewidth=2, alpha=0.5)
8451+
8452+
ax.add_patch(poly)
8453+
8454+
line = mpl.lines.Line2D((-1, 1), (0.5, 0.5), clip_on=True, clip_path=poly)
8455+
line.set_path_effects([patheffects.withTickedStroke()])
8456+
ax.add_artist(line)
8457+
8458+
line = mpl.lines.Line2D((-1, 1), (-0.5, -0.5), color='r', clip_on=True,
8459+
clip_path=poly)
8460+
ax.add_artist(line)
8461+
8462+
poly2 = mpl.patches.Polygon(
8463+
[[-1, 1], [0, 1], [0, -0.25]], facecolor="#beefc0", alpha=0.3,
8464+
edgecolor="#faded0", linewidth=2, clip_on=True, clip_path=poly)
8465+
ax.add_artist(poly2)
8466+
8467+
# When text clipping works, the "Annotation" text should be clipped
8468+
ax.annotate('Annotation', (-0.75, -0.75), xytext=(0.1, 0.75),
8469+
arrowprops={'color': 'k'}, clip_on=True, clip_path=poly)
8470+
8471+
poly3 = mpl.patches.Polygon(
8472+
[[0, 0], [0, 0.5], [0.5, 0.5], [0.5, 0]], facecolor="g", edgecolor="y",
8473+
linewidth=2, alpha=0.3, clip_on=True, clip_path=poly)
8474+
8475+
fig.add_artist(poly3, clip=True)
8476+
8477+
ax.set_xlim(-1, 1)
8478+
ax.set_ylim(-1, 1)
8479+
8480+
84448481
@mpl.style.context('default')
84458482
def test_rc_axes_label_formatting():
84468483
mpl.rcParams['axes.labelcolor'] = 'red'

0 commit comments

Comments
 (0)
0