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

Skip to content

Commit 4632fac

Browse files
committed
Do not set clip path if it exists
1 parent 32b6ebb commit 4632fac

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
@@ -2218,7 +2218,8 @@ def add_artist(self, a):
22182218
self._children.append(a)
22192219
a._remove_method = self._children.remove
22202220
self._set_artist_props(a)
2221-
a.set_clip_path(self.patch)
2221+
if a.get_clip_path() is None:
2222+
a.set_clip_path(self.patch)
22222223
self.stale = True
22232224
return a
22242225

@@ -2426,7 +2427,8 @@ def add_table(self, tab):
24262427
_api.check_isinstance(mtable.Table, tab=tab)
24272428
self._set_artist_props(tab)
24282429
self._children.append(tab)
2429-
tab.set_clip_path(self.patch)
2430+
if tab.get_clip_path() is None:
2431+
tab.set_clip_path(self.patch)
24302432
tab._remove_method = self._children.remove
24312433
return tab
24322434

lib/matplotlib/figure.py

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

511-
if clip:
511+
if clip and artist.get_clip_path() is None:
512512
artist.set_clip_path(self.patch)
513513

514514
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

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
@@ -8455,6 +8455,43 @@ def test_zorder_and_explicit_rasterization():
84558455
fig.savefig(b, format='pdf')
84568456

84578457

8458+
@image_comparison(["preset_clip_paths.png"], remove_text=True, style="mpl20")
8459+
def test_preset_clip_paths():
8460+
fig, ax = plt.subplots()
8461+
8462+
poly = mpl.patches.Polygon(
8463+
[[1, 0], [0, 1], [-1, 0], [0, -1]], facecolor="#ddffdd",
8464+
edgecolor="#00ff00", linewidth=2, alpha=0.5)
8465+
8466+
ax.add_patch(poly)
8467+
8468+
line = mpl.lines.Line2D((-1, 1), (0.5, 0.5), clip_on=True, clip_path=poly)
8469+
line.set_path_effects([patheffects.withTickedStroke()])
8470+
ax.add_artist(line)
8471+
8472+
line = mpl.lines.Line2D((-1, 1), (-0.5, -0.5), color='r', clip_on=True,
8473+
clip_path=poly)
8474+
ax.add_artist(line)
8475+
8476+
poly2 = mpl.patches.Polygon(
8477+
[[-1, 1], [0, 1], [0, -0.25]], facecolor="#beefc0", alpha=0.3,
8478+
edgecolor="#faded0", linewidth=2, clip_on=True, clip_path=poly)
8479+
ax.add_artist(poly2)
8480+
8481+
# When text clipping works, the "Annotation" text should be clipped
8482+
ax.annotate('Annotation', (-0.75, -0.75), xytext=(0.1, 0.75),
8483+
arrowprops={'color': 'k'}, clip_on=True, clip_path=poly)
8484+
8485+
poly3 = mpl.patches.Polygon(
8486+
[[0, 0], [0, 0.5], [0.5, 0.5], [0.5, 0]], facecolor="g", edgecolor="y",
8487+
linewidth=2, alpha=0.3, clip_on=True, clip_path=poly)
8488+
8489+
fig.add_artist(poly3, clip=True)
8490+
8491+
ax.set_xlim(-1, 1)
8492+
ax.set_ylim(-1, 1)
8493+
8494+
84588495
@mpl.style.context('default')
84598496
def test_rc_axes_label_formatting():
84608497
mpl.rcParams['axes.labelcolor'] = 'red'

0 commit comments

Comments
 (0)