Ejemplo n.º 1
0
def kde(data, bins=None, kernel=None, **kwargs):
    """ kde(a, bins=None, range=None, **kwargs)
    
    Make a kernerl density estimate plot of the data. This is like a 
    histogram, but produces a smoother result, thereby better represening
    the probability density function.
    
    See the vv.StatData for more statistics on data.
    
    Parameters
    ----------
    a : array_like
        The data to calculate the historgam of.
    bins : int (optional)
        The number of bins. If not given, the best number of bins is 
        determined automatically using the Freedman-Diaconis rule.
    kernel : float or sequence (optional)
        The kernel to use for distributing the values. If a scalar is given,
        a Gaussian kernel with a sigma equal to the given number is used.
        If not given, the best kernel is chosen baded on the number of bins.
    kwargs : keyword arguments
        These are given to the plot function.
    
    """

    # Get stats
    from visvis.processing.statistics import StatData

    stats = StatData(data)

    # Get kde
    xx, values = stats.kde(bins, kernel)

    # Plot
    return vv.plot(xx, values, **kwargs)
Ejemplo n.º 2
0
Archivo: kde.py Proyecto: chiluf/visvis
def kde(data, bins=None, kernel=None, **kwargs):
    """ kde(a, bins=None, range=None, **kwargs)
    
    Make a kernerl density estimate plot of the data. This is like a 
    histogram, but produces a smoother result, thereby better represening
    the probability density function.
    
    See the vv.StatData for more statistics on data.
    
    Parameters
    ----------
    a : array_like
        The data to calculate the historgam of.
    bins : int (optional)
        The number of bins. If not given, the best number of bins is 
        determined automatically using the Freedman-Diaconis rule.
    kernel : float or sequence (optional)
        The kernel to use for distributing the values. If a scalar is given,
        a Gaussian kernel with a sigma equal to the given number is used.
        If not given, the best kernel is chosen baded on the number of bins.
    kwargs : keyword arguments
        These are given to the plot function.
    
    """

    # Get stats
    from visvis.processing.statistics import StatData
    stats = StatData(data)

    # Get kde
    xx, values = stats.kde(bins, kernel)

    # Plot
    return vv.plot(xx, values, **kwargs)
Ejemplo n.º 3
0
def hist(data, bins=None, drange=None, normed=False, weights=None):
    """ hist(a, bins=None, range=None, normed=False, weights=None)
    
    Make a histogram plot of the data. Uses np.histogram (new version) 
    internally. See its docs for more information. 
    
    See the kde() function for a more accurate density estimate.
    See the vv.StatData for more statistics on data.
    
    Parameters
    ----------
    a : array_like
        The data to calculate the historgam of.
    bins : int or sequence of scalars, optional
        If `bins` is an int, it defines the number of equal-width bins in
        the given range. If `bins` is a sequence, it defines the bin edges, 
        including the rightmost edge, allowing for non-uniform bin widths.
        If bins is not given, the best number of bins is determined
        automatically using the Freedman-Diaconis rule.
    range : (float, float)
        The lower and upper range of the bins. If not provided, range is
        simply (a.min(), a.max()). Values outside the range are ignored. 
    normed : bool
        If False, the result will contain the number of samples in each bin. 
        If True, the result is the value of the probability *density* 
        function at the bin, normalized such that the *integral* over the 
        range is 1. Note that the sum of the histogram values will not be 
        equal to 1 unless bins of unity width are chosen; it is not a 
        probability *mass* function.
    weights : array_like
        An array of weights, of the same shape as `a`. Each value in `a` 
        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.     
    
    """
    
    # Auto determine bins?
    if bins is None:
        from visvis.processing.statistics import StatData
        stats = StatData(data)
        bins = stats.best_number_of_bins()
    
    # let numpy do the work
    if np.__version__ < '1.3':
        values, edges = np.histogram(data, bins, drange, normed, weights, new=True)
    else:
        values, edges = np.histogram(data, bins, drange, normed, weights)
    
    # the bins are the left bin edges, let's get the centers
    centers = np.empty(values.shape, np.float64)
    for i in range(len(values)):
        centers[i] = (edges[i] + edges[i+1]) * 0.5
    
    # plot
    dbin = centers[1] - centers[0]
    return vv.bar(centers, values, width=dbin*0.9)
Ejemplo n.º 4
0
def hist(data, bins=None, drange=None, normed=False, weights=None):
    """ hist(a, bins=None, range=None, normed=False, weights=None)
    
    Make a histogram plot of the data. Uses np.histogram (new version)
    internally. See its docs for more information.
    
    See the kde() function for a more accurate density estimate.
    See the vv.StatData for more statistics on data.
    
    Parameters
    ----------
    a : array_like
        The data to calculate the historgam of.
    bins : int or sequence of scalars, optional
        If `bins` is an int, it defines the number of equal-width bins in
        the given range. If `bins` is a sequence, it defines the bin edges,
        including the rightmost edge, allowing for non-uniform bin widths.
        If bins is not given, the best number of bins is determined
        automatically using the Freedman-Diaconis rule.
    range : (float, float)
        The lower and upper range of the bins. If not provided, range is
        simply (a.min(), a.max()). Values outside the range are ignored.
    normed : bool
        If False, the result will contain the number of samples in each bin.
        If True, the result is the value of the probability *density*
        function at the bin, normalized such that the *integral* over the
        range is 1. Note that the sum of the histogram values will not be
        equal to 1 unless bins of unity width are chosen; it is not a
        probability *mass* function.
    weights : array_like
        An array of weights, of the same shape as `a`. Each value in `a`
        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.
    
    """

    # Auto determine bins?
    if bins is None:
        from visvis.processing.statistics import StatData
        stats = StatData(data)
        bins = stats.best_number_of_bins()

    # let numpy do the work
    values, edges = np.histogram(data, bins, drange, normed, weights)

    # the bins are the left bin edges, let's get the centers
    centers = np.empty(values.shape, np.float64)
    for i in range(len(values)):
        centers[i] = (edges[i] + edges[i + 1]) * 0.5

    # plot
    dbin = centers[1] - centers[0]
    return vv.bar(centers, values, width=dbin * 0.9)
Ejemplo n.º 5
0
    def __init__(self, data, width, whiskers):

        # Get stats of data
        self._stats = StatData(data)

        # Init width
        self._width = width

        # Init whisker style
        self._whiskers = whiskers

        # Init line style
        self._lc = (0, 0, 0)
        self._lw = 1

        # Calculate now
        self.calculate()
Ejemplo n.º 6
0
                box.SetWhiskers(value)

        return locals()


if __name__ == '__main__':

    vv.figure(1)
    vv.clf()
    a = vv.gca()
    d1 = np.random.normal(1, 4, (1000, 1000))
    d2 = np.random.normal(2, 3, (20, ))
    d3 = np.random.uniform(-1, 3, (100, ))
    d4 = [
        1, 2, 1, 2.0, 8, 2, 3, 1, 2, 2, 3, 2, 2.1, 8, 8, 8, 8, 8, 1.2, 1.3, 0,
        0, 1.5, 2
    ]

    b = boxplot((d1, d2, d3, d4), width=0.8, whiskers='violin')
    ##
    dd = d4
    stat = StatData(dd)
    bins1, values1 = stat.histogram_np(normed=True)
    bins2, values2 = stat.histogram()
    bins3, values3 = stat.kde()
    vv.figure(2)
    vv.clf()
    vv.bar(bins2, values2)  #, lc='r', ms='.', mc='r')
    vv.plot(bins3, values3)  #, lc='g', ms='.', mc='g')
    vv.plot(bins1, values1, lc='b', ms='.', mc='b', ls=':', mw=4)
    #print abs(bins1-bins2).sum()
Ejemplo n.º 7
0
        """ Get/Set the style of the whiskers. 
        """
        def fget(self):
            return self._boxes[0]._whiskers
        def fset(self, value):
            for box in self._boxes:
                box.SetWhiskers(value)
        return locals()


if __name__ == '__main__':
    
    vv.figure(1); vv.clf()
    a = vv.gca()
    d1 = np.random.normal(1, 4, (1000,1000))
    d2 = np.random.normal(2, 3, (20,))
    d3 = np.random.uniform(-1, 3, (100,))
    d4 = [1,2,1,2.0, 8, 2, 3, 1, 2, 2, 3, 2, 2.1, 8, 8, 8, 8, 8,  1.2, 1.3, 0, 0, 1.5, 2]
    
    b = boxplot((d1,d2,d3, d4), width=0.8, whiskers='violin')
    ##
    dd = d4
    stat = StatData(dd)
    bins1, values1 = stat.histogram_np(normed=True)
    bins2, values2 = stat.histogram()
    bins3, values3 = stat.kde(     )
    vv.figure(2); vv.clf()    
    vv.bar(bins2, values2)#, lc='r', ms='.', mc='r')
    vv.plot(bins3, values3)#, lc='g', ms='.', mc='g')
    vv.plot(bins1, values1, lc='b', ms='.', mc='b', ls=':', mw=4)
    #print abs(bins1-bins2).sum()