matplotlib / matplotlib/matplotlib

PatchCollection edges incorrect when clip path is set

Open
#15,946 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### Bug report

**Bug summary**

Cartopy has a mostly-superfluous `GeoAxes.background_patch` which I'm trying to convert to the standard `Axes.patch`. By default, the `Axes.patch` is a `Rectangle`, which triggers a fast-path to set a clip box instead of a clip path. However, since most map boundaries are non-square, the full clip path code is used instead. When doing so, this causes some strange artifacts.

(Un)Fortunately, it can be reproduced with plain Matplotlib below.

**Code for reproduction**

```python
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
from matplotlib.path import Path

pth = Path([[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]],
[1, 2, 2, 2, 79])

fig, ax = plt.subplots(2, 2, sharex=True, sharey=True)
ax[0, 0].set_xlim(-1, 2)
ax[0, 0].set_ylim(-1, 2)

for a in ax[0]:
collection = a.add_collection(
PatchCollection([mpatches.PathPatch(pth)],
facecolor='C0', edgecolor='k'))

collection.set_clip_path(
Path([[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]]),
a.transAxes)

for a in ax[1]:
patch = a.add_patch(mpatches.PathPatch(pth, facecolor='C0', edgecolor='k'))

patch.set_clip_path(
Path([[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]]),
a.transAxes)

ax[0, 0].set_ylabel('PatchCollection')
ax[1, 0].set_ylabel('PathPatch')
ax[1, 0].set_xlabel('Default clip box')
ax[1, 1].set_xlabel('Custom clip path')

plt.savefig('test.png')
```

**Actual outcome**

In the bottom row is a plain `PathPatch`, and the top row is a `PatchCollection` containing the same `PathPatch`. The left column shows the default clipping (to the clip box of the `Axes`). The right column uses a clip path of a manually-specified 0-1 rectangle in `Axes` space, so it should be equivalent.

But the horizontal edges are either doubled over or the wrong width in the top-right case.

![test](https://user-images.githubusercontent.com/302469/70871924-e7444d00-1f71-11ea-816d-c8a04e9019ea.png)

**Expected outcome**

All path edges should look the same.

**Matplotlib version**

* Operating system: Fedora 30
* Matplotlib version: 3.1.3 and master, 9d00ca839e5faf10257095ad1e2bfb5296549d47
* Matplotlib backend (`print(matplotlib.get_backend())`): TkAgg (probably only the Agg bit is relevant)
* Python version: 3.6.9 / 3.6.3

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 PatchCollection.set_clip_path and compare its rendering path with the PathPatch case in the supplied reproduction. Run the example with the reported versions or a current checkout, then verify that the PatchCollection's clipped horizontal edges match the PathPatch edges and are not doubled or incorrectly sized.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.