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

Skip to content

Commit 414a9d7

Browse files
committed
support for multi-hist with different length
svn path=/trunk/matplotlib/; revision=6055
1 parent 8b8d6c5 commit 414a9d7

3 files changed

Lines changed: 35 additions & 20 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2008-08-28 Added support for multiple histograms with data of
2+
different length - MM
3+
14
2008-08-28 Fix step plots with log scale - MGD
25

36
2008-08-28 Fix masked arrays with markers in non-Agg backends - MGD

examples/pylab_examples/histogram_demo_extended.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,16 @@
7878

7979
n, bins, patches = P.hist(x, 10, normed=1, histtype='barstacked')
8080

81+
#
82+
# finally: make a multiple-histogram of data-sets with different length
83+
#
84+
x0 = mu + sigma*P.randn(10000)
85+
x1 = mu + sigma*P.randn(7000)
86+
x2 = mu + sigma*P.randn(3000)
87+
88+
P.figure()
89+
90+
n, bins, patches = P.hist( [x0,x1,x2], 10, histtype='bar')
91+
8192

8293
P.show()

lib/matplotlib/axes.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6157,7 +6157,7 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
61576157
- 'step' generates a lineplot that is by default
61586158
unfilled
61596159
6160-
- 'stepfilled' generates a lineplot that this by default
6160+
- 'stepfilled' generates a lineplot that is by default
61616161
filled.
61626162
61636163
*align*: ['left' | 'mid' | 'right' ]
@@ -6209,26 +6209,27 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
62096209
raise DeprecationWarning(
62106210
'hist now uses the rwidth to give relative width and not absolute width')
62116211

6212-
# todo: make hist() work with list of arrays with different lengths
6213-
x = np.asarray(x).copy()
6214-
if len(x.shape)==2 and min(x.shape)==1:
6215-
x.shape = max(x.shape),
6216-
6217-
if len(x.shape)==2 and x.shape[0]<x.shape[1]:
6218-
warnings.warn('2D hist should be nsamples x nvariables; this looks transposed')
6219-
6220-
if len(x.shape)==2:
6221-
n = []
6222-
for i in xrange(x.shape[1]):
6223-
# this will automatically overwrite bins,
6224-
# so that each histogram uses the same bins
6225-
m, bins = np.histogram(x[:,i], bins, range=range,
6226-
normed=bool(normed), new=True)
6227-
n.append(m)
6228-
else:
6229-
n, bins = np.histogram(x, bins, range=range,
6212+
try:
6213+
x = np.transpose(np.asarray(x).copy())
6214+
if len(x.shape)==1:
6215+
x.shape = (1,x.shape[0])
6216+
elif len(x.shape)==2 and x.shape[1]<x.shape[0]:
6217+
warnings.warn('2D hist should be nsamples x nvariables; this looks transposed')
6218+
except ValueError:
6219+
# multiple hist with data of different length
6220+
if iterable(x[0]) and not is_string_like(x[0]):
6221+
tx = []
6222+
for i in xrange(len(x)):
6223+
tx.append( np.asarray(x[i]).copy() )
6224+
x = tx
6225+
6226+
n = []
6227+
for i in xrange(len(x)):
6228+
# this will automatically overwrite bins,
6229+
# so that each histogram uses the same bins
6230+
m, bins = np.histogram(x[i], bins, range=range,
62306231
normed=bool(normed), new=True)
6231-
n = [n,]
6232+
n.append(m)
62326233

62336234
if cumulative:
62346235
slc = slice(None)

0 commit comments

Comments
 (0)