Skip to content

Commit 0d93aa1

Browse files
authored
Merge pull request #168 from dstansby/dedup-single-axis
De-duplicate single axis logic
2 parents 65b6f11 + cf16da3 commit 0d93aa1

File tree

4 files changed

+28
-35
lines changed

4 files changed

+28
-35
lines changed

Diff for: src/napari_matplotlib/base.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from .util import Interval, from_napari_css_get_size_of
1616

17-
__all__ = ["BaseNapariMPLWidget", "NapariMPLWidget"]
17+
__all__ = ["BaseNapariMPLWidget", "NapariMPLWidget", "SingleAxesWidget"]
1818

1919

2020
class BaseNapariMPLWidget(QWidget):
@@ -270,6 +270,27 @@ def on_update_layers(self) -> None:
270270
"""
271271

272272

273+
class SingleAxesWidget(NapariMPLWidget):
274+
"""
275+
In addition to `NapariMPLWidget`, this sets up a single axes and
276+
the callback to clear it.
277+
"""
278+
279+
def __init__(
280+
self,
281+
napari_viewer: napari.viewer.Viewer,
282+
parent: Optional[QWidget] = None,
283+
):
284+
super().__init__(napari_viewer=napari_viewer, parent=parent)
285+
self.add_single_axes()
286+
287+
def clear(self) -> None:
288+
"""
289+
Clear the axes.
290+
"""
291+
self.axes.clear()
292+
293+
273294
class NapariNavigationToolbar(NavigationToolbar2QT):
274295
"""Custom Toolbar style for Napari."""
275296

Diff for: src/napari_matplotlib/histogram.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import numpy as np
55
from qtpy.QtWidgets import QWidget
66

7-
from .base import NapariMPLWidget
7+
from .base import SingleAxesWidget
88
from .util import Interval
99

1010
__all__ = ["HistogramWidget"]
1111

1212
_COLORS = {"r": "tab:red", "g": "tab:green", "b": "tab:blue"}
1313

1414

15-
class HistogramWidget(NapariMPLWidget):
15+
class HistogramWidget(SingleAxesWidget):
1616
"""
1717
Display a histogram of the currently selected layer.
1818
"""
@@ -26,15 +26,8 @@ def __init__(
2626
parent: Optional[QWidget] = None,
2727
):
2828
super().__init__(napari_viewer, parent=parent)
29-
self.add_single_axes()
3029
self._update_layers(None)
3130

32-
def clear(self) -> None:
33-
"""
34-
Clear the axes.
35-
"""
36-
self.axes.clear()
37-
3831
def draw(self) -> None:
3932
"""
4033
Clear the axes and histogram the currently selected layer/slice.

Diff for: src/napari_matplotlib/scatter.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import numpy.typing as npt
55
from qtpy.QtWidgets import QComboBox, QLabel, QVBoxLayout, QWidget
66

7-
from .base import NapariMPLWidget
7+
from .base import SingleAxesWidget
88
from .util import Interval
99

1010
__all__ = ["ScatterBaseWidget", "ScatterWidget", "FeaturesScatterWidget"]
1111

1212

13-
class ScatterBaseWidget(NapariMPLWidget):
13+
class ScatterBaseWidget(SingleAxesWidget):
1414
"""
1515
Base class for widgets that scatter two datasets against each other.
1616
"""
@@ -19,20 +19,6 @@ class ScatterBaseWidget(NapariMPLWidget):
1919
# the scatter is plotted as a 2D histogram
2020
_threshold_to_switch_to_histogram = 500
2121

22-
def __init__(
23-
self,
24-
napari_viewer: napari.viewer.Viewer,
25-
parent: Optional[QWidget] = None,
26-
):
27-
super().__init__(napari_viewer, parent=parent)
28-
self.add_single_axes()
29-
30-
def clear(self) -> None:
31-
"""
32-
Clear the axes.
33-
"""
34-
self.axes.clear()
35-
3622
def draw(self) -> None:
3723
"""
3824
Scatter the currently selected layers.

Diff for: src/napari_matplotlib/slice.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy.typing as npt
66
from qtpy.QtWidgets import QComboBox, QHBoxLayout, QLabel, QSpinBox, QWidget
77

8-
from .base import NapariMPLWidget
8+
from .base import SingleAxesWidget
99
from .util import Interval
1010

1111
__all__ = ["SliceWidget"]
@@ -14,7 +14,7 @@
1414
_dims = ["x", "y", "z"]
1515

1616

17-
class SliceWidget(NapariMPLWidget):
17+
class SliceWidget(SingleAxesWidget):
1818
"""
1919
Plot a 1D slice along a given dimension.
2020
"""
@@ -29,7 +29,6 @@ def __init__(
2929
):
3030
# Setup figure/axes
3131
super().__init__(napari_viewer, parent=parent)
32-
self.add_single_axes()
3332

3433
button_layout = QHBoxLayout()
3534
self.layout().addLayout(button_layout)
@@ -108,12 +107,6 @@ def _get_xy(self) -> Tuple[npt.NDArray[Any], npt.NDArray[Any]]:
108107

109108
return x, y
110109

111-
def clear(self) -> None:
112-
"""
113-
Clear the axes.
114-
"""
115-
self.axes.cla()
116-
117110
def draw(self) -> None:
118111
"""
119112
Clear axes and draw a 1D plot.

0 commit comments

Comments
 (0)