|
20 | 20 | _COLORS = {"r": "tab:red", "g": "tab:green", "b": "tab:blue"}
|
21 | 21 |
|
22 | 22 |
|
| 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 | + |
23 | 33 | class HistogramWidget(SingleAxesWidget):
|
24 | 34 | """
|
25 | 35 | Display a histogram of the currently selected layer.
|
@@ -70,13 +80,7 @@ def draw(self) -> None:
|
70 | 80 |
|
71 | 81 | # Important to calculate bins after slicing 3D data, to avoid reading
|
72 | 82 | # 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) |
80 | 84 |
|
81 | 85 | if layer.rgb:
|
82 | 86 | # Histogram RGB channels independently
|
@@ -215,9 +219,9 @@ def draw(self) -> None:
|
215 | 219 | if data is None:
|
216 | 220 | return
|
217 | 221 |
|
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()) |
221 | 225 | patches = cast(BarContainer, patches)
|
222 | 226 |
|
223 | 227 | # recolor the histogram plot
|
|
0 commit comments