Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit addd0ca

Browse files
committed
Deprecate setting Line2D's pickradius via set_picker.
There's set_pickradius just for that purpose; moreover `set_picker(True)` would previously accidentally also set the pickradius to 1 (because True == 1...). Also don't set self._contains in Line2D.set_picker -- this is consistent with all other artist classes.
1 parent 60cb031 commit addd0ca

File tree

4 files changed

+34
-43
lines changed

4 files changed

+34
-43
lines changed

doc/api/next_api_changes/deprecations.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,11 @@ The following parameters do not have any effect and are deprecated:
124124
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125125
This method is deprecated. Use
126126
``ax.dataLim.set(Bbox.union([ax.dataLim, bounds]))`` instead.
127+
128+
Setting `.Line2D`\'s pickradius via `.Line2D.set_picker`
129+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130+
Setting a `.Line2D`\'s pickradius (i.e. the tolerance for pick events and
131+
containment checks) is deprecated. Use `.Line2D.set_pickradius` instead.
132+
133+
`.Line2D.set_picker` no longer sets the artist's custom-contain() check. Use
134+
``Line2D.set_contains`` instead.

examples/event_handling/legend_picking.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,39 @@
55
66
Enable picking on the legend to toggle the original line on and off
77
"""
8+
89
import numpy as np
910
import matplotlib.pyplot as plt
1011

11-
t = np.arange(0.0, 0.2, 0.1)
12-
y1 = 2*np.sin(2*np.pi*t)
13-
y2 = 4*np.sin(2*np.pi*2*t)
12+
13+
t = np.linspace(0, 1)
14+
y1 = 2 * np.sin(2*np.pi*t)
15+
y2 = 4 * np.sin(2*np.pi*2*t)
1416

1517
fig, ax = plt.subplots()
1618
ax.set_title('Click on legend line to toggle line on/off')
17-
line1, = ax.plot(t, y1, lw=2, label='1 HZ')
18-
line2, = ax.plot(t, y2, lw=2, label='2 HZ')
19-
leg = ax.legend(loc='upper left', fancybox=True, shadow=True)
20-
leg.get_frame().set_alpha(0.4)
19+
line1, = ax.plot(t, y1, lw=2, label='1 Hz')
20+
line2, = ax.plot(t, y2, lw=2, label='2 Hz')
21+
leg = ax.legend(fancybox=True, shadow=True)
2122

22-
23-
# we will set up a dict mapping legend line to orig line, and enable
24-
# picking on the legend line
2523
lines = [line1, line2]
26-
lined = dict()
24+
lined = {} # Will legend lines to original lines.
2725
for legline, origline in zip(leg.get_lines(), lines):
28-
legline.set_picker(5) # 5 pts tolerance
26+
legline.set_picker(True) # Enable picking on the legend line.
2927
lined[legline] = origline
3028

3129

3230
def on_pick(event):
33-
# on the pick event, find the orig line corresponding to the
34-
# legend proxy line, and toggle the visibility
31+
# On the pick event, find the original line corresponding to the legend
32+
# proxy line, and toggle its visibility.
3533
legline = event.artist
3634
origline = lined[legline]
37-
vis = not origline.get_visible()
38-
origline.set_visible(vis)
35+
visible = not origline.get_visible()
36+
origline.set_visible(visible)
3937
# Change the alpha on the line in the legend so we can see what lines
40-
# have been toggled
41-
if vis:
42-
legline.set_alpha(1.0)
43-
else:
44-
legline.set_alpha(0.2)
38+
# have been toggled.
39+
legline.set_alpha(1.0 if visible else 0.2)
4540
fig.canvas.draw()
4641

4742
fig.canvas.mpl_connect('pick_event', on_pick)
48-
4943
plt.show()

lib/matplotlib/artist.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ def set_picker(self, picker):
516516
517517
Parameters
518518
----------
519-
picker : None or bool or float or callable
519+
picker : None or bool or callable
520520
This can be one of the following:
521521
522522
- *None*: Picking is disabled for this artist (default).
@@ -525,14 +525,6 @@ def set_picker(self, picker):
525525
artist will fire a pick event if the mouse event is over
526526
the artist.
527527
528-
- A float: If picker is a number it is interpreted as an
529-
epsilon tolerance in points and the artist will fire
530-
off an event if it's data is within epsilon of the mouse
531-
event. For some artists like lines and patch collections,
532-
the artist may provide additional data to the pick event
533-
that is generated, e.g., the indices of the data within
534-
epsilon of the pick event
535-
536528
- A function: If picker is callable, it is a user supplied
537529
function which determines whether the artist is hit by the
538530
mouse event::
@@ -543,6 +535,9 @@ def set_picker(self, picker):
543535
artist, return *hit=True* and props is a dictionary of
544536
properties you want added to the PickEvent attributes.
545537
538+
- For `.Line2D` only, *picker* can also be a float that sets the
539+
tolerance for checking whether an event occurred "on" the line;
540+
this is deprecated. Use `.Line2D.set_pickradius` instead.
546541
"""
547542
self._picker = picker
548543

lib/matplotlib/lines.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,6 @@ def __init__(self, xdata, ydata,
399399
self.update(kwargs)
400400
self.pickradius = pickradius
401401
self.ind_offset = 0
402-
if isinstance(self._picker, Number):
403-
self.pickradius = self._picker
404402

405403
self._xorig = np.asarray([])
406404
self._yorig = np.asarray([])
@@ -603,16 +601,12 @@ def get_markevery(self):
603601
return self._markevery
604602

605603
def set_picker(self, p):
606-
"""Sets the event picker details for the line.
607-
608-
Parameters
609-
----------
610-
p : float or callable[[Artist, Event], Tuple[bool, dict]]
611-
If a float, it is used as the pick radius in points.
612-
"""
613-
if callable(p):
614-
self._contains = p
615-
else:
604+
# docstring inherited
605+
if isinstance(p, Number) and not isinstance(p, bool):
606+
# After deprecation, the whole method can be deleted and inherited.
607+
cbook.warn_deprecated(
608+
"3.3", message="Setting the line's pick radius via set_picker "
609+
"is deprecated; use set_pickradius instead.")
616610
self.pickradius = p
617611
self._picker = p
618612

0 commit comments

Comments
 (0)