Skip to content

Automated clip path generation should sometimes use intersection #25598

Open
@oscargus

Description

@oscargus

In #23199 a change was introduced that does not overwrite the clip path if already set. There is one corner case that generates a new problem. Figure.add_artist(..., clip=True) used to clip based on "figure", but there is a (possibly artificial) case where it would be better to add an intersection of the figure and the set clip path. I guess that in general, it can be useful to have polygon intersection support, although it is a non-trivial case where we may want to rely on some external library. There are probably several issues that benefit from this, but at least #23454.

However, for this specific case, it may be a bit easier since we know that at least the figure is convex (a box) so it may be possible to solve it separately.


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

Originally posted by @oscargus in #23199 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions