Example #1
0
def arr_hist(arr, nbins='auto', av_per_bin=40):
    """ Plot histogram of contents of arr
    setting mbins overrides av_per_bin """

    ## If nbins set to auto choose it so that there are on average av_per_bin
    if nbins == 'auto':
        if type(arr) == tuple:
            nbins = int(len(arr[0]) / av_per_bin)
        else:
            nbins = int(len(arr) / av_per_bin)
        ## Make sure there are at least 10 bins
        if nbins < 10:
            nbins = 10

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ## the histogram of the data with histtype='step'
    n, bins, patches = plt.hist(arr,
                                nbins,
                                normed=0,
                                histtype='stepfilled',
                                color=['g', 'b', 'c'][0:len(arr)])

    # plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

    x = (0.04, 0.8)
    for i, subarr in enumerate(tf_simple.make_iter(arr)):
        tf_string.str_moment(subarr, ax=ax, xy=(x[i], 0.7))

    plt.show()
    return
Example #2
0
def poly(x, coefs, str_eqn=False):
    """ Polynomial function of order len(args)-1
    Return: arg1 + arg2 x + arg3 x^2 + arg4 x^3 + ..."""

    # str_eqn=kwargs.pop('str_eqn', False)

    # if not args:
    #     raise('poly requires at least one arguement')

    # db(args=args)
    sum = 0
    pow = 0
    eqn = []
    for pow, coef in enumerate(tf_simple.make_iter(coefs)[::-1]):
        sum += coef * x**pow
        if str_eqn:
            if coef == 0:  # Don't add 0x^2 to the string etc
                pass
            elif pow > 1:
                eqn.insert(0, '{:0.3g}x^{}'.format(coef, pow))
            elif pow == 1:
                eqn.insert(0, '{:0.3g}x'.format(coef))
            else:
                eqn.insert(0, '{:0.3g}'.format(coef))  # no x^0 for constant
    str_eqn = '' + ' + '.join(eqn)  # join x terms separated by +s

    if not str_eqn:
        return sum  # just numerical output
    else:
        return sum, str_eqn
Example #3
0
def poly(x, coefs, str_eqn = False):
    """ Polynomial function of order len(args)-1
    Return: arg1 + arg2 x + arg3 x^2 + arg4 x^3 + ..."""

    # str_eqn=kwargs.pop('str_eqn', False)

    # if not args:
    #     raise('poly requires at least one arguement')

    # db(args=args)
    sum = 0
    pow = 0
    eqn = []
    for pow, coef in enumerate(tf_simple.make_iter(coefs)[::-1]):
        sum += coef * x**pow
        if str_eqn:
            if coef == 0: # Don't add 0x^2 to the string etc
                pass
            elif pow > 1:
                eqn.insert(0,'{:0.3g}x^{}'.format(coef,pow))
            elif pow == 1:
                eqn.insert(0,'{:0.3g}x'.format(coef))
            else:
                eqn.insert(0,'{:0.3g}'.format(coef)) # no x^0 for constant
    str_eqn = '' + ' + '.join(eqn) # join x terms separated by +s

    if not str_eqn:
        return sum # just numerical output
    else:
        return sum, str_eqn
Example #4
0
def arr_hist(arr, nbins='auto', av_per_bin=40):
    """ Plot histogram of contents of arr
    setting mbins overrides av_per_bin """

    ## If nbins set to auto choose it so that there are on average av_per_bin
    if nbins == 'auto':
        if type(arr) == tuple:
            nbins = int(len(arr[0])/av_per_bin)
        else:
            nbins = int(len(arr)/av_per_bin)
        ## Make sure there are at least 10 bins
        if nbins < 10:
            nbins = 10

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ## the histogram of the data with histtype='step'
    n, bins, patches = plt.hist(arr, nbins, normed=0, histtype='stepfilled', color=['g','b','c'][0:len(arr)])

    # plt.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

    x = (0.04, 0.8)
    for i, subarr in enumerate(tf_simple.make_iter(arr)):
        tf_string.str_moment(subarr, ax = ax, xy=(x[i],0.7))

    plt.show()
    return
Example #5
0
    def __init__(self, x=None, y=None, title = None, subplot=111, defaults=1, text=None, block=True, dir_fig = './Figures/', fn='Figure_tmp', cm='jet'):
        ## Check arguments are as expected
        db(tx=type(x))
        if not x==None: assert type(x) == ParamFloat, 'x plotting variable must be a <ParamFloat> (tuple not accepted)'
        if not y==None: assert type(y) == ParamFloat or (type(y) == tuple and type(y[0])==ParamFloat), 'y plotting variable must be a <ParamFloat> or tuple of <ParamFloat>s'
        if text: assert (type(text)==tuple and len(text)>=3), 'text keyword must have format (x,y,str,{fontsize})'

        PlotLines.nfig += 1  # Increase counter for number of plot instances
        self.fig_no = PlotLines.nfig  # Set the figure number to the number of this instance
        self.shown = False

        ## Store arguments in self
        self.fig = None
        self.title = title
        self.x = x
        self.y = tf_simple.make_iter(y) # Always make y a tuple even if it only contains one set of data
        self.text = [] # text is a list of tuples containing (x,y,str,{fontsize})
        if text: self.text.append(text) # Text to annotate plot
        self.subplot = subplot
        self.block = block
        self.dir_fig = dir_fig
        self.fn = fn
        self.cm = cm

        ## Set default mpl properties (font sizes etc)
        tfp.set_mpl_defaults(defaults=defaults)
        plt.ion() # Interactive plotting (draw implicit)

        ## Create the figure and axes
        self.ax, self.fig = tfp.new_axis(subplot, fig_no=self.fig_no)

        ## Set axis title and labels
        if self.title:
            self.set_title(title)
        if not self.x == None:
            self.set_xlabel(self.x.label())
        if not self.y == None:
            self.set_ylabel(self.y[0].label()) # set y axis using first y parameter in tuple

        if text:
            self.set_text()
Example #6
0
    def __init__(self,
                 x=None,
                 y=None,
                 title=None,
                 subplot=111,
                 defaults=1,
                 text=None,
                 block=True,
                 dir_fig='./Figures/',
                 fn='Figure_tmp',
                 cm='jet'):
        ## Check arguments are as expected
        db(tx=type(x))
        if not x == None:
            assert type(
                x
            ) == ParamFloat, 'x plotting variable must be a <ParamFloat> (tuple not accepted)'
        if not y == None:
            assert type(y) == ParamFloat or (
                type(y) == tuple and type(y[0]) == ParamFloat
            ), 'y plotting variable must be a <ParamFloat> or tuple of <ParamFloat>s'
        if text:
            assert (type(text) == tuple and len(text) >= 3
                    ), 'text keyword must have format (x,y,str,{fontsize})'

        PlotLines.nfig += 1  # Increase counter for number of plot instances
        self.fig_no = PlotLines.nfig  # Set the figure number to the number of this instance
        self.shown = False

        ## Store arguments in self
        self.fig = None
        self.title = title
        self.x = x
        self.y = tf_simple.make_iter(
            y
        )  # Always make y a tuple even if it only contains one set of data
        self.text = [
        ]  # text is a list of tuples containing (x,y,str,{fontsize})
        if text: self.text.append(text)  # Text to annotate plot
        self.subplot = subplot
        self.block = block
        self.dir_fig = dir_fig
        self.fn = fn
        self.cm = cm

        ## Set default mpl properties (font sizes etc)
        tfp.set_mpl_defaults(defaults=defaults)
        plt.ion()  # Interactive plotting (draw implicit)

        ## Create the figure and axes
        self.ax, self.fig = tfp.new_axis(subplot, fig_no=self.fig_no)

        ## Set axis title and labels
        if self.title:
            self.set_title(title)
        if not self.x == None:
            self.set_xlabel(self.x.label())
        if not self.y == None:
            self.set_ylabel(self.y[0].label()
                            )  # set y axis using first y parameter in tuple

        if text:
            self.set_text()