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

Skip to content

text is not clipped by clip_path #8270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fruchart opened this issue Mar 11, 2017 · 4 comments
Open

text is not clipped by clip_path #8270

fruchart opened this issue Mar 11, 2017 · 4 comments
Labels
keep Items to be ignored by the “Stale” Github Action topic: text

Comments

@fruchart
Copy link

Bug report

Bug summary

I expect that a matplotlib.text.Text object should disappear when clipped by setting clip_path=some_path. In practice, the text is still here.

Code for reproduction

import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt

plt.figure(figsize=(8,8))
ax = plt.gca()

poly = mpl.patches.Polygon([[1,0], [0,1], [-1,0], [0,-1]], 
                           facecolor="#ddffdd", edgecolor="#00ff00", linewidth=2, alpha=0.5)

ax.add_patch(poly)

# should not be displayed ...
txt_outside = mpl.text.Text(0.75,0.75,"outside :(", clip_on=True, clip_path=poly)
ax.add_artist(txt_outside)

# should be (and is) displayed
txt_inside = mpl.text.Text(0.,0.,"inside :)", clip_on=True, clip_path=poly)
ax.add_artist(txt_inside)

# works perfectly
scatter = plt.scatter(*np.transpose([[0.1,0.1],[0.7,0.7]]), zorder=5, clip_path=poly)

ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
plt.show()

Actual outcome

image

Expected outcome

I would expect the text outside :( not to appear on the image.

Matplotlib version

This happens with matplotlib 2.0.0 with python 3.6.0 (installed with anaconda on linux). It also happens with matplotlib 1.5.3 with python 3.5.2 (same setup).

@anntzer
Copy link
Contributor

anntzer commented Jul 28, 2017

The first culprit occurs in _AxesBase.add_artist

    def add_artist(self, a):
        ....
        a.axes = self
        self.artists.append(a)
        self._set_artist_props(a)
        a.set_clip_path(self.patch) # <-- oops
        a._remove_method = lambda h: self.artists.remove(h)
        self.stale = True
        return a

or, if calling ax.text directly, at

    def text(...):
        ...
                t.set_clip_path(self.patch)
        self._add_text(t)
        return t

i.e., when adding the text object to the axes, the clip path is simply dropped, and replaced by the axes clip path. (I think we should just not set the clip path if it is already set.)

But even delaying the setting of the clip path until after the artist is added to the axes does not seem to help. I suspect there are some issues with Artist.set_clip_path, which is handling a bunch of different cases, but am not certain of that.

@anntzer
Copy link
Contributor

anntzer commented May 13, 2020

As commented above

diff --git i/lib/matplotlib/axes/_base.py w/lib/matplotlib/axes/_base.py
index aec405415..4206956d7 100644
--- i/lib/matplotlib/axes/_base.py
+++ w/lib/matplotlib/axes/_base.py
@@ -1898,7 +1898,8 @@ class _AxesBase(martist.Artist):
         self.artists.append(a)
         a._remove_method = self.artists.remove
         self._set_artist_props(a)
-        a.set_clip_path(self.patch)
+        if a.get_clip_path() is None:
+            a.set_clip_path(self.patch)
         self.stale = True
         return a

is a first necessary step to fix this (this is consistent with e.g. add_line(); there's a few other places (e.g. add_table(), text(), etc.) where we call set_clip_path without checking a previously existing clip path and should likewise be fixed.
This is sufficient to make things work with mplcairo, but not with agg.

@github-actions
Copy link

github-actions bot commented Apr 9, 2023

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Apr 9, 2023
@anntzer
Copy link
Contributor

anntzer commented Apr 9, 2023

#8270 (comment) has been integrated, but as noted this is not enough for agg.

@oscargus oscargus added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Apr 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep Items to be ignored by the “Stale” Github Action topic: text
Projects
None yet
Development

No branches or pull requests

4 participants