8000 Small cleanup to VertexSelector. · matplotlib/matplotlib@a5998a5 · GitHub
[go: up one dir, main page]

Skip to content

Commit a5998a5

Browse files
committed
Small cleanup to VertexSelector.
Make the class picklable, by making `canvas` a property and using a picklable callback connect. Also some docstring fixes.
1 parent 165df1b commit a5998a5

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

lib/matplotlib/lines.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,16 +1515,15 @@ class VertexSelector:
15151515
Derived classes should override the `process_selected` method to do
15161516
something with the picks.
15171517
1518-
Here is an example which highlights the selected verts with red
1519-
circles::
1518+
Here is an example which highlights the selected verts with red circles::
15201519
15211520
import numpy as np
15221521
import matplotlib.pyplot as plt
15231522
import matplotlib.lines as lines
15241523
15251524
class HighlightSelected(lines.VertexSelector):
15261525
def __init__(self, line, fmt='ro', **kwargs):
1527-
lines.VertexSelector.__init__(self, line)
1526+
super().__init__(line)
15281527
self.markers, = self.axes.plot([], [], fmt, **kwargs)
15291528
15301529
def process_selected(self, ind, xs, ys):
@@ -1537,27 +1536,29 @@ def process_selected(self, ind, xs, ys):
15371536
15381537
selector = HighlightSelected(line)
15391538
plt.show()
1540-
15411539
"""
1540+
15421541
def __init__(self, line):
15431542
"""
1544-
Initialize the class with a `.Line2D`. The line should already be
1545-
added to an `~.axes.Axes` and should have the picker property set.
1543+
Parameters
1544+
----------
1545+
line : `.Line2D`
1546+
The line must already have been added to an `~.axes.Axes` and must
1547+
have its picker property set.
15461548
"""
15471549
if line.axes is None:
15481550
raise RuntimeError('You must first add the line to the Axes')
1549-
15501551
if line.get_picker() is None:
15511552
raise RuntimeError('You must first set the picker property '
15521553
'of the line')
1553-
15541554
self.axes = line.axes
15551555
self.line = line
1556-
self.canvas = self.axes.figure.canvas
1557-
self.cid = self.canvas.mpl_connect('pick_event', self.onpick)
1558-
1556+
self.cid = self.canvas.callbacks._connect_picklable(
1557+
'pick_event', self.onpick)
15591558
self.ind = set()
15601559

1560+
canvas = property(lambda self: self.axes.figure.canvas)
1561+
15611562
def process_selected(self, ind, xs, ys):
15621563
"""
15631564
Default "do nothing" implementation of the `process_selected` method.

lib/matplotlib/tests/test_pickle.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from matplotlib import cm
99
from matplotlib.testing.decorators import check_figures_equal
1010
from matplotlib.dates import rrulewrapper
11+
from matplotlib.lines import VertexSelector
1112
import matplotlib.pyplot as plt
1213
import matplotlib.transforms as mtransforms
1314
import matplotlib.figure as mfigure
@@ -231,3 +232,8 @@ def test_dynamic_norm():
231232
mpl.scale.LogitScale, mpl.colors.Normalize)()
232233
assert type(pickle.loads(pickle.dumps(logit_norm_instance))) \
233234
== type(logit_norm_instance)
235+
236+
237+
def test_vertexselector():
238+
line, = plt.plot([0, 1], picker=True)
239+
pickle.loads(pickle.dumps(VertexSelector(line)))

0 commit comments

Comments
 (0)
0