Skip to content

Value error in histplot with binwidth smaller than half the data range #3646

Description

@jhncls

sns.histplot([1, 2, 3], binwidth=7) crashes with

"ValueError: bins must be positive, when an integer"

It is related to #2721, which is marked as solved, but the error also happens in the dev version.

The cause seems to be line 136 in counting.py with bins = int(round((stop - start) / binwidth)), setting it to 0 for small ranges. Changing this to bins = max(1, int(round((stop - start) / binwidth))) would probably solve it adequately.

(The code also has problems when binwidth=0 (division by zero) or negative (this makes bins negative, which causes numpy to protest)).

By the way, binwidth is ignored when discrete=True. As an example, sns.histplot(titanic[titanic['who'] == 'woman'], x='age', binwidth=5) has some bins with 5 and others with 6 ages. Of course, people who really care can provide their own custom bin edges.

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