Skip to content

Commit 8a32243

Browse files
authored
Merge branch 'main' into 2.0clog
2 parents f149a10 + 86fbedc commit 8a32243

5 files changed

+14
-10
lines changed

src/napari_matplotlib/histogram.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
_COLORS = {"r": "tab:red", "g": "tab:green", "b": "tab:blue"}
2121

2222

23+
def _get_bins(data: npt.NDArray[Any]) -> npt.NDArray[Any]:
24+
if data.dtype.kind in {"i", "u"}:
25+
# Make sure integer data types have integer sized bins
26+
step = np.ceil(np.ptp(data) / 100)
27+
return np.arange(np.min(data), np.max(data) + step, step)
28+
else:
29+
# For other data types, just have 128 evenly spaced bins
30+
return np.linspace(np.min(data), np.max(data), 100)
31+
32+
2333
class HistogramWidget(SingleAxesWidget):
2434
"""
2535
Display a histogram of the currently selected layer.
@@ -70,13 +80,7 @@ def draw(self) -> None:
7080

7181
# Important to calculate bins after slicing 3D data, to avoid reading
7282
# whole cube into memory.
73-
if data.dtype.kind in {"i", "u"}:
74-
# Make sure integer data types have integer sized bins
75-
step = abs(np.max(data) - np.min(data)) // 100
76-
step = max(1, step)
77-
bins = np.arange(np.min(data), np.max(data) + step, step)
78-
else:
79-
bins = np.linspace(np.min(data), np.max(data), 100)
83+
bins = _get_bins(data)
8084

8185
if layer.rgb:
8286
# Histogram RGB channels independently
@@ -215,9 +219,9 @@ def draw(self) -> None:
215219
if data is None:
216220
return
217221

218-
_, bins, patches = self.axes.hist(
219-
data, bins=50, edgecolor="white", linewidth=0.3
220-
)
222+
bins = _get_bins(data)
223+
224+
_, bins, patches = self.axes.hist(data, bins=bins.tolist())
221225
patches = cast(BarContainer, patches)
222226

223227
# recolor the histogram plot
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)