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

Skip to content

Remove old normalising code from plt.hist #9121

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 4 commits into from
Oct 19, 2017

Conversation

dstansby
Copy link
Member

This PR does a few things:

for i in xrange(nx):
# this will automatically overwrite bins,
# so that each histogram uses the same bins
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
m = m.astype(float) # causes problems later if it's an int
if mlast is None:
mlast = np.zeros(len(bins)-1, m.dtype)
if density and not stacked:
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the old normalising code

if stacked:
if mlast is None:
Copy link
Member Author

Choose a reason for hiding this comment

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

This if statement is never used due to the same statement on line 6097

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Aug 30, 2017
@@ -1464,6 +1464,14 @@ def test_hist_step_filled():
assert all([p.get_facecolor() == p.get_edgecolor() for p in patches])


@image_comparison(baseline_images=['hist_density'], extensions=['png'])
def test_hist_density():
np.random.seed(0)
Copy link
Member

Choose a reason for hiding this comment

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

Can you use 19680801 (JDH's birthday) as the seed?

Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Modulo changing the seed and squashing the image changes.

@dstansby dstansby modified the milestones: 2.2 (next next feature release), 2.1 (next point release) Sep 2, 2017
np.random.seed(19680801)
data = np.random.standard_normal(2000)
fig, ax = plt.subplots()
ax.hist(data, density=True)
Copy link
Member

Choose a reason for hiding this comment

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

Does this actually need an image test, or can we just test that np.histogram properly applies the density kwarg?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think there was ever any image test that used the 'density' or 'norm' kwargs, so I thought it would be a good idea to add one.

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 we are trying to keep image tests to things where the actual image appearance might change if code changes. So sure if there is no hist image test there should be one. If such a test does exist then this just tests the value of the bars, and I think you can do that w/o an image comparison. But I’m not passionate about it or anything.

Copy link
Member

Choose a reason for hiding this comment

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

Beating a dead horse, but:

np.random.seed(19680801)
data = np.random.standard_normal(2000)
fig, ax = plt.subplots()
n, bins, patches = ax.hist(data, density=True)
assert np.isclose(np.sum(n * np.diff(bins)), 1.0)

@phobson phobson merged commit d0eb9ca into matplotlib:master Oct 19, 2017
@dstansby dstansby deleted the hist-numpy-doc branch October 19, 2017 19:15
@tacaswell
Copy link
Member

@meeseeksdev backport to v2.1.x

NelleV added a commit that referenced this pull request Oct 26, 2017
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rely on numpy to properly normalize histograms with unequal bin widths
5 participants