Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,16 +1515,15 @@ class VertexSelector:
Derived classes should override the `process_selected` method to do
something with the picks.
Here is an example which highlights the selected verts with red
circles::
Here is an example which highlights the selected verts with red circles::
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as lines
class HighlightSelected(lines.VertexSelector):
def __init__(self, line, fmt='ro', **kwargs):
lines.VertexSelector.__init__(self, line)
super().__init__(line)
self.markers, = self.axes.plot([], [], fmt, **kwargs)
def process_selected(self, ind, xs, ys):
Expand All @@ -1537,27 +1536,29 @@ def process_selected(self, ind, xs, ys):
selector = HighlightSelected(line)
plt.show()
"""

def __init__(self, line):
"""
Initialize the class with a `.Line2D`. The line should already be
added to an `~.axes.Axes` and should have the picker property set.
Parameters
----------
line : `.Line2D`
The line must already have been added to an `~.axes.Axes` and must
have its picker property set.
"""
if line.axes is None:
raise RuntimeError('You must first add the line to the Axes')

if line.get_picker() is None:
raise RuntimeError('You must first set the picker property '
'of the line')

self.axes = line.axes
self.line = line
self.canvas = self.axes.figure.canvas
self.cid = self.canvas.mpl_connect('pick_event', self.onpick)

self.cid = self.canvas.callbacks._connect_picklable(
'pick_event', self.onpick)
self.ind = set()

canvas = property(lambda self: self.axes.figure.canvas)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was there a reason we needed canvas in the first place?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not have made this public to start with, but e.g. the HighlightSelector example (in the docstring) directly accesses it and it is marginally more convenient to have it there, so let's not bother with deprecating it?


def process_selected(self, ind, xs, ys):
"""
Default "do nothing" implementation of the `process_selected` method.
Expand Down
6 changes: 6 additions & 0 deletions lib/matplotlib/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from matplotlib import cm
from matplotlib.testing.decorators import check_figures_equal
from matplotlib.dates import rrulewrapper
from matplotlib.lines import VertexSelector
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
import matplotlib.figure as mfigure
Expand Down Expand Up @@ -231,3 +232,8 @@ def test_dynamic_norm():
mpl.scale.LogitScale, mpl.colors.Normalize)()
assert type(pickle.loads(pickle.dumps(logit_norm_instance))) \
== type(logit_norm_instance)


def test_vertexselector():
line, = plt.plot([0, 1], picker=True)
pickle.loads(pickle.dumps(VertexSelector(line)))