CMS 3D CMS Logo

Functions | Variables

mpl_axes_hist_fix Namespace Reference

Functions

def hist

Variables

 is_sequence_of_strings = cbook.is_sequence_of_strings
 is_string_like = cbook.is_string_like
 iterable = cbook.iterable

Function Documentation

def mpl_axes_hist_fix::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,
  color = None,
  label = None,
  kwargs 
)
call signature::

  hist(x, bins=10, range=None, normed=False, cumulative=False,
       bottom=None, histtype='bar', align='mid',
       orientation='vertical', rwidth=None, log=False, **kwargs)

Compute and draw the histogram of *x*. The return value is a
tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
[*patches0*, *patches1*,...]) if the input contains multiple
data.

Multiple data can be provided via *x* as a list of datasets
of potentially different length ([*x0*, *x1*, ...]), or as
a 2-D ndarray in which each column is a dataset.  Note that
the ndarray form is transposed relative to the list form.

Masked arrays are not supported at present.

Keyword arguments:

  *bins*:
    Either an integer number of bins or a sequence giving the
    bins.  If *bins* is an integer, *bins* + 1 bin edges
    will be returned, consistent with :func:`numpy.histogram`
    for numpy version >= 1.3, and with the *new* = True argument
    in earlier versions.
    Unequally spaced bins are supported if *bins* is a sequence.

  *range*:
    The lower and upper range of the bins. Lower and upper outliers
    are ignored. If not provided, *range* is (x.min(), x.max()).
    Range has no effect if *bins* is a sequence.

    If *bins* is a sequence or *range* is specified, autoscaling
    is based on the specified bin range instead of the
    range of x.

  *normed*:
    If *True*, the first element of the return tuple will
    be the counts normalized to form a probability density, i.e.,
    ``n/(len(x)*dbin)``.  In a probability density, the integral of
    the histogram should be 1; you can verify that with a
    trapezoidal integration of the probability density function::

      pdf, bins, patches = ax.hist(...)
      print np.sum(pdf * np.diff(bins))

    .. Note:: Until numpy release 1.5, the underlying numpy
              histogram function was incorrect with *normed*=*True*
              if bin sizes were unequal.  MPL inherited that
              error.  It is now corrected within MPL when using
              earlier numpy versions

  *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.
    The last bin gives the total number of datapoints.  If *normed*
    is also *True* then the histogram is normalized such that the
    last bin equals 1. If *cumulative* evaluates to less than 0
    (e.g. -1), the direction of accumulation is reversed.  In this
    case, if *normed* is also *True*, then the histogram is normalized
    such that the first bin equals 1.

  *histtype*: [ 'bar' | 'barstacked' | 'step' | 'stepfilled' ]
    The type of histogram to draw.

      - 'bar' is a traditional bar-type histogram.  If multiple data
        are given the bars are aranged side by side.

      - 'barstacked' is a bar-type histogram where multiple
        data are stacked on top of each other.

      - 'step' generates a lineplot that is by default
        unfilled.

      - 'stepfilled' generates a lineplot that is by default
        filled.

  *align*: ['left' | 'mid' | 'right' ]
    Controls how the histogram is plotted.

      - 'left': bars are centered on the left bin edges.

      - 'mid': bars are centered between the bin edges.

      - 'right': bars are centered on the right bin edges.

  *orientation*: [ 'horizontal' | 'vertical' ]
    If 'horizontal', :func:`~matplotlib.pyplot.barh` will be
    used for bar-type histograms and the *bottom* kwarg will be
    the left edges.

  *rwidth*:
    The relative width of the bars as a fraction of the bin
    width.  If *None*, automatically compute the width. Ignored
    if *histtype* = 'step' or 'stepfilled'.

  *log*:
    If *True*, the histogram axis will be set to a log scale.
    If *log* is *True* and *x* is a 1D array, empty bins will
    be filtered out and only the non-empty (*n*, *bins*,
    *patches*) will be returned.

  *color*:
    Color spec or sequence of color specs, one per
    dataset.  Default (*None*) uses the standard line
    color sequence.

  *label*:
    String, or sequence of strings to match multiple
    datasets.  Bar charts yield multiple patches per
    dataset, but only the first gets the label, so
    that the legend command will work as expected::

        ax.hist(10+2*np.random.randn(1000), label='men')
        ax.hist(12+3*np.random.randn(1000), label='women', alpha=0.5)
        ax.legend()

kwargs are used to update the properties of the
:class:`~matplotlib.patches.Patch` instances returned by *hist*:

%(Patch)s

**Example:**

.. plot:: mpl_examples/pylab_examples/histogram_demo.py

Definition at line 14 of file mpl_axes_hist_fix.py.

00019                   :
00020     """
00021     call signature::
00022 
00023       hist(x, bins=10, range=None, normed=False, cumulative=False,
00024            bottom=None, histtype='bar', align='mid',
00025            orientation='vertical', rwidth=None, log=False, **kwargs)
00026 
00027     Compute and draw the histogram of *x*. The return value is a
00028     tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
00029     [*patches0*, *patches1*,...]) if the input contains multiple
00030     data.
00031 
00032     Multiple data can be provided via *x* as a list of datasets
00033     of potentially different length ([*x0*, *x1*, ...]), or as
00034     a 2-D ndarray in which each column is a dataset.  Note that
00035     the ndarray form is transposed relative to the list form.
00036 
00037     Masked arrays are not supported at present.
00038 
00039     Keyword arguments:
00040 
00041       *bins*:
00042         Either an integer number of bins or a sequence giving the
00043         bins.  If *bins* is an integer, *bins* + 1 bin edges
00044         will be returned, consistent with :func:`numpy.histogram`
00045         for numpy version >= 1.3, and with the *new* = True argument
00046         in earlier versions.
00047         Unequally spaced bins are supported if *bins* is a sequence.
00048 
00049       *range*:
00050         The lower and upper range of the bins. Lower and upper outliers
00051         are ignored. If not provided, *range* is (x.min(), x.max()).
00052         Range has no effect if *bins* is a sequence.
00053 
00054         If *bins* is a sequence or *range* is specified, autoscaling
00055         is based on the specified bin range instead of the
00056         range of x.
00057 
00058       *normed*:
00059         If *True*, the first element of the return tuple will
00060         be the counts normalized to form a probability density, i.e.,
00061         ``n/(len(x)*dbin)``.  In a probability density, the integral of
00062         the histogram should be 1; you can verify that with a
00063         trapezoidal integration of the probability density function::
00064 
00065           pdf, bins, patches = ax.hist(...)
00066           print np.sum(pdf * np.diff(bins))
00067 
00068         .. Note:: Until numpy release 1.5, the underlying numpy
00069                   histogram function was incorrect with *normed*=*True*
00070                   if bin sizes were unequal.  MPL inherited that
00071                   error.  It is now corrected within MPL when using
00072                   earlier numpy versions
00073 
00074       *weights*
00075         An array of weights, of the same shape as *x*.  Each value in
00076         *x* only contributes its associated weight towards the bin
00077         count (instead of 1).  If *normed* is True, the weights are
00078         normalized, so that the integral of the density over the range
00079         remains 1.
00080 
00081       *cumulative*:
00082         If *True*, then a histogram is computed where each bin
00083         gives the counts in that bin plus all bins for smaller values.
00084         The last bin gives the total number of datapoints.  If *normed*
00085         is also *True* then the histogram is normalized such that the
00086         last bin equals 1. If *cumulative* evaluates to less than 0
00087         (e.g. -1), the direction of accumulation is reversed.  In this
00088         case, if *normed* is also *True*, then the histogram is normalized
00089         such that the first bin equals 1.
00090 
00091       *histtype*: [ 'bar' | 'barstacked' | 'step' | 'stepfilled' ]
00092         The type of histogram to draw.
00093 
00094           - 'bar' is a traditional bar-type histogram.  If multiple data
00095             are given the bars are aranged side by side.
00096 
00097           - 'barstacked' is a bar-type histogram where multiple
00098             data are stacked on top of each other.
00099 
00100           - 'step' generates a lineplot that is by default
00101             unfilled.
00102 
00103           - 'stepfilled' generates a lineplot that is by default
00104             filled.
00105 
00106       *align*: ['left' | 'mid' | 'right' ]
00107         Controls how the histogram is plotted.
00108 
00109           - 'left': bars are centered on the left bin edges.
00110 
00111           - 'mid': bars are centered between the bin edges.
00112 
00113           - 'right': bars are centered on the right bin edges.
00114 
00115       *orientation*: [ 'horizontal' | 'vertical' ]
00116         If 'horizontal', :func:`~matplotlib.pyplot.barh` will be
00117         used for bar-type histograms and the *bottom* kwarg will be
00118         the left edges.
00119 
00120       *rwidth*:
00121         The relative width of the bars as a fraction of the bin
00122         width.  If *None*, automatically compute the width. Ignored
00123         if *histtype* = 'step' or 'stepfilled'.
00124 
00125       *log*:
00126         If *True*, the histogram axis will be set to a log scale.
00127         If *log* is *True* and *x* is a 1D array, empty bins will
00128         be filtered out and only the non-empty (*n*, *bins*,
00129         *patches*) will be returned.
00130 
00131       *color*:
00132         Color spec or sequence of color specs, one per
00133         dataset.  Default (*None*) uses the standard line
00134         color sequence.
00135 
00136       *label*:
00137         String, or sequence of strings to match multiple
00138         datasets.  Bar charts yield multiple patches per
00139         dataset, but only the first gets the label, so
00140         that the legend command will work as expected::
00141 
00142             ax.hist(10+2*np.random.randn(1000), label='men')
00143             ax.hist(12+3*np.random.randn(1000), label='women', alpha=0.5)
00144             ax.legend()
00145 
00146     kwargs are used to update the properties of the
00147     :class:`~matplotlib.patches.Patch` instances returned by *hist*:
00148 
00149     %(Patch)s
00150 
00151     **Example:**
00152 
00153     .. plot:: mpl_examples/pylab_examples/histogram_demo.py
00154     """
00155     if not self._hold: self.cla()
00156 
00157     # NOTE: the range keyword overwrites the built-in func range !!!
00158     #       needs to be fixed in numpy                           !!!
00159 
00160     # Validate string inputs here so we don't have to clutter
00161     # subsequent code.
00162     if histtype not in ['bar', 'barstacked', 'step', 'stepfilled']:
00163         raise ValueError("histtype %s is not recognized" % histtype)
00164 
00165     if align not in ['left', 'mid', 'right']:
00166         raise ValueError("align kwarg %s is not recognized" % align)
00167 
00168     if orientation not in [ 'horizontal', 'vertical']:
00169         raise ValueError(
00170             "orientation kwarg %s is not recognized" % orientation)
00171 
00172 
00173     if kwargs.get('width') is not None:
00174         raise DeprecationWarning(
00175             'hist now uses the rwidth to give relative width '
00176             'and not absolute width')
00177 
00178     # Massage 'x' for processing.
00179     # NOTE: Be sure any changes here is also done below to 'weights'
00180     if isinstance(x, np.ndarray) or not iterable(x[0]):
00181         # TODO: support masked arrays;
00182         x = np.asarray(x)
00183         if x.ndim == 2:
00184             x = x.T # 2-D input with columns as datasets; switch to rows
00185         elif x.ndim == 1:
00186             x = x.reshape(1, x.shape[0])  # new view, single row
00187         else:
00188             raise ValueError("x must be 1D or 2D")
00189         if x.shape[1] < x.shape[0]:
00190             warnings.warn('2D hist input should be nsamples x nvariables;\n '
00191                 'this looks transposed (shape is %d x %d)' % x.shape[::-1])
00192     else:
00193         # multiple hist with data of different length
00194         x = [np.array(xi) for xi in x]
00195 
00196     nx = len(x) # number of datasets
00197 
00198     if color is None:
00199         color = [self._get_lines.color_cycle.next()
00200                                         for i in xrange(nx)]
00201     else:
00202         color = mcolors.colorConverter.to_rgba_array(color)
00203         if len(color) != nx:
00204             raise ValueError("color kwarg must have one color per dataset")
00205 
00206     # We need to do to 'weights' what was done to 'x'
00207     if weights is not None:
00208         if isinstance(weights, np.ndarray) or not iterable(weights[0]) :
00209             w = np.array(weights)
00210             if w.ndim == 2:
00211                 w = w.T
00212             elif w.ndim == 1:
00213                 w.shape = (1, w.shape[0])
00214             else:
00215                 raise ValueError("weights must be 1D or 2D")
00216         else:
00217             w = [np.array(wi) for wi in weights]
00218 
00219         if len(w) != nx:
00220             raise ValueError('weights should have the same shape as x')
00221         for i in xrange(nx):
00222             if len(w[i]) != len(x[i]):
00223                 raise ValueError(
00224                     'weights should have the same shape as x')
00225     else:
00226         w = [None]*nx
00227 
00228 
00229     # Save autoscale state for later restoration; turn autoscaling
00230     # off so we can do it all a single time at the end, instead
00231     # of having it done by bar or fill and then having to be redone.
00232     _saved_autoscalex = self.get_autoscalex_on()
00233     _saved_autoscaley = self.get_autoscaley_on()
00234     self.set_autoscalex_on(False)
00235     self.set_autoscaley_on(False)
00236 
00237     # Save the datalimits for the same reason:
00238     _saved_bounds = self.dataLim.bounds
00239 
00240     # Check whether bins or range are given explicitly. In that
00241     # case use those values for autoscaling.
00242     binsgiven = (cbook.iterable(bins) or range != None)
00243 
00244     # If bins are not specified either explicitly or via range,
00245     # we need to figure out the range required for all datasets,
00246     # and supply that to np.histogram.
00247     if not binsgiven:
00248         xmin = np.inf
00249         xmax = -np.inf
00250         for xi in x:
00251             xmin = min(xmin, xi.min())
00252             xmax = max(xmax, xi.max())
00253         range = (xmin, xmax)
00254 
00255     #hist_kwargs = dict(range=range, normed=bool(normed))
00256     # We will handle the normed kwarg within mpl until we
00257     # get to the point of requiring numpy >= 1.5.
00258     hist_kwargs = dict(range=range)
00259     if np.__version__ < "1.3": # version 1.1 and 1.2
00260         hist_kwargs['new'] = True
00261 
00262     n = []
00263     for i in xrange(nx):
00264         # this will automatically overwrite bins,
00265         # so that each histogram uses the same bins
00266         m, bins = np.histogram(x[i], bins, weights=w[i], **hist_kwargs)
00267         if normed:
00268             db = np.diff(bins)
00269             m = (m.astype(float) / db) / m.sum()
00270         n.append(m)
00271     if normed and db.std() > 0.01 * db.mean():
00272         warnings.warn("""
00273         This release fixes a normalization bug in the NumPy histogram
00274         function prior to version 1.5, occuring with non-uniform
00275         bin widths. The returned and plotted value is now a density:
00276             n / (N * bin width),
00277         where n is the bin count and N the total number of points.
00278         """)
00279 
00280 
00281 
00282     if cumulative:
00283         slc = slice(None)
00284         if cbook.is_numlike(cumulative) and cumulative < 0:
00285             slc = slice(None,None,-1)
00286 
00287         if normed:
00288             n = [(m * np.diff(bins))[slc].cumsum()[slc] for m in n]
00289         else:
00290             n = [m[slc].cumsum()[slc] for m in n]
00291 
00292     patches = []
00293 
00294     if histtype.startswith('bar'):
00295         totwidth = np.diff(bins)
00296 
00297         if rwidth is not None:
00298             dr = min(1.0, max(0.0, rwidth))
00299         elif len(n)>1:
00300             dr = 0.8
00301         else:
00302             dr = 1.0
00303 
00304         if histtype=='bar':
00305             width = dr*totwidth/nx
00306             dw = width
00307 
00308             if nx > 1:
00309                 boffset = -0.5*dr*totwidth*(1.0-1.0/nx)
00310             else:
00311                 boffset = 0.0
00312             stacked = False
00313         elif histtype=='barstacked':
00314             width = dr*totwidth
00315             boffset, dw = 0.0, 0.0
00316             stacked = True
00317 
00318         if align == 'mid' or align == 'edge':
00319             boffset += 0.5*totwidth
00320         elif align == 'right':
00321             boffset += totwidth
00322 
00323         if orientation == 'horizontal':
00324             _barfunc = self.barh
00325         else:  # orientation == 'vertical'
00326             _barfunc = self.bar
00327 
00328         for m, c in zip(n, color):
00329             patch = _barfunc(bins[:-1]+boffset, m, width, bottom,
00330                               align='center', log=log,
00331                               color=c)
00332             patches.append(patch)
00333             if stacked:
00334                 if bottom is None:
00335                     bottom = 0.0
00336                 bottom += m
00337             boffset += dw
00338 
00339     elif histtype.startswith('step'):
00340         x = np.zeros( 2*len(bins), np.float )
00341         y = np.zeros( 2*len(bins), np.float )
00342 
00343         x[0::2], x[1::2] = bins, bins
00344 
00345         # FIX FIX FIX
00346         # This is the only real change.
00347         # minimum = min(bins)
00348         if log is True:
00349             minimum = 1.0
00350         elif log:
00351             minimum = float(log)
00352         else:
00353             minimum = 0.0
00354         # FIX FIX FIX end
00355 
00356         if align == 'left' or align == 'center':
00357             x -= 0.5*(bins[1]-bins[0])
00358         elif align == 'right':
00359             x += 0.5*(bins[1]-bins[0])
00360 
00361         if log:
00362             y[0],y[-1] = minimum, minimum
00363             if orientation == 'horizontal':
00364                 self.set_xscale('log')
00365             else:  # orientation == 'vertical'
00366                 self.set_yscale('log')
00367 
00368         fill = (histtype == 'stepfilled')
00369 
00370         for m, c in zip(n, color):
00371             y[1:-1:2], y[2::2] = m, m
00372             if log:
00373                 y[y<minimum]=minimum
00374             if orientation == 'horizontal':
00375                 x,y = y,x
00376 
00377             if fill:
00378                 patches.append( self.fill(x, y,
00379                     closed=False, facecolor=c) )
00380             else:
00381                 patches.append( self.fill(x, y,
00382                     closed=False, edgecolor=c, fill=False) )
00383 
00384         # adopted from adjust_x/ylim part of the bar method
00385         if orientation == 'horizontal':
00386             xmin0 = max(_saved_bounds[0]*0.9, minimum)
00387             xmax = self.dataLim.intervalx[1]
00388             for m in n:
00389                 xmin = np.amin(m[m!=0]) # filter out the 0 height bins
00390             xmin = max(xmin*0.9, minimum)
00391             xmin = min(xmin0, xmin)
00392             self.dataLim.intervalx = (xmin, xmax)
00393         elif orientation == 'vertical':
00394             ymin0 = max(_saved_bounds[1]*0.9, minimum)
00395             ymax = self.dataLim.intervaly[1]
00396             for m in n:
00397                 ymin = np.amin(m[m!=0]) # filter out the 0 height bins
00398             ymin = max(ymin*0.9, minimum)
00399             ymin = min(ymin0, ymin)
00400             self.dataLim.intervaly = (ymin, ymax)
00401 
00402     if label is None:
00403         labels = ['_nolegend_']
00404     elif is_string_like(label):
00405         labels = [label]
00406     elif is_sequence_of_strings(label):
00407         labels = list(label)
00408     else:
00409         raise ValueError(
00410             'invalid label: must be string or sequence of strings')
00411     if len(labels) < nx:
00412         labels += ['_nolegend_'] * (nx - len(labels))
00413 
00414     for (patch, lbl) in zip(patches, labels):
00415         for p in patch:
00416             p.update(kwargs)
00417             p.set_label(lbl)
00418             lbl = '_nolegend_'
00419 
00420     if binsgiven:
00421         if orientation == 'vertical':
00422             self.update_datalim([(bins[0],0), (bins[-1],0)], updatey=False)
00423         else:
00424             self.update_datalim([(0,bins[0]), (0,bins[-1])], updatex=False)
00425 
00426     self.set_autoscalex_on(_saved_autoscalex)
00427     self.set_autoscaley_on(_saved_autoscaley)
00428     self.autoscale_view()
00429 
00430     if nx == 1:
00431         return n[0], bins, cbook.silent_list('Patch', patches[0])
00432     else:
00433         return n, bins, cbook.silent_list('Lists of Patches', patches)

Variable Documentation

mpl_axes_hist_fix::is_sequence_of_strings = cbook.is_sequence_of_strings

Definition at line 12 of file mpl_axes_hist_fix.py.

mpl_axes_hist_fix::is_string_like = cbook.is_string_like

Definition at line 11 of file mpl_axes_hist_fix.py.

mpl_axes_hist_fix::iterable = cbook.iterable

Definition at line 10 of file mpl_axes_hist_fix.py.