8000 Do not set clip path if it exists by oscargus · Pull Request #23199 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Do not set clip path if it exists #23199

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

Merged
merged 1 commit into from
Mar 31, 2023

Conversation

oscargus
Copy link
Member
@oscargus oscargus commented Jun 3, 2022

PR Summary

This fixes parts of #8270 and in general it does not update the clip path of an artist added with add_artist if it already have a clip path set. Although that is not enough for #8270 (depending on backend).

See: #8270 (comment)

Edit: more cases are added where a new clip path is not added if one is already set. I guess that in general one would like to have a union of the clip paths? Is that easily obtainable in some way?

Tests will be added.

PR Checklist

Tests and Styling

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • [N/A] New features are documented, with examples if plot related.
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • [N/A] Documentation is sphinx and numpydoc compliant (the docs should build without error).

@oscargus
Copy link
Member Author
oscargus commented Jun 3, 2022

Illustration

import matplotlib as mpl
from matplotlib import pyplot as plt

fig, ax = plt.subplots()

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)

line = mpl.lines.Line2D((-1, 1), (0.5, 0.5), clip_on=True, clip_path=poly)
ax.add_artist(line)

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

gives on current main:
image

with this PR:
image

(no, there is no need to do add_artist for a line, but since text doesn't work, a line is simple to use to illustrate the problem with the current code.)

@@ -686,7 +687,7 @@ def annotate(self, text, xy, xytext=None, xycoords='data', textcoords=None,
textcoords=textcoords, arrowprops=arrowprops,
annotation_clip=annotation_clip, **kwargs)
a.set_transform(mtransforms.IdentityTransform())
if 'clip_on' in kwargs:
if kwargs.get('clip_on', False) and a.get_clip_path() is None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that clip_on not only should be in kwargs, but also be set to True?

@@ -369,7 +369,7 @@ def draw_path(self, renderer, gc, tpath, affine, rgbFace):
self.patch.set_transform(affine + self._offset_transform(renderer))
self.patch.set_clip_box(gc.get_clip_rectangle())
clip_path = gc.get_clip_path()
if clip_path:
if clip_path and self.patch.get_clip_path() is None:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is bit doubtful...

@oscargus oscargus changed the title Do not set clip path in add_artist if it exists Do not set clip path if it exists Jun 24, 2022
@greglucas
Copy link
Contributor

@oscargus is there a reason you have this in draft? I think it looks good! The only thing that could be added would be a quick test. Might be able to do a check_figures_equal test with that line through a rectangle and the other with the line just set to the extents of the rectangle.

@greglucas
Copy link
Contributor

Also if you are interested in looking at the text portion, this PR seemed like a good start that could be reworked: #10811

@oscargus
Copy link
Member Author
oscargus commented Mar 4, 2023

I think it was probably the test issue. Which ideally should be testing all of the modifications and I may not really know how PathEffects work.

Not sure that what you suggest will work, at least not with a rotated rectangle, as the "shorter" line must be quite exact to pass the test.

Will rebase and see what happens for a start.

@oscargus oscargus force-pushed the donotsetnewclippath branch 2 times, most recently from 24c3802 to 1d2b56d Compare March 4, 2023 15:15
@oscargus
Copy link
Member Author
oscargus commented Mar 4, 2023

This is the added test image on main:
image

@oscargus oscargus force-pushed the donotsetnewclippath branch from 1d2b56d to 4632fac Compare March 4, 2023 15:18
@oscargus oscargus marked this pull request as ready for review March 4, 2023 15:19
@@ -8455,6 +8455,43 @@ def test_zorder_and_explicit_rasterization():
fig.savefig(b, format='pdf')


@image_comparison(["preset_clip_paths.png"], remove_text=True, style="mpl20")
def test_preset_clip_paths():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to add an image here that we know is wrong just to have to change it again later?

Some possible alternatives:

  • Adding all of the lines/patches completely outside of the rectangle so they are completely clipped? This should make it simpler to compare against a plain rectangle in a check_figures_equal comparison.
  • Removing the annotation for now and adding one outside the axes later once that gets fixed.
  • Use a Cairo backend if the text clipping works properly there...
  • Perhaps simplest though, can you check the expected clip_path after you set it with artists.get_clip_path() is axes.patch, artist.get_clip_path() is polygon for the various cases.

Copy link
Member Author
@oscargus oscargus Mar 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comparing get_clip_path doesn't seem to work:

 assert <matplotlib.transforms.TransformedPatchPath object at 0x000002BE4A717F70> is <matplotlib.patches.Polygon object at 0x000002BE4A5465E0>

Removing annotation and then adding it doesn't really seem better? Now, the test will fail once it is fixed so one will notice this and automatically have to update it (big risk that this test will not be updated.

For patheffects, this happens at draw time, so that will not be tested (not 100% sure that it is now as I do not fully understand that part).

I'd take it that one actually would like to take the intersection of e.g. the axes patch and the provided clip path (although I am not sure if that will ever become a problem). So even if the get_clip_path approach did work, it would break if this was fixed.

I think this is an example that shows that it is better to use the intersection:

import matplotlib as mpl
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
poly3 = mpl.patches.Polygon([[-0.5, 0], [-0.5, 0.5], [0.5, 0.5], [0.5, 0]],
                           facecolor="g", edgecolor="y", linewidth=2, alpha=0.3)

fig.add_artist(poly3, clip=True)

line = mpl.lines.Line2D((-1, 1), (0.25, 0.25), color='r', clip_on=True, clip_path=poly3)
ax.add_artist(line)
# or
# fig.add_artist(line, clip=True)

With this PR one gets
image

while current master gives

image

or

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I totally follow these recent images... The first one looks like the Polygon isn't actually containing the entire line which I would have expected from the clip, so is there still an error present?

The images from main seem to show that the default clip paths are the axes and figure respectively which I expected.

Comparing get_clip_path doesn't seem to work:

Ahh, yes, if you look at the def set_clip_path implementation, I think you can just call TransformedPathPatch(artist)?

Sorry for sending you down this rabbit hole :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one looks like the Polygon isn't actually containing the entire line which I would have expected from the clip, so is there still an error present?

This is a new error. Although probably this case is artificial, it is the consequence of not setting the previous default clip. So as long as we cannot do an intersection of the clip paths, we have to select which error we get.

Then the question is if these compare the same? Will have to look, but pretty sure that one will have to overload the __eq__ method here. Or extract the path. (I guess is will not work as they will not be the same object.)

Copy link
Contributor
@greglucas greglucas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not hold this up on the test image. It is a useful change.

@greglucas greglucas merged commit a844eca into matplotlib:main Mar 31, 2023
@QuLogic QuLogic added this to the v3.8.0 milestone Mar 31, 2023
@oscargus oscargus deleted the donotsetnewclippath branch April 1, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0