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

[pre-commit.ci] pre-commit autoupdate #16

Merged
merged 7 commits into from
Jan 23, 2024
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
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ ci:
autoupdate_schedule: 'quarterly'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v3.13.0
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py37-plus, --keep-runtime-typing]
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v2.4.0
rev: v2.5.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/PyCQA/autoflake
Expand All @@ -21,11 +21,11 @@ repos:
- id: autoflake
args: ["--in-place", "--remove-all-unused-imports"]
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.9.1
rev: 23.12.1
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
Expand Down
72 changes: 55 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=1,
init_class=None,
markers=None,
colors=None,
disable_legend=False,
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, default False
If *True* do not display the legend.
legend_bbox : tuple
bbox to use for the legend
legend_loc : str or int
Expand Down Expand Up @@ -67,34 +71,44 @@ class in *classes*
if markers is None:
markers = ["o"] * len(self._classes)

self._disable_legend = disable_legend

self.ax = ax
self._lines = {}
linestyle = line_kwargs.pop("linestyle", "")
for i, c in enumerate(self._classes):
label = c
(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 +141,28 @@ def set_positions(self, positions):
self._positions[k] = list(v)
self._observers.process('pos-set', self.get_positions())

def clear_positions(self, classes: str | list[str] | None = None):
"""
Clear all points of classes in *classes*.

Either all classes or a list of classes.

Parameters
----------
classes : list, str, optional
A list, or single, of classes to clear. If None, all classes will be cleared.
"""
if classes is None:
classes = list(self._positions.keys())
elif isinstance(classes, str):
classes = [classes]

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

self._update_points()
self._observers.process('pos-set', self.get_positions())

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 +176,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
Loading