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

Conversation

alexrudy
Copy link
Contributor

@alexrudy alexrudy commented Sep 29, 2019

PR Summary

Issue #15332 raised a problem with the bins= argument to axes.hist, where it would fail if the bins are datetime.datetime objects (although the data can be datetime.datetime objects).

In axes.hist we call convert_xunits on x and bin_range. This PR also calls convert_xunits on bins if it is a sequence (and not a scalar or string).

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

tiny formatting point but basically looks good.

@@ -1754,6 +1754,27 @@ def test_hist_datetime_datasets():
ax.hist(data, stacked=False)


@pytest.mark.parametrize("bins_preprocess",
[lambda bins: None,
Copy link
Member

Choose a reason for hiding this comment

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

I think that if you leave out this test case, you can actually assert that the bins are what you expect them to be, since all the other ones return valid representations of the underlying data and should thus set the histogram binning to the same values. Maybe move the None test to a separate test?

This test does ensure that the current failure mode (raising an error) doesn't occur, but it doesn't ensure that these different types actually work as expected, which would be nice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On closer inspection, the previous test implicitly covers the None case, so I think just removing the None case and then asserting that the bins are equal to mpl.dates.date2num(date_edges) is whats required, so I did that.

@jklymak
Copy link
Member

jklymak commented Oct 1, 2019

We merged a PR that now causes this to fail for a couple of reasons. The svg failure is not your problem, but the unused import failure in the flake8 check should be fixed before we merge. Srry for the inconvenience!

@alexrudy alexrudy force-pushed the hist-datetime-bins branch from 42c188d to 7a1b89e Compare October 1, 2019 21:38
@alexrudy alexrudy force-pushed the hist-datetime-bins branch from 4763de1 to db32b26 Compare October 3, 2019 16:18
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?

@dstansby dstansby added this to the v3.2.0 milestone Oct 11, 2019
@dstansby dstansby merged commit e672bf8 into matplotlib:master Oct 11, 2019
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Oct 11, 2019
timhoffm added a commit that referenced this pull request Oct 11, 2019
…347-on-v3.2.x

Backport PR #15347 on branch v3.2.x (Fix axes.hist bins units)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants