Skip to content

hist(): align!='mid' and unequally spaced bins don't play well together. #12524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6644,6 +6644,13 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
# so that each histogram uses the same bins
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
tops.append(m)

# align != 'mid' only makes sense for equal-sized bins.
if align != 'mid' and not cbook._is_equally_spaced(bins):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should only apply to the step-like functions, so if histtype in ['step', 'stepfilled'] ...
For the bar-like functions, it may not make too much sense, but at least it's not wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think #12494 shows very clearly that whoever implemented align did not take non-equally-spaced bins into account, and while the result is "not wrong", it doesn't make much sense either.

raise ValueError(
"When 'bins' are not equally spaced, the only valid value for "
"'align' is 'mid'")

tops = np.array(tops, float) # causes problems later if it's an int
if stacked:
tops = tops.cumsum(axis=0)
Expand Down
7 changes: 7 additions & 0 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,3 +2165,10 @@ def __init__(self, fget):

def __get__(self, instance, owner):
return self._fget(owner)


def _is_equally_spaced(x):
"""
Return whether the values in *x* are equally spaced (up to fp errors).
"""
return len(x) < 2 or np.allclose(np.diff(x), (x[-1] - x[0]) / (len(x) - 1))
4 changes: 2 additions & 2 deletions lib/matplotlib/streamplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,9 @@ def __init__(self, x, y):
self.width = x[-1] - x[0]
self.height = y[-1] - y[0]

if not np.allclose(np.diff(x), self.width / (self.nx - 1)):
if not cbook._is_equally_spaced(x):
raise ValueError("'x' values must be equally spaced")
if not np.allclose(np.diff(y), self.height / (self.ny - 1)):
if not cbook._is_equally_spaced(y):
raise ValueError("'y' values must be equally spaced")

@property
Expand Down
5 changes: 5 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3278,6 +3278,11 @@ def test_hist_labels():
assert l[2][0].get_label() == '00'


def test_hist_invalid_combinations():
with pytest.raises(ValueError):
plt.figure().subplots().hist([], bins=[0, 1/4, 1], align="left")


@image_comparison(['transparent_markers'], remove_text=True)
def test_transparent_markers():
np.random.seed(0)
Expand Down