Skip to content

Commit 94e5763

Browse files
committed
Update hist bin centers algorithm
Signed-off-by: Tomas Pereira de Vasconcelos <tomasvasconcelos1@gmail.com>
1 parent c573bd0 commit 94e5763

6 files changed

Lines changed: 10 additions & 6 deletions

File tree

206 Bytes
Loading

docs/reference/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Unreleased changes
77

88
### Bug fixes
99

10-
- Fix the way histogram bin midpoints are computed ({gh-pr}`364`)
10+
- Fix the way histogram bin centers are computed ({gh-pr}`364`)
1111

1212
### Documentation
1313

src/ridgeplot/_hist.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def bin_trace_samples(
3434
raise ValueError("The weights array should have the same length as the samples array.")
3535
if not np.isfinite(weights).all():
3636
raise ValueError("The weights array should not contain any infs or NaNs.")
37-
hist, bins = np.histogram(trace_samples, bins=nbins, weights=weights)
38-
bin_midpoints = np.linspace(bins[0], bins[-1], nbins)
39-
return [(float(x), float(y)) for x, y in zip(bin_midpoints, hist, strict=True)]
37+
hist_counts, hist_edges = np.histogram(trace_samples, bins=nbins, weights=weights)
38+
bin_centers = 0.5 * (hist_edges[:-1] + hist_edges[1:])
39+
return [(float(x), float(y)) for x, y in zip(bin_centers, hist_counts, strict=True)]
4040

4141

4242
def bin_samples(
-318 Bytes
Loading

tests/e2e/artifacts/basic_hist.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/unit/test_hist.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212

1313
SAMPLES_IN = [1, 2, 2, 3, 4]
1414
NBINS = 4
15-
DENSITIES_OUT = [(1, 1), (2, 2), (3, 1), (4, 1)]
15+
# NOTE: The x values in DENSITIES_OUT correspond to the centers of
16+
# equally spaced bins over the range [1, 4].
17+
# This can be counterintuitive for count data, as the bins
18+
# do not align with the integer sample values.
19+
DENSITIES_OUT = [(1.375, 1), (2.125, 2), (2.875, 1), (3.625, 1)]
1620
X_OUT, Y_OUT = zip(*DENSITIES_OUT, strict=True)
1721

1822
WEIGHTS = [1, 1, 1, 1, 9]

0 commit comments

Comments
 (0)