Skip to content

Commit 9067a2d

Browse files
committed
Don't allow bins lower than 0 if dtype is unisgned
1 parent d589d5f commit 9067a2d

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Diff for: examples/histogram.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import napari
66

77
viewer = napari.Viewer()
8-
viewer.open_sample("napari", "kidney")
8+
viewer.open_sample("napari", "coins")
99

1010
viewer.window.add_plugin_dock_widget(
1111
plugin_name="napari-matplotlib", widget_name="Histogram"

Diff for: src/napari_matplotlib/histogram.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(
4646
bins_start.setStepType(QAbstractSpinBox.AdaptiveDecimalStepType)
4747
bins_start.setRange(-1e10, 1e10)
4848
bins_start.setValue(0)
49-
bins_start.setWrapping(True)
49+
bins_start.setWrapping(False)
5050
bins_start.setKeyboardTracking(False)
5151
bins_start.setDecimals(2)
5252

@@ -55,6 +55,7 @@ def __init__(
5555
bins_stop.setStepType(QAbstractSpinBox.AdaptiveDecimalStepType)
5656
bins_stop.setRange(-1e10, 1e10)
5757
bins_stop.setValue(100)
58+
bins_start.setWrapping(False)
5859
bins_stop.setKeyboardTracking(False)
5960
bins_stop.setDecimals(2)
6061

@@ -148,13 +149,17 @@ def on_update_layers(self) -> None:
148149
self.autoset_widget_bins(data=layer_data)
149150

150151
# Only allow integer bins for integer data
152+
# And only allow values greater than 0 for unsigned integers
151153
n_decimals = 0 if np.issubdtype(layer_data.dtype, np.integer) else 2
152-
self.findChild(QDoubleSpinBox, name="bins start").setDecimals(
153-
n_decimals
154-
)
155-
self.findChild(QDoubleSpinBox, name="bins stop").setDecimals(
156-
n_decimals
157-
)
154+
is_unsigned = layer_data.dtype.kind == "u"
155+
minimum_value = 0 if is_unsigned else -1e10
156+
157+
bins_start = self.findChild(QDoubleSpinBox, name="bins start")
158+
bins_stop = self.findChild(QDoubleSpinBox, name="bins stop")
159+
bins_start.setDecimals(n_decimals)
160+
bins_stop.setDecimals(n_decimals)
161+
bins_start.setMinimum(minimum_value)
162+
bins_stop.setMinimum(minimum_value)
158163

159164
def draw(self) -> None:
160165
"""
@@ -169,7 +174,7 @@ def draw(self) -> None:
169174
self.bins_start,
170175
self.bins_stop,
171176
self.bins_num,
172-
dtype=int if np.issubdtype(data.dtype, np.integer) else float,
177+
dtype=data.dtype,
173178
)
174179

175180
if layer.rgb:

0 commit comments

Comments
 (0)