Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: Introduced new keyword 'density' in the hist function #8408

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 4 commits into from

Conversation

moboehle
Copy link
Contributor

Introduced new keyword 'density' in the hist function to be consistent with numpy.

This pull request closes issue #7364. Contrary to what was suggested in the comments, no error is raised in the case that both keywords density and normed are used. If neither are defined, default is 'False'.

@QuLogic
Copy link
Member

QuLogic commented Mar 30, 2017

@tacaswell I thought we had a PR for this; did it not get merged?

@phobson
Copy link
Member

phobson commented Mar 30, 2017

@QuLogic see #7856

@moboehle
Copy link
Contributor Author

Sorry, I didn't see that there was a PR already, since it did not reference the issue.
The solutions to introducing 'density' as a new keyword are almost identical. However, as @tacaswell pointed out, we can pass the keyword density to np.histogram and delete the additional normalization in the hist function, in order to simplify the code. This is the main difference between the two fixes. Furthermore, in the current version of this PR, no error is raised if 'normed' and 'density' are both used but not contradicting. I think as long as there is no ambiguity in the input, no error should be raised, as it would cause an unnecessary inconvenience to the user.

@@ -6220,7 +6225,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
if cbook.is_numlike(cumulative) and cumulative < 0:
slc = slice(None, None, -1)

if normed:
if density:
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this path is never tested. Could you add a test that uses the density option?

@@ -6132,6 +6135,10 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
if histtype == 'barstacked' and not stacked:
stacked = True

if density is not None and normed is not None and normed != density:
Copy link
Member

Choose a reason for hiding this comment

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

Add a test to check this

@@ -6132,6 +6135,10 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
if histtype == 'barstacked' and not stacked:
stacked = True

if density is not None and normed is not None and normed != density:
raise ValueError("kwargs density and normed cannot have different\
Copy link
Member

Choose a reason for hiding this comment

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

Minor style comment. Can you do:

raise ValueError("kwargs density and normed cannot have different "
                 "values. Use density only, since normed will be deprecated.")

@phobson phobson added this to the 2.1 (next point release) milestone Mar 31, 2017
@tacaswell
Copy link
Member

Thanks for your work on this!

I definitely think it should raise if both kwargs are passed. If the uses passes in both, then they must be passed as identical values (so we do not have to document and people do not have understand the precedence rules). This can lead to very brittle code, will just move the frustration / debugging to a later time (when they two values get out of sync), and does not actually help improve the clarity.

@moboehle Are you comfortable enough with git to merge the two pull requests together?

@moboehle
Copy link
Contributor Author

moboehle commented Apr 2, 2017

@phobson Thanks for your feedback, I added some tests now.

@tacaswell You are right, allowing to set both 'normed' and 'density' could lead to odd behavior, I updated this now. I would be happy to merge the pull requests together, but I am not quite sure how to go about this.

Copy link
Member

@dstansby dstansby left a comment

Choose a reason for hiding this comment

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

This looks good - just need to get rid of the changes to the autogenerated file.

I also can't see any tests in the diff?

bottom=None, histtype='bar', align='mid', orientation='vertical',
rwidth=None, log=False, color=None, label=None, stacked=False,
hold=None, data=None, **kwargs):
def hist(x, bins=None, range=None, density=None, normed=None, weights=None,
Copy link
Member

Choose a reason for hiding this comment

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

lib/matplotlib/pyplot.py is an autogenerated file, so these changes will need to be reverted.

@tacaswell
Copy link
Member

#7856 has a test (that needs an image committed).

@moboehle I suggest you do the following:

git checkout bccn_hist # just to be safe
# add a remote
git remote add chelseatroy https://github.com/chelseatroy/matplotlib.git
# update your remotes
git remote update
git merge chelseatroy/histogram_compatibility_with_numpy_7364

You will probably have to clean up some conflicts, but then commit and push everything back to your branch.

You will need commit the test image results as well.

If you need help jump into https://gitter.im/matplotlib/matplotlib

@QuLogic
Copy link
Member

QuLogic commented May 28, 2017

Ping @moboehle.

@dstansby
Copy link
Member

dstansby commented Aug 5, 2017

Hi @moboehle , 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.

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