-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Histogram compatibility with numpy 7364 #7856
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
Histogram compatibility with numpy 7364 #7856
Conversation
determinism_tex.svg
Outdated
@@ -0,0 +1,29 @@ | |||
<?xml version="1.0" encoding="utf-8" standalone="no"?> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rebase and remove this file entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @QuLogic!
Thanks for the quick response. I rebased, but my changes are up to date with the current remote. The contents of determinism_tex.svg appear to be tested in test_backend_svg.py (line 204). Would you still like me to remove it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added in your first commit. You'll need to rebase and edit the first commit to remove the file.
@@ -5875,10 +5875,10 @@ def table(self, **kwargs): | |||
#### Data analysis | |||
|
|||
@_preprocess_data(replace_names=["x", 'weights'], label_namer="x") | |||
def hist(self, x, bins=None, range=None, normed=False, weights=None, | |||
def hist(self, x, bins=None, range=None, density=None, weights=None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the order is probably not a good idea, for backwards compatibility's sake, even though people really shouldn't be expecting order-only arguments to work this far in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @QuLogic!
My understanding is that if a user is expecting order-only arguments to work this far in, then their argument will just take the name density instead of normed now. And since the name density is meant, in this change, to actually replace normed, the code will still be backward compatible. If the developer specifically named it normed, then the fact that it's named allows it to work even though normed is at the end now.
Please let me know if this is inaccurate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is safe to do. If people are using positional it just works, if they are using keyword it still just works. By putting this here we make it clearer which the preferred name is.
lib/matplotlib/axes/_axes.py
Outdated
@@ -5923,7 +5923,10 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None, | |||
|
|||
Default is ``None`` | |||
|
|||
normed : boolean, optional | |||
normed or density : boolean, optional |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
normed, density
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest putting 'density' first as that will (eventually) be the preferred name.
lib/matplotlib/axes/_axes.py
Outdated
|
||
weights : (n, ) array_like or None, optional | ||
An array of weights, of the same shape as `x`. Each value in `x` | ||
only contributes its associated weight towards the bin count | ||
(instead of 1). If `normed` is True, the weights are normalized, | ||
(instead of 1). If `normed` and/or 'density' is True, the weights are normalized, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably too long; wrap at 79 characters.
lib/matplotlib/axes/_axes.py
Outdated
so that the integral of the density over the range remains 1. | ||
|
||
Default is ``None`` | ||
|
||
cumulative : boolean, optional | ||
If `True`, then a histogram is computed where each bin gives the | ||
counts in that bin plus all bins for smaller values. The last bin | ||
gives the total number of datapoints. If `normed` is also `True` | ||
gives the total number of datapoints. If `normed` and/or 'density' is also `True` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably too long; wrap at 79 characters.
lib/matplotlib/axes/_axes.py
Outdated
@@ -6068,6 +6076,15 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None, | |||
.. plot:: mpl_examples/statistics/histogram_demo_features.py | |||
|
|||
""" | |||
|
|||
# This sets the density variable, if necessary, to its predecessor, 'normed.' | |||
if density != None and normed != None and density != normed: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use is not None
.
lib/matplotlib/axes/_axes.py
Outdated
# This sets the density variable, if necessary, to its predecessor, 'normed.' | ||
if density != None and normed != None and density != normed: | ||
raise ValueError('The density and normed arguments represent the same concept. Please set only one of them, or set them to the same value.') | ||
elif normed != None and density == None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use is None
.
lib/matplotlib/tests/test_axes.py
Outdated
@@ -2541,6 +2541,18 @@ def test_hist_stacked_normed(): | |||
ax = fig.add_subplot(111) | |||
ax.hist((d1, d2), stacked=True, normed=True) | |||
|
|||
@image_comparison(baseline_images=['hist_stacked_normed']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two blank lines above.
lib/matplotlib/tests/test_axes.py
Outdated
@@ -3262,10 +3274,6 @@ def test_specgram_angle_freqs(): | |||
noverlap=noverlap, pad_to=pad_to, sides='onesided', | |||
mode='phase', scale='dB') | |||
|
|||
with pytest.raises(ValueError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@QuLogic it shouldn't have been. My mistake—good catch!
lib/matplotlib/tests/test_axes.py
Outdated
d1 = np.linspace(1, 3, 20) | ||
d2 = np.linspace(0, 10, 50) | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fig, ax = plt.subplots()
Mostly minor formatting things; it seems to make sense otherwise. |
If that makes your life easier, we are using the tool |
set, then that value will be used. If neither are set, then the args | ||
will be treated as 'False.' | ||
|
||
If both are set to different things, the hist function raises an |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest 'if both are set, raise' even if they are the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change made and pushed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs are out of sync with the code.
Pushed several changes as per @QuLogic's observations and suggestions! |
bdcaab5
to
932e4c8
Compare
Force pushed to remove determinism_tex.svg from 3 commits back. |
There are still a few PEP 8 violations that are causing the tests to fail (https://travis-ci.org/matplotlib/matplotlib/jobs/193148586#L1777), would it be okay to fix them? They look like they are all lines that are longer than 79 characters, which is the maximum allowed line length. |
lib/matplotlib/axes/_axes.py
Outdated
|
||
weights : (n, ) array_like or None, optional | ||
An array of weights, of the same shape as `x`. Each value in `x` | ||
only contributes its associated weight towards the bin count | ||
(instead of 1). If `normed` is True, the weights are normalized, | ||
so that the integral of the density over the range remains 1. | ||
(instead of 1). If `normed` and/or 'density' is True, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be just 'or'
lib/matplotlib/axes/_axes.py
Outdated
If `cumulative` evaluates to less than 0 (e.g., -1), the direction | ||
of accumulation is reversed. In this case, if `normed` is also | ||
`True`, then the histogram is normalized such that the first bin | ||
gives the total number of datapoints. If `normed` and/or 'density' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only 'or'
lib/matplotlib/axes/_axes.py
Outdated
@@ -6032,7 +6042,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None, | |||
Returns | |||
------- | |||
n : array or list of arrays | |||
The values of the histogram bins. See **normed** and **weights** | |||
The values of the histogram bins. See **normed or density** and **weights** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is likely too long and needs to be wrapped.
lib/matplotlib/tests/test_axes.py
Outdated
@@ -2542,6 +2542,18 @@ def test_hist_stacked_normed(): | |||
ax.hist((d1, d2), stacked=True, normed=True) | |||
|
|||
|
|||
@image_comparison(baseline_images=['hist_stacked_normed']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are going to add an image test you also need to commit the baseline images.
If this adds coverage we don't have other wise, the images (or at least the png) should be added; if not, just drop the decorator.
@chelseatroy ping on this. |
Hi @tacaswell. Just saw this. I guess for some reason I didn't receive a notification about your ping on this PR. If this still hasn't been done, I can make the changes this week...though I suspect someone has beat me to it. |
@tacaswell I guess I'm pretty lost as to what to do from here. All the build checks are failing, I don't really understand why, I basically just changed the character width of some comments. |
Presumably because of other changes since the last time you added a commit, there are conflicts so that your changes no longer merge cleanly. The "Resolve conflicts" button shows them nicely, and it looks like they will be easy to resolve. |
Looks like there are 2 formatting issues. |
elif normed is None and density is None: | ||
density = False | ||
|
||
def _normalize_input(inp, ename='input'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am confused by this? Did this come from the merge?
Hi @chelseatroy, thanks for the PR! Since there were two PRs I have merged them together with a few fixes and put them in #8993 - you will still be attributed to the work when it gets merged though. |
The hist function should now work with either 'normed' or 'density' defined.
If neither are defined, default is 'False.'
If both are defined as different things, the function raises an error.
Some tests added, and comment documentation updated, to reflect the changes.