Skip to content

Commit d5a12ff

Browse files
committed
add set_position method
1 parent 7ec9970 commit d5a12ff

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

mpl_point_clicker/_clicker.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,30 @@ class in *classes*
103103
self._update_legend_alpha()
104104
self._observers = CallbackRegistry()
105105

106-
def get_positions(self, copy=True):
106+
def get_positions(self):
107107
return {k: np.asarray(v) for k, v in self._positions.items()}
108108

109+
def set_positions(self, positions):
110+
"""
111+
Set the current positions for all classes.
112+
113+
Parameters
114+
----------
115+
positions : dict
116+
A dictionary with strings as keys and 2D array-like values. The values
117+
will be interpreted as (N, 2) with x, y as the columns. The keys in the dictionary
118+
must all be valid classes. If a class is not included in *positions* then the existing
119+
values will not be modified.
120+
"""
121+
# check all keys first so we don't partially overwrite data
122+
for k in positions.keys():
123+
if k not in self._classes:
124+
raise ValueError(f"class {k} is not in {self._classes}")
125+
126+
for k, v in positions.keys():
127+
self._positions[k] = list(v)
128+
self._observers.process('pos-set', self.get_positions())
129+
109130
def _on_pick(self, event):
110131
# On the pick event, find the original line corresponding to the legend
111132
# proxy line, and toggle its visibility.
@@ -221,11 +242,28 @@ def on_class_changed(self, func):
221242
Parameters
222243
----------
223244
func : callable
224-
Function to call when *set_positions* is called.
245+
Function to call when the current class is changed.
225246
226247
Returns
227248
-------
228249
int
229250
Connection id (which can be used to disconnect *func*).
230251
"""
231252
self._observers.connect('class-changed', lambda klass: func(klass))
253+
254+
def on_positions_set(self, func):
255+
"""
256+
Connect *func* as a callback function when the *set_positions* function is called.
257+
*func* will receive the updated dictionary of all points.
258+
259+
Parameters
260+
----------
261+
func : callable
262+
Function to call when *set_positions* is called.
263+
264+
Returns
265+
-------
266+
int
267+
Connection id (which can be used to disconnect *func*).
268+
"""
269+
return self._observers.connect('pos-set', lambda pos_dict: func(pos_dict))

0 commit comments

Comments
 (0)