Skip to content

Commit

Permalink
Merge branch 'main' into 2.0clog
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby authored Jan 15, 2024
2 parents f149a10 + 86fbedc commit 8a32243
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/napari_matplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
_COLORS = {"r": "tab:red", "g": "tab:green", "b": "tab:blue"}


def _get_bins(data: npt.NDArray[Any]) -> npt.NDArray[Any]:
if data.dtype.kind in {"i", "u"}:
# Make sure integer data types have integer sized bins
step = np.ceil(np.ptp(data) / 100)
return np.arange(np.min(data), np.max(data) + step, step)
else:
# For other data types, just have 128 evenly spaced bins
return np.linspace(np.min(data), np.max(data), 100)


class HistogramWidget(SingleAxesWidget):
"""
Display a histogram of the currently selected layer.
Expand Down Expand Up @@ -70,13 +80,7 @@ def draw(self) -> None:

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

if layer.rgb:
# Histogram RGB channels independently
Expand Down Expand Up @@ -215,9 +219,9 @@ def draw(self) -> None:
if data is None:
return

_, bins, patches = self.axes.hist(
data, bins=50, edgecolor="white", linewidth=0.3
)
bins = _get_bins(data)

_, bins, patches = self.axes.hist(data, bins=bins.tolist())
patches = cast(BarContainer, patches)

# recolor the histogram plot
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/napari_matplotlib/tests/baseline/test_histogram_2D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/napari_matplotlib/tests/baseline/test_histogram_3D.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8a32243

Please sign in to comment.