Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Changes
- Histogram plots of points and vector layers are now coloured with their napari colourmap.
- Added support for Matplotlib 3.8

Bug fixes
~~~~~~~~~
- Use integer bin limits for integer images in ``HistogramWidget``

1.1.0
-----
Additions
Expand Down
18 changes: 14 additions & 4 deletions src/napari_matplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import numpy as np
import numpy.typing as npt
from matplotlib.container import BarContainer
from qtpy.QtWidgets import QComboBox, QLabel, QVBoxLayout, QWidget
from qtpy.QtWidgets import (
QComboBox,
QLabel,
QVBoxLayout,
QWidget,
)

from .base import SingleAxesWidget
from .features import FEATURES_LAYER_TYPES
Expand Down Expand Up @@ -65,20 +70,25 @@ def draw(self) -> None:

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

if layer.rgb:
# Histogram RGB channels independently
for i, c in enumerate("rgb"):
self.axes.hist(
data[..., i].ravel(),
bins=bins,
bins=bins.tolist(),
label=c,
histtype="step",
color=_COLORS[c],
)
else:
self.axes.hist(data.ravel(), bins=bins, label=layer.name)
self.axes.hist(data.ravel(), bins=bins.tolist(), label=layer.name)

self._contrast_lines = [
self.axes.axvline(lim, color="white")
Expand Down