esmvaltool/diag_scripts/ocean/diagnostic_tools.py
def add_legend_outside_right(
        plot_details,
        ax1,
        column_width=0.1,
        loc='right'
):
    """
    Add a legend outside the plot, to the right.

    plot_details is a 2 level dict,
    where the first level is some key (which is hidden)
    and the 2nd level contains the keys:
        'c': color
        'lw': line width (optional)
        'ls': line style (optional)
        'label': label for the legend.
    ax1 is the axis where the plot was drawn.
    """
    # ####
    # Create dummy axes:
    legend_size = len(plot_details.keys()) + 1
    box = ax1.get_position()
    if loc.lower() == 'right':
        nrows = 25
        ncols = int(legend_size / nrows) + 1
        ax1.set_position([box.x0,
                          box.y0,
                          box.width * (1. - column_width * ncols),
                          box.height])

    if loc.lower() == 'below':
        ncols = 4
        nrows = int(legend_size / ncols) + 1
        ax1.set_position([box.x0,
                          box.y0 + (nrows * column_width),
                          box.width,
                          box.height - (nrows * column_width)])

    # Add emply plots to dummy axis.
    for index in sorted(plot_details.keys()):
        colour = plot_details[index]['c']

        linewidth = plot_details[index].get('lw', 1)

        linestyle = plot_details[index].get('ls', '-')

        label = plot_details[index].get('label', str(index))

        plt.plot(
            [], [],
            c=colour,
            lw=linewidth,
            ls=linestyle,
            label=label)

    if loc.lower() == 'right':
        legd = ax1.legend(
            loc='center left',
            ncol=ncols,
            prop={'size': 10},
            bbox_to_anchor=(1., 0.5))
    if loc.lower() == 'below':
        legd = ax1.legend(
            loc='upper center',
            ncol=ncols,
            prop={'size': 10},
            bbox_to_anchor=(0.5, -2. * column_width))
    legd.draw_frame(False)
    legd.get_frame().set_alpha(0.)

    Add a legend outside the plot, to the right.

    plot_details is a 2 level dict,
    where the first level is some key (which is hidden)
    and the 2nd level contains the keys:
        'c': color
        'lw': line width (optional)
        'ls': line style (optional)
        'label': label for the legend.
    ax1 is the axis where the plot was drawn.
    
Add a legend outside the plot, to the right.
Add a legend outside the plot to the right .
def add_legend_outside_right(
        plot_details,
        ax1,
        column_width=0.1,
        loc='right'
):
    legend_size = len(plot_details.keys()) + 1
    box = ax1.get_position()
    if loc.lower() == 'right':
        nrows = 25
        ncols = int(legend_size / nrows) + 1
        ax1.set_position([box.x0,
                          box.y0,
                          box.width * (1. - column_width * ncols),
                          box.height])
    if loc.lower() == 'below':
        ncols = 4
        nrows = int(legend_size / ncols) + 1
        ax1.set_position([box.x0,
                          box.y0 + (nrows * column_width),
                          box.width,
                          box.height - (nrows * column_width)])
    for index in sorted(plot_details.keys()):
        colour = plot_details[index]['c']
        linewidth = plot_details[index].get('lw', 1)
        linestyle = plot_details[index].get('ls', '-')
        label = plot_details[index].get('label', str(index))
        plt.plot(
            [], [],
            c=colour,
            lw=linewidth,
            ls=linestyle,
            label=label)
    if loc.lower() == 'right':
        legd = ax1.legend(
            loc='center left',
            ncol=ncols,
            prop={'size': 10},
            bbox_to_anchor=(1., 0.5))
    if loc.lower() == 'below':
        legd = ax1.legend(
            loc='upper center',
            ncol=ncols,
            prop={'size': 10},
            bbox_to_anchor=(0.5, -2. * column_width))
    legd.draw_frame(False)
    legd.get_frame().set_alpha(0.)
def add_legend_outside_right ( plot_details , ax1 , column_width = 0.1 , loc = 'right' ) : legend_size = len ( plot_details . keys ( ) ) + 1 box = ax1 . get_position ( ) if loc . lower ( ) == 'right' : nrows = 25 ncols = int ( legend_size / nrows ) + 1 ax1 . set_position ( [ box . x0 , box . y0 , box . width * ( 1. - column_width * ncols ) , box . height ] ) if loc . lower ( ) == 'below' : ncols = 4 nrows = int ( legend_size / ncols ) + 1 ax1 . set_position ( [ box . x0 , box . y0 + ( nrows * column_width ) , box . width , box . height - ( nrows * column_width ) ] ) for index in sorted ( plot_details . keys ( ) ) : colour = plot_details [ index ] [ 'c' ] linewidth = plot_details [ index ] . get ( 'lw' , 1 ) linestyle = plot_details [ index ] . get ( 'ls' , '-' ) label = plot_details [ index ] . get ( 'label' , str ( index ) ) plt . plot ( [ ] , [ ] , c = colour , lw = linewidth , ls = linestyle , label = label ) if loc . lower ( ) == 'right' : legd = ax1 . legend ( loc = 'center left' , ncol = ncols , prop = { 'size' : 10 } , bbox_to_anchor = ( 1. , 0.5 ) ) if loc . lower ( ) == 'below' : legd = ax1 . legend ( loc = 'upper center' , ncol = ncols , prop = { 'size' : 10 } , bbox_to_anchor = ( 0.5 , - 2. * column_width ) ) legd . draw_frame ( False ) legd . get_frame ( ) . set_alpha ( 0. )
Add a legend outside the plot, to the right.
Add a legend outside the plot to the right .
"""
    Add a legend outside the plot, to the right.

    plot_details is a 2 level dict,
    where the first level is some key (which is hidden)
    and the 2nd level contains the keys:
        'c': color
        'lw': line width (optional)
        'ls': line style (optional)
        'label': label for the legend.
    ax1 is the axis where the plot was drawn.
    """ # #### # Create dummy axes: # Add emply plots to dummy axis.
plot_details
ax1
column_width
loc