matplotlib / matplotlib/matplotlib

LineCollection.contains (ab)uses pickradius<=0 as marker for "filled"

Open
#17,281 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

API: consistency keep
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### Bug report

**Bug summary**

When the pickradius of a LineCollection is set to a <=0 value, LineCollection.contains treats the polygon as "closed".

**Code for reproduction**
```python
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
from matplotlib.backend_bases import MouseEvent
import numpy as np

fig, axs = plt.subplots(2)

for ax, pr in zip(axs, [0, 10]):
coll = LineCollection([[(0, 0), (1, 0), (0, 1)]])
ax.add_collection(coll)
ax.set(xlim=(-1, 2), ylim=(-1, 2))
coll.set_pickradius(pr)
for x in np.linspace(*ax.get_xlim())[1:-1]:
for y in np.linspace(*ax.get_ylim())[1:-1]:
x1, y1 = ax.transData.transform((x, y))
contained, _ = coll.contains(
MouseEvent("button_press_event", fig.canvas, x1, y1))
ax.plot(x, y, "o", c="g" if contained else "r", ms=1)

plt.show()
```
**Actual outcome**

![test](https://user-images.githubusercontent.com/1322974/80740851-fdb20600-8b18-11ea-9c38-dff40f285153.png)

Note how on the top LineCollection (using pickradius=0), all points "in" the closed polygon are considered "contained".

**Expected outcome**

Only points exactly on the LineCollection are contained for pickradius = 0, no points are contained for pickradius < 0 (consistently with Line2D).

---

The relevant code is in Line2D.contains:
```python
ind = _path.point_in_path_collection(
mouseevent.x, mouseevent.y, pickradius,
transform.frozen(), paths, self.get_transforms(),
offsets, transOffset, pickradius <= 0, # <--- this last argument is "filled"
self._offset_position)
```

**Matplotlib version**

* Operating system: linux
* Matplotlib version: master, likely since a long time ago
* Matplotlib backend (`print(matplotlib.get_backend())`): any
* Python version: 38
* Jupyter version (if applicable): no
* Other libraries: no

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 LineCollection.contains and compare its behavior with Line2D.contains, especially the call to _path.point_in_path_collection shown in the report. Run the provided reproduction with pickradius values 0, 10, and below 0, then verify that containment matches the stated expected behavior for each value.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.