Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6513a69

Browse filesBrowse files
committed
Re-use histogram binning
1 parent 06d4b17 commit 6513a69
Copy full SHA for 6513a69

File tree

5 files changed

+14
-10
lines changed
Filter options

5 files changed

+14
-10
lines changed

‎src/napari_matplotlib/histogram.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/histogram.py
+14-10Lines changed: 14 additions & 10 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.