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

Skip to content

Commit bb4425a

Browse files
committed
Added support for weights to axes.hist()
svn path=/trunk/matplotlib/; revision=6592
1 parent ce66510 commit bb4425a

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2008-12-12 Added support for the numpy.histogram() weights parameter
2+
to the axes hist() method. Docs taken from numpy - MM
3+
14
2008-12-12 Fixed warning in hist() with numpy 1.2 - MM
25

36
2008-12-12 Removed external packages: configobj and enthought.traits

lib/matplotlib/axes.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6422,9 +6422,10 @@ def get_shared_y_axes(self):
64226422

64236423
#### Data analysis
64246424

6425-
def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
6426-
bottom=None, histtype='bar', align='mid',
6427-
orientation='vertical', rwidth=None, log=False, **kwargs):
6425+
def hist(self, x, bins=10, range=None, normed=False, weights=None,
6426+
cumulative=False, bottom=None, histtype='bar', align='mid',
6427+
orientation='vertical', rwidth=None, log=False,
6428+
**kwargs):
64286429
"""
64296430
call signature::
64306431
@@ -6468,6 +6469,13 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
64686469
pdf, bins, patches = ax.hist(...)
64696470
print np.sum(pdf * np.diff(bins))
64706471
6472+
*weights*
6473+
An array of weights, of the same shape as *x*. Each value in
6474+
*x* only contributes its associated weight towards the bin
6475+
count (instead of 1). If *normed* is True, the weights are
6476+
normalized, so that the integral of the density over the range
6477+
remains 1.
6478+
64716479
*cumulative*:
64726480
If *True*, then a histogram is computed where each bin
64736481
gives the counts in that bin plus all bins for smaller values.
@@ -6543,7 +6551,7 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
65436551
if not self._hold: self.cla()
65446552

65456553
# NOTE: the range keyword overwrites the built-in func range !!!
6546-
# needs to be fixed in with numpy !!!
6554+
# needs to be fixed in numpy !!!
65476555

65486556
if kwargs.get('width') is not None:
65496557
raise DeprecationWarning(
@@ -6566,7 +6574,29 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
65666574
tx.append( np.array(x[i]) )
65676575
x = tx
65686576
else:
6569-
raise ValueError, 'Can not use providet data to create a histogram'
6577+
raise ValueError, 'Can not use provided data to create a histogram'
6578+
6579+
if weights is not None:
6580+
try:
6581+
w = np.transpose(np.array(weights))
6582+
if len(w.shape)==1:
6583+
w.shape = (1, w.shape[0])
6584+
except:
6585+
if iterable(weights[0]) and not is_string_like(weights[0]):
6586+
tw = []
6587+
for i in xrange(len(weights)):
6588+
tw.append( np.array(weights[i]) )
6589+
w = tw
6590+
else:
6591+
raise ValueError, 'Can not use provided weights to create a hist'
6592+
6593+
if len(x) != len(w):
6594+
raise ValueError, 'weights should have the same shape as x'
6595+
for i in xrange(len(x)):
6596+
if len(x[i]) != len(w[i]):
6597+
raise ValueError, 'weights should have the same shape as x'
6598+
else:
6599+
w = [None]*len(x)
65706600

65716601
# Check whether bins or range are given explicitly. In that
65726602
# case do not autoscale axes.
@@ -6584,7 +6614,7 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
65846614
for i in xrange(len(x)):
65856615
# this will automatically overwrite bins,
65866616
# so that each histogram uses the same bins
6587-
m, bins = np.histogram(x[i], bins, **hist_kwargs)
6617+
m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
65886618
n.append(m)
65896619

65906620
if cumulative:

0 commit comments

Comments
 (0)