Skip to content

Commit 9f4cbb9

Browse files
committed
expose FeaturesScatterWidget as plugin
1 parent 0340c54 commit 9f4cbb9

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

Diff for: examples/features_scatter.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import numpy as np
33
from skimage.measure import regionprops_table
44

5-
from napari_matplotlib.scatter import FeaturesScatterWidget
6-
75
# make a test label image
86
label_image = np.zeros((100, 100), dtype=np.uint16)
97

@@ -30,8 +28,9 @@
3028
viewer.add_points(points_data, features=points_features)
3129

3230
# make the widget
33-
features_widget = FeaturesScatterWidget(viewer, histogram_for_large_data=False)
34-
viewer.window.add_dock_widget(features_widget)
31+
viewer.window.add_plugin_dock_widget(
32+
plugin_name="napari-matplotlib", widget_name="FeaturesScatter"
33+
)
3534

3635
if __name__ == "__main__":
3736
napari.run()

Diff for: src/napari_matplotlib/napari.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@ contributions:
88

99
- id: napari-matplotlib.scatter
1010
python_name: napari_matplotlib:ScatterWidget
11-
title: Make a scatter plot
11+
title: Make a scatter plot of image intensities
12+
13+
- id: napari-matplotlib.features_scatter
14+
python_name: napari_matplotlib:FeaturesScatterWidget
15+
title: Make a scatter plot of layer features
1216

1317
widgets:
1418
- command: napari-matplotlib.histogram
1519
display_name: Histogram
1620

1721
- command: napari-matplotlib.scatter
1822
display_name: Scatter
23+
24+
- command: napari-matplotlib.features_scatter
25+
display_name: FeaturesScatter

Diff for: src/napari_matplotlib/scatter.py

+13-29
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,26 @@
1111

1212

1313
class ScatterBaseWidget(NapariMPLWidget):
14+
# opacity value for the markers
15+
_marker_alpha = 0.5
16+
17+
# flag set to True if histogram should be used
18+
# for plotting large points
19+
_histogram_for_large_data = True
20+
21+
# if the number of points is greater than this value,
22+
# the scatter is plotted as a 2dhist
23+
_threshold_to_switch_to_histogram = 500
24+
1425
def __init__(
1526
self,
1627
napari_viewer: napari.viewer.Viewer,
17-
marker_alpha: float = 0.5,
18-
histogram_for_large_data: bool = True,
1928
):
2029
super().__init__(napari_viewer)
2130

22-
# flag set to True if histogram should be used
23-
# for plotting large points
24-
self.histogram_for_large_data = histogram_for_large_data
25-
26-
# set plotting visualization attributes
27-
self._marker_alpha = 0.5
28-
2931
self.axes = self.canvas.figure.subplots()
3032
self.update_layers(None)
3133

32-
@property
33-
def marker_alpha(self) -> float:
34-
"""Alpha (opacity) for the scatter markers"""
35-
return self._marker_alpha
36-
37-
@marker_alpha.setter
38-
def marker_alpha(self, alpha: float):
39-
self._marker_alpha = alpha
40-
self._draw()
41-
4234
def clear(self) -> None:
4335
self.axes.clear()
4436

@@ -52,15 +44,15 @@ def draw(self) -> None:
5244
# don't plot if there isn't data
5345
return
5446

55-
if self.histogram_for_large_data and (data[0].size > 500):
47+
if self._histogram_for_large_data and (data[0].size > 500):
5648
self.axes.hist2d(
5749
data[0].ravel(),
5850
data[1].ravel(),
5951
bins=100,
6052
norm=mcolor.LogNorm(),
6153
)
6254
else:
63-
self.axes.scatter(data[0], data[1], alpha=self.marker_alpha)
55+
self.axes.scatter(data[0], data[1], alpha=self._marker_alpha)
6456

6557
self.axes.set_xlabel(x_axis_name)
6658
self.axes.set_ylabel(y_axis_name)
@@ -95,13 +87,9 @@ class ScatterWidget(ScatterBaseWidget):
9587
def __init__(
9688
self,
9789
napari_viewer: napari.viewer.Viewer,
98-
marker_alpha: float = 0.5,
99-
histogram_for_large_data: bool = True,
10090
):
10191
super().__init__(
10292
napari_viewer,
103-
marker_alpha=marker_alpha,
104-
histogram_for_large_data=histogram_for_large_data,
10593
)
10694

10795
def _get_data(self) -> Tuple[List[np.ndarray], str, str]:
@@ -129,15 +117,11 @@ class FeaturesScatterWidget(ScatterBaseWidget):
129117
def __init__(
130118
self,
131119
napari_viewer: napari.viewer.Viewer,
132-
marker_alpha: float = 0.5,
133-
histogram_for_large_data: bool = True,
134120
key_selection_gui: bool = True,
135121
):
136122
self._key_selection_widget = None
137123
super().__init__(
138124
napari_viewer,
139-
marker_alpha=marker_alpha,
140-
histogram_for_large_data=histogram_for_large_data,
141125
)
142126

143127
if key_selection_gui is True:

0 commit comments

Comments
 (0)