From 415b9419c036cc7dea7de9fb69b0c3bc19c8145b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 1 Jan 2024 17:50:07 +0000 Subject: [PATCH 1/7] [pre-commit.ci] pre-commit autoupdate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit updates: - [github.com/pre-commit/pre-commit-hooks: v4.4.0 → v4.5.0](https://github.com/pre-commit/pre-commit-hooks/compare/v4.4.0...v4.5.0) - [github.com/asottile/pyupgrade: v3.13.0 → v3.15.0](https://github.com/asottile/pyupgrade/compare/v3.13.0...v3.15.0) - [github.com/asottile/setup-cfg-fmt: v2.4.0 → v2.5.0](https://github.com/asottile/setup-cfg-fmt/compare/v2.4.0...v2.5.0) - [github.com/PyCQA/isort: 5.12.0 → 5.13.2](https://github.com/PyCQA/isort/compare/5.12.0...5.13.2) - [github.com/psf/black: 23.9.1 → 23.12.1](https://github.com/psf/black/compare/23.9.1...23.12.1) --- .pre-commit-config.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9adef7f..005ed45 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -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 From 847abd04f5816277c93ce5fd20d52603ae55a257 Mon Sep 17 00:00:00 2001 From: Xray <37886333+Xraydylan@users.noreply.github.com> Date: Wed, 10 Jan 2024 12:31:01 +0100 Subject: [PATCH 2/7] Default class --- mpl_point_clicker/_clicker.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/mpl_point_clicker/_clicker.py b/mpl_point_clicker/_clicker.py index beb1206..91e1328 100644 --- a/mpl_point_clicker/_clicker.py +++ b/mpl_point_clicker/_clicker.py @@ -17,7 +17,7 @@ class clicker: def __init__( self, ax, - classes, + classes=None, init_class=None, markers=None, colors=None, @@ -31,6 +31,7 @@ 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* @@ -47,6 +48,9 @@ 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 + if isinstance(classes, Integral): self._classes = list(range(classes)) else: From da8ade9d7a6921450207d47fa4af5dc27fa59ceb Mon Sep 17 00:00:00 2001 From: Xray <37886333+Xraydylan@users.noreply.github.com> Date: Wed, 10 Jan 2024 12:31:19 +0100 Subject: [PATCH 3/7] Optional Legend --- mpl_point_clicker/_clicker.py | 50 ++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/mpl_point_clicker/_clicker.py b/mpl_point_clicker/_clicker.py index 91e1328..f21bb14 100644 --- a/mpl_point_clicker/_clicker.py +++ b/mpl_point_clicker/_clicker.py @@ -21,6 +21,7 @@ def __init__( init_class=None, markers=None, colors=None, + disable_legend=None, legend_bbox=(1.04, 1), legend_loc="upper left", pick_dist=10, @@ -38,6 +39,8 @@ 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 @@ -50,6 +53,7 @@ class in *classes* """ if classes is None: classes = 1 + disable_legend = True if isinstance(classes, Integral): self._classes = list(range(classes)) @@ -70,35 +74,47 @@ 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 + + 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 (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) @@ -144,6 +160,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]: From 32e81b87ace122de6b9173885e8739f5180d6d54 Mon Sep 17 00:00:00 2001 From: Xray <37886333+Xraydylan@users.noreply.github.com> Date: Wed, 10 Jan 2024 12:31:31 +0100 Subject: [PATCH 4/7] Clear positions function --- mpl_point_clicker/_clicker.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/mpl_point_clicker/_clicker.py b/mpl_point_clicker/_clicker.py index f21bb14..4628661 100644 --- a/mpl_point_clicker/_clicker.py +++ b/mpl_point_clicker/_clicker.py @@ -147,6 +147,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. + + 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() + def _on_pick(self, event): # On the pick event, find the original line corresponding to the legend # proxy line, and toggle its visibility. From 2f82f5167f5c8c5228ba81a5f16c22ad791e5773 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 10 Jan 2024 11:37:53 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mpl_point_clicker/_clicker.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mpl_point_clicker/_clicker.py b/mpl_point_clicker/_clicker.py index 4628661..5df9774 100644 --- a/mpl_point_clicker/_clicker.py +++ b/mpl_point_clicker/_clicker.py @@ -104,7 +104,7 @@ class in *classes* self._class_leg_artists = {} for legline, legtext, klass in zip( - self._leg.get_lines(), self._leg.get_texts(), self._classes + self._leg.get_lines(), self._leg.get_texts(), self._classes ): legline.set_picker(pick_dist) legtext.set_picker(pick_dist) @@ -112,7 +112,11 @@ class in *classes* self._leg_artists[legline] = klass try: # mpl < 3.5 - self._class_leg_artists[klass] = (legline, legline._legmarker, legtext) + self._class_leg_artists[klass] = ( + legline, + legline._legmarker, + legtext, + ) except AttributeError: self._class_leg_artists[klass] = (legline, legtext) From 0b8c7949f4194f0bac61987d13d1e5c8a3189887 Mon Sep 17 00:00:00 2001 From: Ian Hunt-Isaak Date: Tue, 23 Jan 2024 14:18:56 -0500 Subject: [PATCH 6/7] review updates --- mpl_point_clicker/_clicker.py | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/mpl_point_clicker/_clicker.py b/mpl_point_clicker/_clicker.py index 5df9774..27f5b15 100644 --- a/mpl_point_clicker/_clicker.py +++ b/mpl_point_clicker/_clicker.py @@ -17,11 +17,11 @@ class clicker: def __init__( self, ax, - classes=None, + classes=1, init_class=None, markers=None, colors=None, - disable_legend=None, + disable_legend=False, legend_bbox=(1.04, 1), legend_loc="upper left", pick_dist=10, @@ -39,8 +39,8 @@ 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 + 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 @@ -51,10 +51,6 @@ 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 - if isinstance(classes, Integral): self._classes = list(range(classes)) else: @@ -74,10 +70,6 @@ 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 self._disable_legend = disable_legend @@ -86,8 +78,6 @@ class in *classes* linestyle = line_kwargs.pop("linestyle", "") for i, c in enumerate(self._classes): label = c - if disable_legend: - label = None (self._lines[c],) = self.ax.plot( [], [], @@ -151,22 +141,27 @@ def set_positions(self, positions): self._positions[k] = list(v) self._observers.process('pos-set', self.get_positions()) - def clear_positions(self, classes=None): + def clear_positions(self, classes: str | list[str] | None=None): """ - Clears all points of classes in *classes*. Either all classes or a list of classes. + Clear all points of classes in *classes*. + + Either all classes or a list of classes. Parameters ---------- - classes : list - A list of classes to clear. If None, all classes will be cleared. + 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 From 31a65048a7c94deb0c81a7bd88e423a2e98887b1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 19:19:54 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mpl_point_clicker/_clicker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mpl_point_clicker/_clicker.py b/mpl_point_clicker/_clicker.py index 27f5b15..8cd91a9 100644 --- a/mpl_point_clicker/_clicker.py +++ b/mpl_point_clicker/_clicker.py @@ -141,9 +141,9 @@ 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): + def clear_positions(self, classes: str | list[str] | None = None): """ - Clear all points of classes in *classes*. + Clear all points of classes in *classes*. Either all classes or a list of classes.