Changeset 2746 in lmdz_wrf for trunk


Ignore:
Timestamp:
Nov 7, 2019, 6:09:59 PM (6 years ago)
Author:
lfita
Message:

Adding:

  • `add_legend_topofix': Function to add the legend on a topofix plot
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/drawing_tools.py

    r2745 r2746  
    163163
    164164####### Functions
     165# add_legend_topofix: Function to add the legend on a topofix plot
    165166# check_colorBar: Check if the given colorbar exists in matplotlib
    166167# colorbar_vals: Function to provide the colorbar values for a figure
     
    1484314844
    1484414845    return kfigS
     14846
     14847def add_legend_topofix(fig, legcolv=[0.8,'vertical',10.,10.,0.,0.8,0.85,'#6464AA']):
     14848    """ Function to add the legend on a topofix plot
     14849      fig= figure to which add the legend
     14850      legcolv = [ijstart, orientation, labsize, sqsize, labrotation, totboxlength,
     14851        ibox, seacolor] values for the legend of colors of the topography ([0.8, 'vertical',
     14852        10., 10., 0., 0.8, 0.85, '#CCCCFF'], default)
     14853          ijstart: initial position as fraction of figure on the oposite axis of
     14854            orientation
     14855          orientation: orientation of the legend
     14856          labsize: font size of the labels
     14857          sqsize: size of the squares of the legend
     14858          labrotation: rotation of the labels of the legend
     14859          totboxlength: total length of the color legend as percentage of the figure
     14860             size along the given orientation
     14861          ibox: starting at the given orientation as percentage of the figure
     14862          seacolor: color for the sea
     14863    """
     14864    fname = 'add_legend_topofix'
     14865
     14866    topobnds = fixtopobnds
     14867
     14868    Ncolors = len(RGBfixtopocolors)
     14869
     14870    # FROM: https://matplotlib.org/examples/color/named_colors.html
     14871    # Get height and width of the figure
     14872    ncols = 1
     14873    nrows = len(topobnds)
     14874    X, Y = fig.get_dpi() * fig.get_size_inches()
     14875
     14876    # Dimensions of the color-legend
     14877    totboxlength = legcolv[5]
     14878    ibox = legcolv[6]
     14879
     14880    cbox = unichr(9632)#+unichr(9632)+unichr(9632)
     14881    bbox = unichr(9633)
     14882
     14883    ccs = np.arange(0.,totboxlength,totboxlength/(nrows+1))
     14884
     14885    iix = legcolv[0]
     14886    Ns = []
     14887    Cs = []
     14888    Xs = []
     14889    Ys = []
     14890    if legcolv[1] == 'vertical':
     14891        bw = totboxlength / (nrows + 1)
     14892        bh = bw * X / Y
     14893        Ns.append('> ' + str(topobnds[Ncolors-2]))
     14894        Cs.append(Ncolors-2)
     14895        Xs.append([iix,iix-bh])
     14896        Ys.append(ibox - ccs[0])
     14897        for i in range(1,Ncolors-2):
     14898            Ns.append(str(topobnds[Ncolors-2-i]))
     14899            Cs.append(Ncolors-2-i)
     14900            Xs.append([iix,iix-bh])
     14901            Ys.append(ibox - ccs[i])
     14902        Ns.append('< 0')
     14903        Cs.append(0)
     14904        Xs.append([iix,iix-bh])
     14905        Ys.append(ibox - ccs[Ncolors-2])
     14906
     14907    elif legcolv[1] == 'horizontal':
     14908        bw = totboxlength / (nrows + 1)
     14909        bh = bw * Y / X
     14910
     14911        Ns.append('< 0')
     14912        Cs.append(0)
     14913        Xs.append(ibox+ccs[0])
     14914        Ys.append([iix-bh,iix])
     14915        for i in range(1,Ncolors-2):
     14916            Ns.append(str(topobnds[i]))
     14917            Cs.append(i)
     14918            Xs.append(ibox + ccs[i])
     14919            Ys.append([iix-bh,iix])
     14920        Ns.append('> ' + str(topobnds[Ncolors-2]))
     14921        Cs.append(Ncolors-2)
     14922        Xs.append(ibox + ccs[Ncolors-2])
     14923        Ys.append([iix-bh,iix])
     14924    else:
     14925        print errormsg
     14926        print '  ' + fname + ": oritnation of legend of colors of topography " +     \
     14927          "scale '" + legcolv[1] + "' not ready !!"
     14928        print '    available ones:', availor
     14929        quit(-1)
     14930
     14931    for ib in range(0,Ncolors-1):
     14932        Nb = Ns[ib]
     14933        if legcolv[1] == 'vertical':
     14934            ix = Xs[ib][0]
     14935            iy = Ys[ib]
     14936        elif legcolv[1] == 'horizontal':
     14937            ix = Xs[ib]
     14938            iy = Ys[ib][0]
     14939        plt.annotate(Nb, xy=(ix,iy), xycoords='figure fraction', fontsize=legcolv[2],\
     14940          rotation=legcolv[4])
     14941        if legcolv[1] == 'vertical':
     14942            ix = Xs[ib][1]
     14943        elif legcolv[1] == 'horizontal':
     14944            iy = Ys[ib][1]
     14945        plt.annotate(cbox, xy=(ix,iy), xycoords='figure fraction',                   \
     14946          fontsize=legcolv[3], color=HEXfixtopocolors[Cs[ib]])
     14947        plt.annotate(bbox, xy=(ix,iy), xycoords='figure fraction',                   \
     14948          fontsize=legcolv[3], color='k')
     14949
     14950    return
Note: See TracChangeset for help on using the changeset viewer.