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

Skip to content

Commit a782092

Browse files
committed
added cumulative histograms
svn path=/trunk/matplotlib/; revision=5147
1 parent b75f7eb commit a782092

3 files changed

Lines changed: 52 additions & 3 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2008-05-16 Added 'cumulative' keyword arg to hist to plot cumulative
2+
histograms. For normed hists, this is normalized to one
3+
assuming equally spaced bins - MM
4+
15
2008-05-15 Fix Tk backend segfault on some machines - MGD
26

37
2008-05-14 Don't use stat on Windows (fixes font embedding problem) - MGD
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python
2+
from pylab import *
3+
4+
mu, sigma = 100, 25
5+
x = mu + sigma*randn(10000)
6+
7+
# the histogram of the data
8+
n, bins, patches = hist(x, 50, normed=1, histtype='step', cumulative=True)
9+
setp(patches, 'facecolor', 'g', 'alpha', 0.75)
10+
11+
# add a 'best fit' line
12+
y = normpdf( bins, mu, sigma).cumsum()
13+
y /= y[-1]
14+
l = plot(bins, y, 'k--', linewidth=1.5)
15+
16+
# overlay the first histogram with a second one
17+
# were the data has a smaller standard deviation
18+
mu, sigma = 100, 10
19+
x = mu + sigma*randn(10000)
20+
21+
n, bins, patches = hist(x, bins=bins, normed=1, histtype='step', cumulative=True)
22+
setp(patches, 'facecolor', 'r', 'alpha', 0.25)
23+
24+
# add a 'best fit' line
25+
y = normpdf( bins, mu, sigma).cumsum()
26+
y /= y[-1]
27+
l = plot(bins, y, 'k--', linewidth=1.5)
28+
29+
grid(True)
30+
ylim(0, 1.1)
31+
32+
#savefig('histogram_demo',dpi=72)
33+
show()

lib/matplotlib/axes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5416,9 +5416,9 @@ def twiny(self):
54165416
#### Data analysis
54175417

54185418

5419-
def hist(self, x, bins=10, normed=False, bottom=None, histtype='bar',
5420-
align='edge', orientation='vertical', width=None,
5421-
log=False, **kwargs):
5419+
def hist(self, x, bins=10, normed=False, cumulative=False,
5420+
bottom=None, histtype='bar', align='edge',
5421+
orientation='vertical', width=None, log=False, **kwargs):
54225422
"""
54235423
HIST(x, bins=10, normed=False, bottom=None, histtype='bar',
54245424
align='edge', orientation='vertical', width=None,
@@ -5439,6 +5439,12 @@ def hist(self, x, bins=10, normed=False, bottom=None, histtype='bar',
54395439
pdf, bins, patches = ax.hist(...)
54405440
print np.trapz(pdf, bins)
54415441
5442+
If cumulative is True then histogram is computed where each bin
5443+
gives the counts in that bin plus all bins for smaller values.
5444+
The last bins gives the total number of datapoints. If normed is
5445+
also True then the histogram is normalized such that the last bin
5446+
equals one (assuming equally spaced bins).
5447+
54425448
histtype = 'bar' | 'step'. The type of histogram to draw.
54435449
'bar' is a traditional bar-type histogram, 'step' generates
54445450
a lineplot.
@@ -5461,6 +5467,12 @@ def hist(self, x, bins=10, normed=False, bottom=None, histtype='bar',
54615467
if not self._hold: self.cla()
54625468
n, bins = np.histogram(x, bins, range=None,
54635469
normed=bool(normed), new=True)
5470+
5471+
if cumulative:
5472+
n = n.cumsum()
5473+
if normed:
5474+
# normalize to 1
5475+
n *= (bins[1]-bins[0])
54645476

54655477
if histtype == 'bar':
54665478
if width is None:

0 commit comments

Comments
 (0)