Esempio n. 1
0
def plotdict(datadict,
             labels=None,
             outfig='./somefig.png',
             title='A boxplot from a dictionary'):

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)

    ax.set_title(title)

    #sys.path.append('/usr/people/plas/python/tools')
    import boxplot_class_JK as mbox

    if labels == None: labels = sorted(datadict.keys())
    if len(labels) < len(datadict.keys()):
        print 'Warning: too few labels, using keys, \nlabels:', labels, '\nkeys: ', datadict.keys(
        )
        labels = sorted(datadict.keys())

    pb = mbox.MultiBoxPlot(ax)
    for f, lab in zip(sorted(datadict.keys()), labels):
        pb.addmultibox([datadict[f]], label=str(lab))
    pb.render()

    plotfile = outfig
    sf = fig.savefig(plotfile)
    print 'plotted ', plotfile
    plt.close(fig)
Esempio n. 2
0
def plotmultidict(datadict,
                  labels=None,
                  outfig='./somefig.png',
                  title='Grouped box plot'):
    '''
    makes a boxplot of different datasets, expects a dict 
    '''

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.set_title(title)

    #sys.path.append('/usr/people/plas/python/tools')
    import boxplot_class_JK as mbox

    if labels == None: labels = datadict.keys()
    if len(labels) < len(datadict.keys()):
        print 'Warning: too few labels, using keys, ' + '\nlabels:', labels, '\nkeys: ', datadict.keys(
        )
        labels = datadict.keys()

    pb = mbox.MultiBoxPlot(ax)

    cases = sorted(datadict.keys())  ## NB cases are sorted alphabetically!
    if str in [type(c) for c in cases]:
        print 'multidict: ', cases, datadict[cases[0]].keys()
        labels = sorted(datadict[cases[0]].keys())

        # take the first case as determining the labels etc(?)
        for f, lab in zip(sorted(datadict[cases[0]].keys()), labels):
            # [datadict[c1][f],datadict[c2][f],...]
            pb.addmultibox([datadict[c][f] for c in cases], label=str(lab))

        pb.addlegend(cases, loc=4)

    else:  # assume it is a 1 case dict
        print 'one case dict: ', datadict.keys()
        for f, lab in zip(sorted(datadict[c].keys()), labels):
            pb.addmultibox([datadict[f]], label=str(lab))

    pb.render()

    plotfile = outfig
    sf = fig.savefig(plotfile)
    print 'plotted ', plotfile
    plt.close(fig)
Esempio n. 3
0
def boxplot_leadtime(df, par='t2m', outdir='./', outfile=None):

    import matplotlib.pyplot as plt
    import boxplot_class_JK as mbox

    # create figure canvas, axes
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    pb = mbox.MultiBoxPlot(ax)

    data = [
        df[df.leadtime_x == leadtime][par]
        for leadtime in df.leadtime_x.unique()
    ]
    pb.addmultibox(data, label=par)

    if outfile == None:
        outfile = 'leadtime_{p}.png'.format(p=par)
    fig.savefig(os.path.join(outdir, outfile))
    return 0
Esempio n. 4
0
def boxplotNoDict(dbfiles,
                  cases,
                  thresholds,
                  nbpts,
                  param='fss',
                  tab='stats',
                  colors=None,
                  baserateThreshold=0.2,
                  outdir='./'):
    '''
    makes comparative boxplots of param (default 'fss') based on the
    HARP spatial databases "dbfiles" (eg 'spatial_BULL_3H_201310.db'),
    described by "cases" (eg 'BULL','D11' etc), where the results are
    generated with given thresholds and neighbourhoods (nbpts).

    The table where this is set is default set to 'stats'.

    A baserateThreshold (default set to 0.2, so 20% of the pixels has
    rain > threshold) is used to filter out so-called 'non-events' as
    this gives a lot of points with value 0 in the distribution.
    '''

    try:
        os.makedirs(outdir)
    except:
        print 'output to ', outdir

    if colors == None: colors = ['blue', 'red', 'green', 'purple', 'orange']

    sys.path.append('/usr/people/plas/python/tools')
    import boxplot_class_JK as mbox

    print cases
    print dbfiles
    #sys.exit(1)

    outfiles = []
    for thr in thresholds:

        fignb = plt.figure()
        axnb = fignb.add_subplot(1, 1, 1)
        pbnb = mbox.MultiBoxPlot(axnb, colors=colors)
        axnb.set_title(
            '{PAR} (> {THR} mm/3h) as a function of NBR SIZE'.format(PAR=param,
                                                                     THR=thr))

        for nbp in nbpts:

            ## nbp plot: eg FSS as a function of neighbourhood size
            datanbp = []
            for c, db in zip(cases, dbfiles):
                sql_fss_nbp = 'select {PAR} from {TAB} ' + 'where threshold = {THR} and nbpts = {NBP} and baserate > {BR}'
                sqlin = sql_fss_nbp.format(PAR=param,
                                           TAB=tab,
                                           THR=str(thr),
                                           NBP=str(nbp),
                                           BR=str(baserateThreshold))
                print sqlin
                inf, sdat = get_data(db,
                                     tab,
                                     sqlin=sql_fss_nbp.format(
                                         PAR=param,
                                         TAB=tab,
                                         THR=str(thr),
                                         NBP=str(nbp),
                                         BR=str(baserateThreshold)))
                datanbp.append(np.array(
                    [d[0]
                     for d in sdat]))  # append to make new list within a list

            pbnb.addmultibox(datanbp, label=str(nbp))

            ## leadtime plot: eg FSS as a function of leadtime
            figlt = plt.figure()
            axlt = figlt.add_subplot(1, 1, 1)
            pblt = mbox.MultiBoxPlot(axlt, colors=colors)
            axlt.set_title(
                '{PAR} (> {THR} mm/3h) as a function of leadtime, size {NBPTS}'
                .format(PAR=param, THR=thr, NBPTS=nbp))

            for lt in range(3, 49, 3):

                datalt = []
                for c, db in zip(cases, dbfiles):

                    sql_gen =  'select fss from stats '+\
                        'where threshold = {THR} and nbpts = {NBP} '+\
                        'and leadtime = {LT} and baserate > {BR}'

                    lts = 3600 * lt  # POSIX: from hours to seconds

                    # getting the actual data
                    sql_fss_lt = sql_gen.format(THR=str(thr),
                                                NBP=str(nbp),
                                                LT=str(lts),
                                                BR=str(baserateThreshold))
                    inf, sdat = get_data(db, tab, sqlin=sql_fss_lt)
                    datalt.append(np.array([d[0] for d in sdat]))
                    print 'Case:', c, db, sql_fss_lt

                print 'for leadtime {LT} data: {DAT}'.format(LT=str(lt),
                                                             DAT=datalt)
                pblt.addmultibox(datalt, label=str(lt))

            pblt.addlegend(cases, loc=3)
            pblt.render()

            outfig = os.path.join(
                outdir, '{PAR}_thr{THR}_nbp{NBP}_lt.png'.format(PAR=param,
                                                                THR=thr,
                                                                NBP=nbp))
            plotfile = outfig
            sf = figlt.savefig(plotfile)
            print '+++++++++ plotted ', plotfile
            plt.close(figlt)
            outfiles.append(plotfile)

        # end loop over NBPTS
        pbnb.addlegend(cases, loc=4)
        pbnb.render()

        outfig = os.path.join(
            outdir, '{PAR}_thr{THR}_nb.png'.format(PAR=param, THR=thr))
        plotfile = outfig
        sf = fignb.savefig(plotfile)
        print '+++++++++ plotted ', plotfile
        outfiles.append(plotfile)
        plt.close(fignb)

    # end loop over thresholds
    return outfiles
Esempio n. 5
0
    #print fssdat
    #sys.exit(0)

    import matplotlib.pyplot
    fig = matplotlib.pyplot.figure()
    ax = fig.add_subplot(2, 1, 1)

    bp = matplotlib.pyplot.boxplot(fssdat, notch=0, sym='+', vert=1, whis=1.5)

    ax = fig.add_subplot(2, 1, 2)

    #home = os.getenv('HOME')
    #sys.path.append(os.path.join(home,'python','tools')
    import boxplot_class_JK as mbox

    pb = mbox.MultiBoxPlot(ax)
    for f, b, d, lt in zip(fssdat, fssdat_M, fssdat_D, range(3, 49, 3)):
        print len(f), len(b)
        if len(f) == 0 or len(b) == 0 or len(d) == 0: break
        pb.addmultibox([f, b, d], label=str(lt))

    #pb.addlegend(['FSS','Baserate'])
    pb.addlegend(['FSS BULL', 'FSS Mode-S', 'FSS D11'])
    pb.render()

    #bp = matplotlib.pyplot.boxplot(fssdat+baserdat, notch=0, sym='+', vert=1, whis=1.5)

    plotfile = 'box_pyclass.png'
    sf = fig.savefig(plotfile)