packages/vaex-viz/vaex/viz/mpl.py
def plot1d(self, x=None, what="count(*)", grid=None, shape=64, facet=None, limits=None, figsize=None, f="identity", n=None, normalize_axis=None,
           xlabel=None, ylabel=None, label=None,
           selection=None, show=False, tight_layout=True, hardcopy=None,
           **kwargs):
    """Viz data in 1d (histograms, running means etc)

    Example

    >>> df.plot1d(df.x)
    >>> df.plot1d(df.x, limits=[0, 100], shape=100)
    >>> df.plot1d(df.x, what='mean(y)', limits=[0, 100], shape=100)

    If you want to do a computation yourself, pass the grid argument, but you are responsible for passing the
    same limits arguments:

    >>> counts = df.mean(df.y, binby=df.x, limits=[0, 100], shape=100)/100.
    >>> df.plot1d(df.x, limits=[0, 100], shape=100, grid=means, label='mean(y)/100')

    :param x: Expression to bin in the x direction
    :param what: What to plot, count(*) will show a N-d histogram, mean('x'), the mean of the x column, sum('x') the sum
    :param grid: If the binning is done before by yourself, you can pass it
    :param facet: Expression to produce facetted plots ( facet='x:0,1,12' will produce 12 plots with x in a range between 0 and 1)
    :param limits: list of [xmin, xmax], or a description such as 'minmax', '99%'
    :param figsize: (x, y) tuple passed to pylab.figure for setting the figure size
    :param f: transform values by: 'identity' does nothing 'log' or 'log10' will show the log of the value
    :param n: normalization function, currently only 'normalize' is supported, or None for no normalization
    :param normalize_axis: which axes to normalize on, None means normalize by the global maximum.
    :param normalize_axis:
    :param xlabel: String for label on x axis (may contain latex)
    :param ylabel: Same for y axis
    :param: tight_layout: call pylab.tight_layout or not
    :param kwargs: extra argument passed to pylab.plot
    :return:
    """

    import pylab
    f = _parse_f(f)
    n = _parse_n(n)
    if type(shape) == int:
        shape = (shape,)
    binby = []
    x = _ensure_strings_from_expressions(x)
    for expression in [x]:
        if expression is not None:
            binby = [expression] + binby
    limits = self.limits(binby, limits)
    if figsize is not None:
        pylab.figure(num=None, figsize=figsize, dpi=80, facecolor='w', edgecolor='k')
    fig = pylab.gcf()
    import re
    if facet is not None:
        match = re.match("(.*):(.*),(.*),(.*)", facet)
        if match:
            groups = match.groups()
            facet_expression = groups[0]
            facet_limits = [ast.literal_eval(groups[1]), ast.literal_eval(groups[2])]
            facet_count = ast.literal_eval(groups[3])
            limits.append(facet_limits)
            binby.append(facet_expression)
            shape = (facet_count,) + shape
        else:
            raise ValueError("Could not understand 'facet' argument %r, expected something in form: 'column:-1,10:5'" % facet)

    if grid is None:
        if what:
            if isinstance(what, (vaex.stat.Expression)):
                grid = what.calculate(self, binby=binby, limits=limits, shape=shape, selection=selection)
            else:
                what = what.strip()
                index = what.index("(")
                import re
                groups = re.match("(.*)\((.*)\)", what).groups()
                if groups and len(groups) == 2:
                    function = groups[0]
                    arguments = groups[1].strip()
                    functions = ["mean", "sum", "std", "count"]
                    if function in functions:
                        # grid = getattr(self, function)(arguments, binby, limits=limits, shape=shape, selection=selection)
                        grid = getattr(vaex.stat, function)(arguments).calculate(self, binby=binby, limits=limits, shape=shape, selection=selection)
                    elif function == "count" and arguments == "*":
                        grid = self.count(binby=binby, shape=shape, limits=limits, selection=selection)
                    elif function == "cumulative" and arguments == "*":
                        # TODO: comulative should also include the tails outside limits
                        grid = self.count(binby=binby, shape=shape, limits=limits, selection=selection)
                        grid = np.cumsum(grid)
                    else:
                        raise ValueError("Could not understand method: %s, expected one of %r'" % (function, functions))
                else:
                    raise ValueError("Could not understand 'what' argument %r, expected something in form: 'count(*)', 'mean(x)'" % what)
        else:
            grid = self.histogram(binby, size=shape, limits=limits, selection=selection)
    fgrid = f(grid)
    if n is not None:
        # ngrid = n(fgrid, axis=normalize_axis)
        ngrid = fgrid / fgrid.sum()
    else:
        ngrid = fgrid
        # reductions = [_parse_reduction(r, colormap, colors) for r in reduce]
        # rgrid = ngrid * 1.
        # for r in reduce:
        #   r = _parse_reduction(r, colormap, colors)
        #   rgrid = r(rgrid)
        # grid = self.reduce(grid, )
    xmin, xmax = limits[-1]
    if facet:
        N = len(grid[-1])
    else:
        N = len(grid)
    xexpression = binby[0]
    xar = np.arange(N + 1) / (N - 0.) * (xmax - xmin) + xmin
    label = str(label or selection or x)
    if facet:
        import math
        rows, columns = int(math.ceil(facet_count / 4.)), 4
        values = np.linspace(facet_limits[0], facet_limits[1], facet_count + 1)
        for i in range(facet_count):
            ax = pylab.subplot(rows, columns, i + 1)
            value = ax.plot(xar, ngrid[i], drawstyle="steps-mid", label=label, **kwargs)
            v1, v2 = values[i], values[i + 1]
            pylab.xlabel(xlabel or x)
            pylab.ylabel(ylabel or what)
            ax.set_title("%3f <= %s < %3f" % (v1, facet_expression, v2))
            if self.iscategory(xexpression):
                labels = self.category_labels(xexpression)
                step = len(labels) // max_labels
                pylab.xticks(range(len(labels))[::step], labels[::step], size='small')
    else:
        # im = pylab.imshow(rgrid, extent=np.array(limits[:2]).flatten(), origin="lower", aspect=aspect)
        pylab.xlabel(xlabel or self.label(x))
        pylab.ylabel(ylabel or what)
        # print(xar, ngrid)
        # repeat the first element, that's how plot/steps likes it..
        g = np.concatenate([ngrid[0:1], ngrid])
        value = pylab.plot(xar, g, drawstyle="steps-pre", label=label, **kwargs)
        if self.iscategory(xexpression):
            labels = self.category_labels(xexpression)
            step = len(labels) // max_labels
            pylab.xticks(range(len(labels))[::step], labels[::step], size='small')
    if tight_layout:
        pylab.tight_layout()
    if hardcopy:
        pylab.savefig(hardcopy)
    if show:
        pylab.show()
    return value
def plot1d(self, x=None, what="count(*)", grid=None, shape=64, facet=None, limits=None, figsize=None, f="identity", n=None, normalize_axis=None,
           xlabel=None, ylabel=None, label=None,
           selection=None, show=False, tight_layout=True, hardcopy=None,
           **kwargs):
    """Viz data in 1d (histograms, running means etc)

    Example

    >>> df.plot1d(df.x)
    >>> df.plot1d(df.x, limits=[0, 100], shape=100)
    >>> df.plot1d(df.x, what='mean(y)', limits=[0, 100], shape=100)

    If you want to do a computation yourself, pass the grid argument, but you are responsible for passing the
    same limits arguments:

    >>> counts = df.mean(df.y, binby=df.x, limits=[0, 100], shape=100)/100.
    >>> df.plot1d(df.x, limits=[0, 100], shape=100, grid=means, label='mean(y)/100')

    :param x: Expression to bin in the x direction
    :param what: What to plot, count(*) will show a N-d histogram, mean('x'), the mean of the x column, sum('x') the sum
    :param grid: If the binning is done before by yourself, you can pass it
    :param facet: Expression to produce facetted plots ( facet='x:0,1,12' will produce 12 plots with x in a range between 0 and 1)
    :param limits: list of [xmin, xmax], or a description such as 'minmax', '99%'
    :param figsize: (x, y) tuple passed to pylab.figure for setting the figure size
    :param f: transform values by: 'identity' does nothing 'log' or 'log10' will show the log of the value
    :param n: normalization function, currently only 'normalize' is supported, or None for no normalization
    :param normalize_axis: which axes to normalize on, None means normalize by the global maximum.
    :param normalize_axis:
    :param xlabel: String for label on x axis (may contain latex)
    :param ylabel: Same for y axis
    :param: tight_layout: call pylab.tight_layout or not
    :param kwargs: extra argument passed to pylab.plot
    :return:
    """

    import pylab
    f = _parse_f(f)
    n = _parse_n(n)
    if type(shape) == int:
        shape = (shape,)
    binby = []
    x = _ensure_strings_from_expressions(x)
    for expression in [x]:
        if expression is not None:
            binby = [expression] + binby
    limits = self.limits(binby, limits)
    if figsize is not None:
        pylab.figure(num=None, figsize=figsize, dpi=80, facecolor='w', edgecolor='k')
    fig = pylab.gcf()
    import re
    if facet is not None:
        match = re.match("(.*):(.*),(.*),(.*)", facet)
        if match:
            groups = match.groups()
            facet_expression = groups[0]
            facet_limits = [ast.literal_eval(groups[1]), ast.literal_eval(groups[2])]
            facet_count = ast.literal_eval(groups[3])
            limits.append(facet_limits)
            binby.append(facet_expression)
            shape = (facet_count,) + shape
        else:
            raise ValueError("Could not understand 'facet' argument %r, expected something in form: 'column:-1,10:5'" % facet)

    if grid is None:
        if what:
            if isinstance(what, (vaex.stat.Expression)):
                grid = what.calculate(self, binby=binby, limits=limits, shape=shape, selection=selection)
            else:
                what = what.strip()
                index = what.index("(")
                import re
                groups = re.match("(.*)\((.*)\)", what).groups()
                if groups and len(groups) == 2:
                    function = groups[0]
                    arguments = groups[1].strip()
                    functions = ["mean", "sum", "std", "count"]
                    if function in functions:
                        # grid = getattr(self, function)(arguments, binby, limits=limits, shape=shape, selection=selection)
                        grid = getattr(vaex.stat, function)(arguments).calculate(self, binby=binby, limits=limits, shape=shape, selection=selection)
                    elif function == "count" and arguments == "*":
                        grid = self.count(binby=binby, shape=shape, limits=limits, selection=selection)
                    elif function == "cumulative" and arguments == "*":
                        # TODO: comulative should also include the tails outside limits
                        grid = self.count(binby=binby, shape=shape, limits=limits, selection=selection)
                        grid = np.cumsum(grid)
                    else:
                        raise ValueError("Could not understand method: %s, expected one of %r'" % (function, functions))
                else:
                    raise ValueError("Could not understand 'what' argument %r, expected something in form: 'count(*)', 'mean(x)'" % what)
        else:
            grid = self.histogram(binby, size=shape, limits=limits, selection=selection)
    fgrid = f(grid)
    if n is not None:
        # ngrid = n(fgrid, axis=normalize_axis)
        ngrid = fgrid / fgrid.sum()
    else:
        ngrid = fgrid
        # reductions = [_parse_reduction(r, colormap, colors) for r in reduce]
        # rgrid = ngrid * 1.
        # for r in reduce:
        #   r = _parse_reduction(r, colormap, colors)
        #   rgrid = r(rgrid)
        # grid = self.reduce(grid, )
    xmin, xmax = limits[-1]
    if facet:
        N = len(grid[-1])
    else:
        N = len(grid)
    xexpression = binby[0]
    xar = np.arange(N + 1) / (N - 0.) * (xmax - xmin) + xmin
    label = str(label or selection or x)
    if facet:
        import math
        rows, columns = int(math.ceil(facet_count / 4.)), 4
        values = np.linspace(facet_limits[0], facet_limits[1], facet_count + 1)
        for i in range(facet_count):
            ax = pylab.subplot(rows, columns, i + 1)
            value = ax.plot(xar, ngrid[i], drawstyle="steps-mid", label=label, **kwargs)
            v1, v2 = values[i], values[i + 1]
            pylab.xlabel(xlabel or x)
            pylab.ylabel(ylabel or what)
            ax.set_title("%3f <= %s < %3f" % (v1, facet_expression, v2))
            if self.iscategory(xexpression):
                labels = self.category_labels(xexpression)
                step = len(labels) // max_labels
                pylab.xticks(range(len(labels))[::step], labels[::step], size='small')
    else:
        # im = pylab.imshow(rgrid, extent=np.array(limits[:2]).flatten(), origin="lower", aspect=aspect)
        pylab.xlabel(xlabel or self.label(x))
        pylab.ylabel(ylabel or what)
        # print(xar, ngrid)
        # repeat the first element, that's how plot/steps likes it..
        g = np.concatenate([ngrid[0:1], ngrid])
        value = pylab.plot(xar, g, drawstyle="steps-pre", label=label, **kwargs)
        if self.iscategory(xexpression):
            labels = self.category_labels(xexpression)
            step = len(labels) // max_labels
            pylab.xticks(range(len(labels))[::step], labels[::step], size='small')
    if tight_layout:
        pylab.tight_layout()
    if hardcopy:
        pylab.savefig(hardcopy)
    if show:
        pylab.show()
    return value
def plot1d ( self , x = None , what = "count(*)" , grid = None , shape = 64 , facet = None , limits = None , figsize = None , f = "identity" , n = None , normalize_axis = None , xlabel = None , ylabel = None , label = None , selection = None , show = False , tight_layout = True , hardcopy = None , * * kwargs ) : import pylab f = _parse_f ( f ) n = _parse_n ( n ) if type ( shape ) == int : shape = ( shape , ) binby = [ ] x = _ensure_strings_from_expressions ( x ) for expression in [ x ] : if expression is not None : binby = [ expression ] + binby limits = self . limits ( binby , limits ) if figsize is not None : pylab . figure ( num = None , figsize = figsize , dpi = 80 , facecolor = 'w' , edgecolor = 'k' ) fig = pylab . gcf ( ) import re if facet is not None : match = re . match ( "(.*):(.*),(.*),(.*)" , facet ) if match : groups = match . groups ( ) facet_expression = groups [ 0 ] facet_limits = [ ast . literal_eval ( groups [ 1 ] ) , ast . literal_eval ( groups [ 2 ] ) ] facet_count = ast . literal_eval ( groups [ 3 ] ) limits . append ( facet_limits ) binby . append ( facet_expression ) shape = ( facet_count , ) + shape else : raise ValueError ( "Could not understand 'facet' argument %r, expected something in form: 'column:-1,10:5'" % facet ) if grid is None : if what : if isinstance ( what , ( vaex . stat . Expression ) ) : grid = what . calculate ( self , binby = binby , limits = limits , shape = shape , selection = selection ) else : what = what . strip ( ) index = what . index ( "(" ) import re groups = re . match ( "(.*)\((.*)\)" , what ) . groups ( ) if groups and len ( groups ) == 2 : function = groups [ 0 ] arguments = groups [ 1 ] . strip ( ) functions = [ "mean" , "sum" , "std" , "count" ] if function in functions : # grid = getattr(self, function)(arguments, binby, limits=limits, shape=shape, selection=selection) grid = getattr ( vaex . stat , function ) ( arguments ) . calculate ( self , binby = binby , limits = limits , shape = shape , selection = selection ) elif function == "count" and arguments == "*" : grid = self . count ( binby = binby , shape = shape , limits = limits , selection = selection ) elif function == "cumulative" and arguments == "*" : # TODO: comulative should also include the tails outside limits grid = self . count ( binby = binby , shape = shape , limits = limits , selection = selection ) grid = np . cumsum ( grid ) else : raise ValueError ( "Could not understand method: %s, expected one of %r'" % ( function , functions ) ) else : raise ValueError ( "Could not understand 'what' argument %r, expected something in form: 'count(*)', 'mean(x)'" % what ) else : grid = self . histogram ( binby , size = shape , limits = limits , selection = selection ) fgrid = f ( grid ) if n is not None : # ngrid = n(fgrid, axis=normalize_axis) ngrid = fgrid / fgrid . sum ( ) else : ngrid = fgrid # reductions = [_parse_reduction(r, colormap, colors) for r in reduce] # rgrid = ngrid * 1. # for r in reduce: #   r = _parse_reduction(r, colormap, colors) #   rgrid = r(rgrid) # grid = self.reduce(grid, ) xmin , xmax = limits [ - 1 ] if facet : N = len ( grid [ - 1 ] ) else : N = len ( grid ) xexpression = binby [ 0 ] xar = np . arange ( N + 1 ) / ( N - 0. ) * ( xmax - xmin ) + xmin label = str ( label or selection or x ) if facet : import math rows , columns = int ( math . ceil ( facet_count / 4. ) ) , 4 values = np . linspace ( facet_limits [ 0 ] , facet_limits [ 1 ] , facet_count + 1 ) for i in range ( facet_count ) : ax = pylab . subplot ( rows , columns , i + 1 ) value = ax . plot ( xar , ngrid [ i ] , drawstyle = "steps-mid" , label = label , * * kwargs ) v1 , v2 = values [ i ] , values [ i + 1 ] pylab . xlabel ( xlabel or x ) pylab . ylabel ( ylabel or what ) ax . set_title ( "%3f <= %s < %3f" % ( v1 , facet_expression , v2 ) ) if self . iscategory ( xexpression ) : labels = self . category_labels ( xexpression ) step = len ( labels ) // max_labels pylab . xticks ( range ( len ( labels ) ) [ : : step ] , labels [ : : step ] , size = 'small' ) else : # im = pylab.imshow(rgrid, extent=np.array(limits[:2]).flatten(), origin="lower", aspect=aspect) pylab . xlabel ( xlabel or self . label ( x ) ) pylab . ylabel ( ylabel or what ) # print(xar, ngrid) # repeat the first element, that's how plot/steps likes it.. g = np . concatenate ( [ ngrid [ 0 : 1 ] , ngrid ] ) value = pylab . plot ( xar , g , drawstyle = "steps-pre" , label = label , * * kwargs ) if self . iscategory ( xexpression ) : labels = self . category_labels ( xexpression ) step = len ( labels ) // max_labels pylab . xticks ( range ( len ( labels ) ) [ : : step ] , labels [ : : step ] , size = 'small' ) if tight_layout : pylab . tight_layout ( ) if hardcopy : pylab . savefig ( hardcopy ) if show : pylab . show ( ) return value
Viz data in 1d (histograms, running means etc)

    Example

    >>> df.plot1d(df.x)
    >>> df.plot1d(df.x, limits=[0, 100], shape=100)
    >>> df.plot1d(df.x, what='mean(y)', limits=[0, 100], shape=100)

    If you want to do a computation yourself, pass the grid argument, but you are responsible for passing the
    same limits arguments:

    >>> counts = df.mean(df.y, binby=df.x, limits=[0, 100], shape=100)/100.
    >>> df.plot1d(df.x, limits=[0, 100], shape=100, grid=means, label='mean(y)/100')

    :param x: Expression to bin in the x direction
    :param what: What to plot, count(*) will show a N-d histogram, mean('x'), the mean of the x column, sum('x') the sum
    :param grid: If the binning is done before by yourself, you can pass it
    :param facet: Expression to produce facetted plots ( facet='x:0,1,12' will produce 12 plots with x in a range between 0 and 1)
    :param limits: list of [xmin, xmax], or a description such as 'minmax', '99%'
    :param figsize: (x, y) tuple passed to pylab.figure for setting the figure size
    :param f: transform values by: 'identity' does nothing 'log' or 'log10' will show the log of the value
    :param n: normalization function, currently only 'normalize' is supported, or None for no normalization
    :param normalize_axis: which axes to normalize on, None means normalize by the global maximum.
    :param normalize_axis:
    :param xlabel: String for label on x axis (may contain latex)
    :param ylabel: Same for y axis
    :param: tight_layout: call pylab.tight_layout or not
    :param kwargs: extra argument passed to pylab.plot
    :return:
Viz data in 1d ( histograms running means etc )