Skip to content

Commit 06d4b17

Browse files
authored
Merge pull request #244 from p-j-smith/feat/integer-hist-bins
Use integer bins for integer data in `HistogramWidget`
2 parents c481207 + 610f19c commit 06d4b17

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Changes
88
- Histogram plots of points and vector layers are now coloured with their napari colourmap.
99
- Added support for Matplotlib 3.8
1010

11+
Bug fixes
12+
~~~~~~~~~
13+
- Use integer bin limits for integer images in ``HistogramWidget``
14+
1115
1.1.0
1216
-----
1317
Additions

src/napari_matplotlib/histogram.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
import numpy as np
55
import numpy.typing as npt
66
from matplotlib.container import BarContainer
7-
from qtpy.QtWidgets import QComboBox, QLabel, QVBoxLayout, QWidget
7+
from qtpy.QtWidgets import (
8+
QComboBox,
9+
QLabel,
10+
QVBoxLayout,
11+
QWidget,
12+
)
813

914
from .base import SingleAxesWidget
1015
from .features import FEATURES_LAYER_TYPES
@@ -65,20 +70,26 @@ def draw(self) -> None:
6570

6671
# Important to calculate bins after slicing 3D data, to avoid reading
6772
# whole cube into memory.
68-
bins = np.linspace(np.min(data), np.max(data), 100)
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)
6980

7081
if layer.rgb:
7182
# Histogram RGB channels independently
7283
for i, c in enumerate("rgb"):
7384
self.axes.hist(
7485
data[..., i].ravel(),
75-
bins=bins,
86+
bins=bins.tolist(),
7687
label=c,
7788
histtype="step",
7889
color=_COLORS[c],
7990
)
8091
else:
81-
self.axes.hist(data.ravel(), bins=bins, label=layer.name)
92+
self.axes.hist(data.ravel(), bins=bins.tolist(), label=layer.name)
8293

8394
self._contrast_lines = [
8495
self.axes.axvline(lim, color="white")
Loading
Loading

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy