Skip to content

Support custom bin edges for histogram binning (nbins only accepts an int) #397

Description

@tpvasconcelos

Problem

When using histogram binning (ridgeplot(samples=..., nbins=...)), the bins are always nbins equal-width bins spanning [min(samples), max(samples)] (numpy's default). For discrete/count data this means bars are generally not aligned with the sample values. For example, for samples=[[0, 1, 2, 3]] and nbins=4, the bin edges are [0, 0.75, 1.5, 2.25, 3.0] and the bars are drawn at the true bin centers 0.375, 1.125, 1.875, 2.625.

This is correct histogram geometry (and consistent with matplotlib/seaborn), but users with discrete/count data often want integer-aligned bins, i.e. edges at -0.5, 0.5, 1.5, ... so that each bar is centered on an integer value. There is currently no way to express this: nbins only accepts an int, so the only workaround is to precompute the histogram manually and pass densities directly.

Proposal

Accept a sequence of bin edges in addition to an int, mirroring np.histogram's bins parameter:

# Today (equal-width bins over [min, max]):
ridgeplot(samples=samples, nbins=4)

# Proposed (custom edges, e.g. integer-aligned bins for count data):
ridgeplot(samples=samples, nbins=[-0.5, 0.5, 1.5, 2.5, 3.5])

(Or via a new dedicated parameter, if overloading nbins is undesirable.)

Notes

  • np.histogram already supports edge sequences natively, so the change in ridgeplot._hist.bin_trace_samples should be small; most of the work is API design, typing, and docs.
  • Surfaced during the test-suite review in Improve test suite: ridgeplot._hist #365.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions