|
25 | 25 | _COLORS = {"r": "tab:red", "g": "tab:green", "b": "tab:blue"}
|
26 | 26 |
|
27 | 27 |
|
| 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 | + |
28 | 38 | class HistogramWidget(SingleAxesWidget):
|
29 | 39 | """
|
30 | 40 | Display a histogram of the currently selected layer.
|
@@ -134,13 +144,7 @@ def _update_contrast_lims(self) -> None:
|
134 | 144 |
|
135 | 145 | def autoset_widget_bins(self, data: npt.NDArray[Any]) -> None:
|
136 | 146 | """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) |
144 | 148 |
|
145 | 149 | # Disable callbacks whilst setting widget values
|
146 | 150 | for widget in self._bin_widgets.values():
|
@@ -353,9 +357,9 @@ def draw(self) -> None:
|
353 | 357 | if data is None:
|
354 | 358 | return
|
355 | 359 |
|
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()) |
359 | 363 | patches = cast(BarContainer, patches)
|
360 | 364 |
|
361 | 365 | # recolor the histogram plot
|
|
0 commit comments