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

Skip to content

Commit 7419d8a

Browse files
committed
a further hist() update
svn path=/trunk/matplotlib/; revision=5411
1 parent 676f82a commit 7419d8a

3 files changed

Lines changed: 60 additions & 28 deletions

File tree

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2008-06-06 hist() revision, applied ideas proposed by Erik Tollerud and
2+
Olle Engdegard: make histtype='step' unfilled by default
3+
and introduce histtype='stepfilled'; use default color
4+
cycle; introduce reverse cumulative histogram; new align
5+
keyword - MM
6+
17
2008-06-06 Fix closed polygon patch and also provide the option to
28
not close the polygon - MGD
39

examples/pylab_examples/histogram_demo_extended.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
x = mu + sigma*P.randn(10000)
1313

1414
# the histogram of the data with histtype='step'
15-
n, bins, patches = P.hist(x, 50, normed=1, histtype='step')
15+
n, bins, patches = P.hist(x, 50, normed=1, histtype='stepfilled')
1616
P.setp(patches, 'facecolor', 'g', 'alpha', 0.75)
1717

1818
# add a line showing the expected distribution
@@ -35,7 +35,6 @@
3535
P.figure()
3636

3737
n, bins, patches = P.hist(x, 50, normed=1, histtype='step', cumulative=True)
38-
P.setp(patches, 'facecolor', 'b', 'alpha', 0.75)
3938

4039
# add a line showing the expected distribution
4140
y = P.normpdf( bins, mu, sigma).cumsum()
@@ -47,13 +46,17 @@
4746
x = mu + sigma2*P.randn(10000)
4847

4948
n, bins, patches = P.hist(x, bins=bins, normed=1, histtype='step', cumulative=True)
50-
P.setp(patches, 'facecolor', 'r', 'alpha', 0.5)
5149

5250
# add a line showing the expected distribution
5351
y = P.normpdf( bins, mu, sigma2).cumsum()
5452
y /= y[-1]
5553
l = P.plot(bins, y, 'r--', linewidth=1.5)
5654

55+
# finally overplot a reverted cumulative histogram
56+
n, bins, patches = P.hist(x, bins=bins, normed=1,
57+
histtype='step', cumulative=-1)
58+
59+
5760
P.grid(True)
5861
P.ylim(0, 1.05)
5962

lib/matplotlib/axes.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5634,7 +5634,7 @@ def twiny(self):
56345634

56355635

56365636
def hist(self, x, bins=10, normed=False, cumulative=False,
5637-
bottom=None, histtype='bar', align='edge',
5637+
bottom=None, histtype='bar', align='mid',
56385638
orientation='vertical', rwidth=None, log=False, **kwargs):
56395639
"""
56405640
call signature::
@@ -5667,20 +5667,26 @@ def hist(self, x, bins=10, normed=False, cumulative=False,
56675667
cumulative:
56685668
if True then a histogram is computed where each bin
56695669
gives the counts in that bin plus all bins for smaller values.
5670-
The last bin gives the total number of datapoints. If normed is
5671-
also True then the histogram is normalized such that the last bin
5672-
equals one.
5670+
The last bin gives the total number of datapoints. If normed
5671+
is also True then the histogram is normalized such that the
5672+
last bin equals one. If cumulative evaluates to less that one
5673+
(e.g. -1), the direction of accumulation is reversed. In this
5674+
case, If normed is also True then the histogram is normalized
5675+
such that the first bin equals one.
56735676
56745677
histtype:
5675-
[ 'bar' | 'barstacked' | 'step' ] The type of histogram
5676-
to draw. 'bar' is a traditional bar-type histogram,
5678+
[ 'bar' | 'barstacked' | 'step' | 'stepfilled' ] The type of
5679+
histogram to draw. 'bar' is a traditional bar-type histogram,
56775680
'barstacked' is a bar-type histogram where multiple data are
5678-
stacked on top of each other, and 'step' generates a lineplot.
5681+
stacked on top of each other. step' generates a lineplot that
5682+
is by default unfilled, and 'stepfilled' generates a lineplot
5683+
that this by default filled.
56795684
56805685
align:
5681-
['edge' | 'center' ] Controles how the histogram is plotted.
5682-
If 'edge', bars are centered between the bin edges.
5683-
If 'center', bars are centered on the left bin edges
5686+
['left' | 'mid' | 'right' ] Controles how the histogram is
5687+
plotted. If 'left', bars are centered on the left bin edges.
5688+
If 'mid', bars are centered between the bin edges. If 'right',
5689+
bars are centered on the right bin edges.
56845690
56855691
orientation:
56865692
[ 'horizontal' | 'vertical' ] If horizontal, barh will be used
@@ -5716,7 +5722,6 @@ def hist(self, x, bins=10, normed=False, cumulative=False,
57165722
warnings.warn('2D hist should be nsamples x nvariables; this looks transposed')
57175723

57185724
if len(x.shape)==2:
5719-
57205725
n = []
57215726
for i in xrange(x.shape[1]):
57225727
# this will automatically overwrite bins,
@@ -5730,13 +5735,16 @@ def hist(self, x, bins=10, normed=False, cumulative=False,
57305735
n = [n,]
57315736

57325737
if cumulative:
5738+
slc = slice(None)
5739+
if cbook.is_numlike(cumulative):
5740+
if cumulative < 0:
5741+
slc = slice(None,None,-1)
5742+
57335743
if normed:
5734-
n = [(m * np.diff(bins)).cumsum() for m in n]
5744+
n = [(m * np.diff(bins))[slc].cumsum()[slc] for m in n]
57355745
else:
5736-
n = [m.cumsum() for m in n]
5746+
n = [m[slc].cumsum()[slc] for m in n]
57375747

5738-
ccount = 0
5739-
colors = _process_plot_var_args.defaultColors[:]
57405748
patches = []
57415749

57425750
if histtype.startswith('bar'):
@@ -5763,14 +5771,16 @@ def hist(self, x, bins=10, normed=False, cumulative=False,
57635771
else:
57645772
raise ValueError, 'invalid histtype: %s' % histtype
57655773

5766-
if align=='edge':
5774+
if align == 'mid' or align == 'edge':
57675775
boffset += 0.5*totwidth
5768-
elif align != 'center':
5776+
elif align == 'right':
5777+
boffset += totwidth
5778+
elif align != 'left' and align != 'center':
57695779
raise ValueError, 'invalid align: %s' % align
57705780

57715781
if orientation == 'horizontal':
57725782
for m in n:
5773-
color = colors[ccount % len(colors)]
5783+
color = self._get_lines._get_next_cycle_color()
57745784
patch = self.barh(bins[:-1]+boffset, m, height=width,
57755785
left=bottom, align='center', log=log,
57765786
color=color)
@@ -5779,10 +5789,9 @@ def hist(self, x, bins=10, normed=False, cumulative=False,
57795789
if bottom is None: bottom = 0.0
57805790
bottom += m
57815791
boffset += dw
5782-
ccount += 1
57835792
elif orientation == 'vertical':
57845793
for m in n:
5785-
color = colors[ccount % len(colors)]
5794+
color = self._get_lines._get_next_cycle_color()
57865795
patch = self.bar(bins[:-1]+boffset, m, width=width,
57875796
bottom=bottom, align='center', log=log,
57885797
color=color)
@@ -5791,19 +5800,20 @@ def hist(self, x, bins=10, normed=False, cumulative=False,
57915800
if bottom is None: bottom = 0.0
57925801
bottom += m
57935802
boffset += dw
5794-
ccount += 1
57955803
else:
57965804
raise ValueError, 'invalid orientation: %s' % orientation
57975805

5798-
elif histtype == 'step':
5806+
elif histtype.startswith('step'):
57995807
x = np.zeros( 2*len(bins), np.float_ )
58005808
y = np.zeros( 2*len(bins), np.float_ )
58015809

58025810
x[0::2], x[1::2] = bins, bins
58035811

5804-
if align == 'center':
5812+
if align == 'left' or align == 'center':
58055813
x -= 0.5*(bins[1]-bins[0])
5806-
elif align != 'edge':
5814+
elif align == 'right':
5815+
x += 0.5*(bins[1]-bins[0])
5816+
elif align != 'mid' and align != 'edge':
58075817
raise ValueError, 'invalid align: %s' % align
58085818

58095819
if log:
@@ -5812,14 +5822,27 @@ def hist(self, x, bins=10, normed=False, cumulative=False,
58125822
self.set_xscale('log')
58135823
elif orientation == 'vertical':
58145824
self.set_yscale('log')
5825+
5826+
fill = False
5827+
if histtype == 'stepfilled':
5828+
fill = True
5829+
elif histtype != 'step':
5830+
raise ValueError, 'invalid histtype: %s' % histtype
58155831

58165832
for m in n:
58175833
y[1:-1:2], y[2::2] = m, m
58185834
if orientation == 'horizontal':
58195835
x,y = y,x
58205836
elif orientation != 'vertical':
58215837
raise ValueError, 'invalid orientation: %s' % orientation
5822-
patches.append( self.fill(x,y,closed=False) )
5838+
5839+
color = self._get_lines._get_next_cycle_color()
5840+
if fill:
5841+
patches.append( self.fill(x, y,
5842+
closed=False, facecolor=color) )
5843+
else:
5844+
patches.append( self.fill(x, y,
5845+
closed=False, edgecolor=color, fill=False) )
58235846

58245847
# adopted from adjust_x/ylim part of the bar method
58255848
if orientation == 'horizontal':

0 commit comments

Comments
 (0)