Skip to content

Commit

Permalink
Merge pull request matplotlib#23403 from anntzer/vsp
Browse files Browse the repository at this point in the history
Small cleanup to VertexSelector.
  • Loading branch information
QuLogic authored Jul 11, 2022
2 parents 8dc14e0 + a5998a5 commit fe870f5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
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)

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)))

0 comments on commit fe870f5

Please sign in to comment.