Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
phobson merged 4 commits intomatplotlib:masterfromdstansby:hist-numpy-doc
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes fromall 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
40 changes: 16 additions & 24 deletionslib/matplotlib/axes/_axes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5988,13 +5988,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
--------
hist2d : 2D histograms

Notes
-----
Until numpy release 1.5, the underlying numpy histogram function was
incorrect with ``normed=True`` if bin sizes were unequal. MPL
inherited that error. It is now corrected within MPL when using
earlier numpy versions.

"""
# Avoid shadowing the builtin.
bin_range = range
Expand DownExpand Up@@ -6087,38 +6080,37 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
else:
hist_kwargs = dict(range=bin_range)

n = []
# List to store all the top coordinates of the histograms
tops = []
mlast = None
# Loop through datasets
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
MemberAuthor

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

db = np.diff(bins)
m = (m.astype(float) / db) / m.sum()
if stacked:
if mlast is None:
Copy link
MemberAuthor

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

mlast = np.zeros(len(bins)-1, m.dtype)
m += mlast
mlast[:] = m
n.append(m)
tops.append(m)

# If a stacked density plot, normalize so the area of all the stacked
# histograms together is 1
if stacked and density:
db = np.diff(bins)
for m inn:
m[:] = (m.astype(float) / db) /n[-1].sum()
for m intops:
m[:] = (m.astype(float) / db) /tops[-1].sum()
if cumulative:
slc = slice(None)
if cbook.is_numlike(cumulative) and cumulative < 0:
slc = slice(None, None, -1)

if density:
n = [(m * np.diff(bins))[slc].cumsum()[slc] for m inn]
tops = [(m * np.diff(bins))[slc].cumsum()[slc] for m intops]
else:
n = [m[slc].cumsum()[slc] for m inn]
tops = [m[slc].cumsum()[slc] for m intops]

patches = []

Expand All@@ -6136,7 +6128,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,

if rwidth is not None:
dr = np.clip(rwidth, 0, 1)
elif (len(n) > 1 and
elif (len(tops) > 1 and
((not stacked) or rcParams['_internal.classic_mode'])):
dr = 0.8
else:
Expand All@@ -6162,7 +6154,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
_barfunc = self.bar
bottom_kwarg = 'bottom'

for m, c in zip(n, color):
for m, c in zip(tops, color):
if bottom is None:
bottom = np.zeros(len(m))
if stacked:
Expand DownExpand Up@@ -6206,7 +6198,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
# For data that is normed to form a probability density,
# set to minimum data value / logbase
# (gives 1 full tick-label unit for the lowest filled bin)
ndata = np.array(n)
ndata = np.array(tops)
minimum = (np.min(ndata[ndata > 0])) / logbase
else:
# For non-normed (density = False) data,
Expand All@@ -6229,7 +6221,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
fill = (histtype == 'stepfilled')

xvals, yvals = [], []
for m inn:
for m intops:
if stacked:
# starting point for drawing polygon
y[0] = y[1]
Expand DownExpand Up@@ -6292,9 +6284,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
p.set_label('_nolegend_')

if nx == 1:
returnn[0], bins, cbook.silent_list('Patch', patches[0])
returntops[0], bins, cbook.silent_list('Patch', patches[0])
else:
returnn, bins, cbook.silent_list('Lists of Patches', patches)
returntops, bins, cbook.silent_list('Lists of Patches', patches)

@_preprocess_data(replace_names=["x", "y", "weights"], label_namer=None)
def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,
Expand Down
View file
Open in desktop
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletionslib/matplotlib/tests/test_axes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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(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 thatnp.histogram properly applies the density kwarg?

Copy link
MemberAuthor

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)assertnp.isclose(np.sum(n*np.diff(bins)),1.0)



@image_comparison(baseline_images=['hist_step_log_bottom'],
remove_text=True, extensions=['png'])
def test_hist_step_log_bottom():
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp