matplotlib / matplotlib/matplotlib

Automated clip path generation should sometimes use intersection

Open
#25,598 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: path handling
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

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](https://user-images.githubusercontent.com/8114497/222954956-039a40bf-f1d7-44f8-928d-18d5aa525bbe.png)

while current master gives

![image](https://user-images.githubusercontent.com/8114497/222954975-34e85762-fcaf-45df-a987-6be076eeea2a.png)

or

![image](https://user-images.githubusercontent.com/8114497/222954984-80dd8300-5a3f-4782-a3d6-026c5b4f4c25.png)

_Originally posted by @oscargus in https://github.com/matplotlib/matplotlib/pull/23199#discussion_r1125638275_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example in the issue and inspect the clip handling changed by #23199, especially the path used by Figure.add_artist(..., clip=True). Compare the existing figure clip path with an explicitly set clip path; done means the rendered result is clipped to their intersection without changing unaffected cases, with regression coverage for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.