Skip to content

Fix axes.hist bins units #15347

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

Merged
merged 3 commits into from
Oct 11, 2019
Merged
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
3 changes: 3 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6575,6 +6575,9 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
if bin_range is not None:
bin_range = self.convert_xunits(bin_range)

if not cbook.is_scalar_or_string(bins):
bins = self.convert_xunits(bins)

# We need to do to 'weights' what was done to 'x'
if weights is not None:
w = cbook._reshape_2D(weights, 'weights')
Expand Down
23 changes: 23 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,6 +1753,29 @@ def test_hist_datetime_datasets():
ax.hist(data, stacked=False)


@pytest.mark.parametrize("bins_preprocess",
[mpl.dates.date2num,
lambda bins: bins,
lambda bins: np.asarray(bins).astype('datetime64')],
ids=['date2num', 'datetime.datetime',
'np.datetime64'])
def test_hist_datetime_datasets_bins(bins_preprocess):
data = [[datetime.datetime(2019, 1, 5), datetime.datetime(2019, 1, 11),
datetime.datetime(2019, 2, 1), datetime.datetime(2019, 3, 1)],
[datetime.datetime(2019, 1, 11), datetime.datetime(2019, 2, 5),
datetime.datetime(2019, 2, 18), datetime.datetime(2019, 3, 1)]]

date_edges = [datetime.datetime(2019, 1, 1), datetime.datetime(2019, 2, 1),
datetime.datetime(2019, 3, 1)]

fig, ax = plt.subplots()
_, bins, _ = ax.hist(data, bins=bins_preprocess(date_edges), stacked=True)
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to parametrize on stacked as well, to tighten up the implementation here?

Please don't let this comment block the merge, I think overall these tests look good to me.

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 agree we could parameterize this test better, but that also applies to the surrounding hist tests. Maybe merge this and if I find the time this weekend I can do a mini refactor of some tests?

np.testing.assert_allclose(bins, mpl.dates.date2num(date_edges))

_, bins, _ = ax.hist(data, bins=bins_preprocess(date_edges), stacked=False)
np.testing.assert_allclose(bins, mpl.dates.date2num(date_edges))


@pytest.mark.parametrize('data, expected_number_of_hists',
[([], 1),
([[]], 1),
Expand Down