Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small improvements for better use #17

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Changes from 4 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
77 changes: 60 additions & 17 deletions mpl_point_clicker/_clicker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class clicker:
def __init__(
self,
ax,
classes,
classes=None,
ianhi marked this conversation as resolved.
Show resolved Hide resolved
init_class=None,
markers=None,
colors=None,
disable_legend=None,
legend_bbox=(1.04, 1),
legend_loc="upper left",
pick_dist=10,
Expand All @@ -31,12 +32,15 @@ def __init__(
----------
ax : matplotlib axis
classes : int or list
default is 1
init_class : int, or str, optional
The initial class to use, otherwise will be the first
class in *classes*
markers : list
The marker styles to use.
colors : list
disable_legend : bool
disable to show the classes in the legend, default is False
legend_bbox : tuple
bbox to use for the legend
legend_loc : str or int
Expand All @@ -47,6 +51,10 @@ class in *classes*
Line2D objects (from ax.plot) are used to generate the markers.
line_kwargs will be passed through to all of the `ax.plot` calls.
"""
if classes is None:
classes = 1
disable_legend = True
ianhi marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(classes, Integral):
self._classes = list(range(classes))
else:
Expand All @@ -66,35 +74,51 @@ class in *classes*
colors = [None] * len(self._classes)
if markers is None:
markers = ["o"] * len(self._classes)
if disable_legend is None:
disable_legend = False
if disable_legend and len(self._classes) != 1:
disable_legend = False
ianhi marked this conversation as resolved.
Show resolved Hide resolved

self._disable_legend = disable_legend

self.ax = ax
self._lines = {}
linestyle = line_kwargs.pop("linestyle", "")
for i, c in enumerate(self._classes):
label = c
if disable_legend:
label = None
ianhi marked this conversation as resolved.
Show resolved Hide resolved
(self._lines[c],) = self.ax.plot(
[],
[],
color=colors[i],
marker=markers[i],
label=c,
label=label,
linestyle=linestyle,
**line_kwargs,
)
self._leg = self.ax.legend(bbox_to_anchor=legend_bbox, loc=legend_loc)
self._leg_artists = {}
self._class_leg_artists = {}
for legline, legtext, klass in zip(
self._leg.get_lines(), self._leg.get_texts(), self._classes
):
legline.set_picker(pick_dist)
legtext.set_picker(pick_dist)
self._leg_artists[legtext] = klass
self._leg_artists[legline] = klass
try:
# mpl < 3.5
self._class_leg_artists[klass] = (legline, legline._legmarker, legtext)
except AttributeError:
self._class_leg_artists[klass] = (legline, legtext)

if not self._disable_legend:
self._leg = self.ax.legend(bbox_to_anchor=legend_bbox, loc=legend_loc)
self._leg_artists = {}
self._class_leg_artists = {}

for legline, legtext, klass in zip(
self._leg.get_lines(), self._leg.get_texts(), self._classes
):
legline.set_picker(pick_dist)
legtext.set_picker(pick_dist)
self._leg_artists[legtext] = klass
self._leg_artists[legline] = klass
try:
# mpl < 3.5
self._class_leg_artists[klass] = (
legline,
legline._legmarker,
legtext,
)
except AttributeError:
self._class_leg_artists[klass] = (legline, legtext)

self._fig = self.ax.figure
self._fig.canvas.mpl_connect("button_press_event", self._clicked)
Expand Down Expand Up @@ -127,6 +151,23 @@ def set_positions(self, positions):
self._positions[k] = list(v)
self._observers.process('pos-set', self.get_positions())

def clear_positions(self, classes=None):
"""
Clears all points of classes in *classes*. Either all classes or a list of classes.
ianhi marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
classes : list
A list of classes to clear. If None, all classes will be cleared.
"""
if classes is None:
classes = list(self._positions.keys())

for k in classes:
self._positions[k].clear()

self._update_points()

ianhi marked this conversation as resolved.
Show resolved Hide resolved
def _on_pick(self, event):
# On the pick event, find the original line corresponding to the legend
# proxy line, and toggle its visibility.
Expand All @@ -140,6 +181,8 @@ def _on_pick(self, event):
self._observers.process('class-changed', klass)

def _update_legend_alpha(self):
if self._disable_legend:
return
for c in self._classes:
alpha = 1 if c == self._current_class else 0.2
for a in self._class_leg_artists[c]:
Expand Down