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

Commitd0eb9ca

Browse files
authored
Merge pull request#9121 from dstansby/hist-numpy-doc
Remove old normalising code from plt.hist
2 parents11dcf76 +2dde48a commitd0eb9ca

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

‎lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6103,13 +6103,6 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
61036103
--------
61046104
hist2d : 2D histograms
61056105
6106-
Notes
6107-
-----
6108-
Until numpy release 1.5, the underlying numpy histogram function was
6109-
incorrect with ``normed=True`` if bin sizes were unequal. MPL
6110-
inherited that error. It is now corrected within MPL when using
6111-
earlier numpy versions.
6112-
61136106
"""
61146107
# Avoid shadowing the builtin.
61156108
bin_range=range
@@ -6202,38 +6195,37 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62026195
else:
62036196
hist_kwargs=dict(range=bin_range)
62046197

6205-
n= []
6198+
# List to store all the top coordinates of the histograms
6199+
tops= []
62066200
mlast=None
6201+
# Loop through datasets
62076202
foriinxrange(nx):
62086203
# this will automatically overwrite bins,
62096204
# so that each histogram uses the same bins
62106205
m,bins=np.histogram(x[i],bins,weights=w[i],**hist_kwargs)
62116206
m=m.astype(float)# causes problems later if it's an int
62126207
ifmlastisNone:
62136208
mlast=np.zeros(len(bins)-1,m.dtype)
6214-
ifdensityandnotstacked:
6215-
db=np.diff(bins)
6216-
m= (m.astype(float)/db)/m.sum()
62176209
ifstacked:
6218-
ifmlastisNone:
6219-
mlast=np.zeros(len(bins)-1,m.dtype)
62206210
m+=mlast
62216211
mlast[:]=m
6222-
n.append(m)
6212+
tops.append(m)
62236213

6214+
# If a stacked density plot, normalize so the area of all the stacked
6215+
# histograms together is 1
62246216
ifstackedanddensity:
62256217
db=np.diff(bins)
6226-
forminn:
6227-
m[:]= (m.astype(float)/db)/n[-1].sum()
6218+
formintops:
6219+
m[:]= (m.astype(float)/db)/tops[-1].sum()
62286220
ifcumulative:
62296221
slc=slice(None)
62306222
ifcbook.is_numlike(cumulative)andcumulative<0:
62316223
slc=slice(None,None,-1)
62326224

62336225
ifdensity:
6234-
n= [(m*np.diff(bins))[slc].cumsum()[slc]forminn]
6226+
tops= [(m*np.diff(bins))[slc].cumsum()[slc]formintops]
62356227
else:
6236-
n= [m[slc].cumsum()[slc]forminn]
6228+
tops= [m[slc].cumsum()[slc]formintops]
62376229

62386230
patches= []
62396231

@@ -6251,7 +6243,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62516243

62526244
ifrwidthisnotNone:
62536245
dr=np.clip(rwidth,0,1)
6254-
elif (len(n)>1and
6246+
elif (len(tops)>1and
62556247
((notstacked)orrcParams['_internal.classic_mode'])):
62566248
dr=0.8
62576249
else:
@@ -6277,7 +6269,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
62776269
_barfunc=self.bar
62786270
bottom_kwarg='bottom'
62796271

6280-
form,cinzip(n,color):
6272+
form,cinzip(tops,color):
62816273
ifbottomisNone:
62826274
bottom=np.zeros(len(m))
62836275
ifstacked:
@@ -6321,7 +6313,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63216313
# For data that is normed to form a probability density,
63226314
# set to minimum data value / logbase
63236315
# (gives 1 full tick-label unit for the lowest filled bin)
6324-
ndata=np.array(n)
6316+
ndata=np.array(tops)
63256317
minimum= (np.min(ndata[ndata>0]))/logbase
63266318
else:
63276319
# For non-normed (density = False) data,
@@ -6344,7 +6336,7 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
63446336
fill= (histtype=='stepfilled')
63456337

63466338
xvals,yvals= [], []
6347-
forminn:
6339+
formintops:
63486340
ifstacked:
63496341
# starting point for drawing polygon
63506342
y[0]=y[1]
@@ -6407,9 +6399,9 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
64076399
p.set_label('_nolegend_')
64086400

64096401
ifnx==1:
6410-
returnn[0],bins,cbook.silent_list('Patch',patches[0])
6402+
returntops[0],bins,cbook.silent_list('Patch',patches[0])
64116403
else:
6412-
returnn,bins,cbook.silent_list('Lists of Patches',patches)
6404+
returntops,bins,cbook.silent_list('Lists of Patches',patches)
64136405

64146406
@_preprocess_data(replace_names=["x","y","weights"],label_namer=None)
64156407
defhist2d(self,x,y,bins=10,range=None,normed=False,weights=None,

‎lib/matplotlib/tests/test_axes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,14 @@ def test_hist_step_filled():
15331533
assertall([p.get_facecolor()==p.get_edgecolor()forpinpatches])
15341534

15351535

1536+
@image_comparison(baseline_images=['hist_density'],extensions=['png'])
1537+
deftest_hist_density():
1538+
np.random.seed(19680801)
1539+
data=np.random.standard_normal(2000)
1540+
fig,ax=plt.subplots()
1541+
ax.hist(data,density=True)
1542+
1543+
15361544
@image_comparison(baseline_images=['hist_step_log_bottom'],
15371545
remove_text=True,extensions=['png'])
15381546
deftest_hist_step_log_bottom():

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp