Skip to content

Commit c5e0886

Browse files
committed
Fix autosetting bins from data
1 parent e86d4f6 commit c5e0886

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/napari_matplotlib/histogram.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,19 @@ def bins_num(self, num: int) -> None:
135135

136136
def autoset_widget_bins(self, data: npt.NDArray[Any]) -> None:
137137
"""Update widgets with bins determined from the image data"""
138-
bins = np.linspace(np.min(data), np.max(data), 100, dtype=data.dtype)
138+
if data.dtype.kind in {"i", "u"}:
139+
# Make sure integer data types have integer sized bins
140+
# We can't use unsigned ints when calculating the step, otherwise
141+
# the following warning is raised:
142+
# 'RuntimeWarning: overflow encountered in scalar subtract'
143+
step = (
144+
abs(np.min(data).astype(int) - np.max(data).astype(int)) // 100
145+
)
146+
step = max(1, step)
147+
bins = np.arange(np.min(data), np.max(data) + step, step)
148+
else:
149+
bins = np.linspace(np.min(data), np.max(data), 100)
150+
139151
self.bins_start = bins[0]
140152
self.bins_stop = bins[-1]
141153
self.bins_num = bins.size

0 commit comments

Comments
 (0)