Skip to content

Commit 37d33ae

Browse files
committed
Merge branch 'main' into feat/hist-bin-params
2 parents 65e84f8 + 86fbedc commit 37d33ae

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

src/napari_matplotlib/histogram.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
_COLORS = {"r": "tab:red", "g": "tab:green", "b": "tab:blue"}
2626

2727

28+
def _get_bins(data: npt.NDArray[Any]) -> npt.NDArray[Any]:
29+
if data.dtype.kind in {"i", "u"}:
30+
# Make sure integer data types have integer sized bins
31+
step = np.ceil(np.ptp(data) / 100)
32+
return np.arange(np.min(data), np.max(data) + step, step)
33+
else:
34+
# For other data types, just have 128 evenly spaced bins
35+
return np.linspace(np.min(data), np.max(data), 100)
36+
37+
2838
class HistogramWidget(SingleAxesWidget):
2939
"""
3040
Display a histogram of the currently selected layer.
@@ -134,13 +144,7 @@ def _update_contrast_lims(self) -> None:
134144

135145
def autoset_widget_bins(self, data: npt.NDArray[Any]) -> None:
136146
"""Update widgets with bins determined from the image data"""
137-
if data.dtype.kind in {"i", "u"}:
138-
# Make sure integer data types have integer sized bins
139-
step = abs(np.max(data) - np.min(data)) // 100
140-
step = max(1, step)
141-
bins = np.arange(np.min(data), np.max(data) + step, step)
142-
else:
143-
bins = np.linspace(np.min(data), np.max(data), 100)
147+
bins = _get_bins(data)
144148

145149
# Disable callbacks whilst setting widget values
146150
for widget in self._bin_widgets.values():
@@ -353,9 +357,9 @@ def draw(self) -> None:
353357
if data is None:
354358
return
355359

356-
_, bins, patches = self.axes.hist(
357-
data, bins=50, edgecolor="white", linewidth=0.3
358-
)
360+
bins = _get_bins(data)
361+
362+
_, bins, patches = self.axes.hist(data, bins=bins.tolist())
359363
patches = cast(BarContainer, patches)
360364

361365
# recolor the histogram plot
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)