From: <mme...@us...> - 2008-12-12 14:55:26
|
Revision: 6592 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6592&view=rev Author: mmetz_bn Date: 2008-12-12 14:55:12 +0000 (Fri, 12 Dec 2008) Log Message: ----------- Added support for weights to axes.hist() Modified Paths: -------------- trunk/matplotlib/CHANGELOG trunk/matplotlib/lib/matplotlib/axes.py Modified: trunk/matplotlib/CHANGELOG =================================================================== --- trunk/matplotlib/CHANGELOG 2008-12-12 14:47:38 UTC (rev 6591) +++ trunk/matplotlib/CHANGELOG 2008-12-12 14:55:12 UTC (rev 6592) @@ -1,3 +1,6 @@ +2008-12-12 Added support for the numpy.histogram() weights parameter + to the axes hist() method. Docs taken from numpy - MM + 2008-12-12 Fixed warning in hist() with numpy 1.2 - MM 2008-12-12 Removed external packages: configobj and enthought.traits Modified: trunk/matplotlib/lib/matplotlib/axes.py =================================================================== --- trunk/matplotlib/lib/matplotlib/axes.py 2008-12-12 14:47:38 UTC (rev 6591) +++ trunk/matplotlib/lib/matplotlib/axes.py 2008-12-12 14:55:12 UTC (rev 6592) @@ -6422,9 +6422,10 @@ #### Data analysis - def hist(self, x, bins=10, range=None, normed=False, cumulative=False, - bottom=None, histtype='bar', align='mid', - orientation='vertical', rwidth=None, log=False, **kwargs): + def hist(self, x, bins=10, range=None, normed=False, weights=None, + cumulative=False, bottom=None, histtype='bar', align='mid', + orientation='vertical', rwidth=None, log=False, + **kwargs): """ call signature:: @@ -6468,6 +6469,13 @@ pdf, bins, patches = ax.hist(...) print np.sum(pdf * np.diff(bins)) + *weights* + An array of weights, of the same shape as *x*. Each value in + *x* only contributes its associated weight towards the bin + count (instead of 1). If *normed* is True, the weights are + normalized, so that the integral of the density over the range + remains 1. + *cumulative*: If *True*, then a histogram is computed where each bin gives the counts in that bin plus all bins for smaller values. @@ -6543,7 +6551,7 @@ if not self._hold: self.cla() # NOTE: the range keyword overwrites the built-in func range !!! - # needs to be fixed in with numpy !!! + # needs to be fixed in numpy !!! if kwargs.get('width') is not None: raise DeprecationWarning( @@ -6566,8 +6574,30 @@ tx.append( np.array(x[i]) ) x = tx else: - raise ValueError, 'Can not use providet data to create a histogram' + raise ValueError, 'Can not use provided data to create a histogram' + if weights is not None: + try: + w = np.transpose(np.array(weights)) + if len(w.shape)==1: + w.shape = (1, w.shape[0]) + except: + if iterable(weights[0]) and not is_string_like(weights[0]): + tw = [] + for i in xrange(len(weights)): + tw.append( np.array(weights[i]) ) + w = tw + else: + raise ValueError, 'Can not use provided weights to create a hist' + + if len(x) != len(w): + raise ValueError, 'weights should have the same shape as x' + for i in xrange(len(x)): + if len(x[i]) != len(w[i]): + raise ValueError, 'weights should have the same shape as x' + else: + w = [None]*len(x) + # Check whether bins or range are given explicitly. In that # case do not autoscale axes. binsgiven = (cbook.iterable(bins) or range != None) @@ -6584,7 +6614,7 @@ for i in xrange(len(x)): # this will automatically overwrite bins, # so that each histogram uses the same bins - m, bins = np.histogram(x[i], bins, **hist_kwargs) + m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs) n.append(m) if cumulative: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |