matplotlib / matplotlib/matplotlib
Automated clip path generation should sometimes use intersection
Nobody has claimed this yet.
- 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

while current master gives

or

_Originally posted by @oscargus in https://github.com/matplotlib/matplotlib/pull/23199#discussion_r1125638275_
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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