def setup_axes(
        ax=None,
        panels='blue',
        colorbars=[False],
        right_margin=None,
        left_margin=None,
        projection='2d',
        show_ticks=False):
    """Grid of axes for plotting, legends and colorbars.
    """
    if '3d' in projection: from mpl_toolkits.mplot3d import Axes3D
    avail_projections = {'2d', '3d'}
    if projection not in avail_projections:
        raise ValueError('choose projection from', avail_projections)
    if left_margin is not None:
        raise ValueError('Currently not supporting to pass `left_margin`.')
    if np.any(colorbars) and right_margin is None:
        right_margin = 1 - rcParams['figure.subplot.right'] + 0.21  # 0.25
    elif right_margin is None:
        right_margin = 1 - rcParams['figure.subplot.right'] + 0.06  # 0.10
    # make a list of right margins for each panel
    if not isinstance(right_margin, list):
        right_margin_list = [right_margin for i in range(len(panels))]
    else:
        right_margin_list = right_margin

    # make a figure with len(panels) panels in a row side by side
    top_offset = 1 - rcParams['figure.subplot.top']
    bottom_offset = 0.15 if show_ticks else 0.08
    left_offset = 1 if show_ticks else 0.3  # in units of base_height
    base_height = rcParams['figure.figsize'][1]
    height = base_height
    base_width = rcParams['figure.figsize'][0]
    if show_ticks: base_width *= 1.1

    draw_region_width = base_width - left_offset - top_offset - 0.5  # this is kept constant throughout

    right_margin_factor = sum([1 + right_margin for right_margin in right_margin_list])
    width_without_offsets = right_margin_factor * draw_region_width  # this is the total width that keeps draw_region_width

    right_offset = (len(panels) - 1) * left_offset
    figure_width = width_without_offsets + left_offset + right_offset
    draw_region_width_frac = draw_region_width / figure_width
    left_offset_frac = left_offset / figure_width
    right_offset_frac = 1 - (len(panels) - 1) * left_offset_frac

    if ax is None:
        pl.figure(figsize=(figure_width, height),
                  subplotpars=sppars(left=0, right=1, bottom=bottom_offset))
    left_positions = [left_offset_frac, left_offset_frac + draw_region_width_frac]
    for i in range(1, len(panels)):
        right_margin = right_margin_list[i-1]
        left_positions.append(left_positions[-1] + right_margin * draw_region_width_frac)
        left_positions.append(left_positions[-1] + draw_region_width_frac)
    panel_pos = [[bottom_offset], [1-top_offset], left_positions]

    axs = []
    if ax is None:
        for icolor, color in enumerate(panels):
            left = panel_pos[2][2*icolor]
            bottom = panel_pos[0][0]
            width = draw_region_width / figure_width
            height = panel_pos[1][0] - bottom
            if projection == '2d': ax = pl.axes([left, bottom, width, height])
            elif projection == '3d': ax = pl.axes([left, bottom, width, height], projection='3d')
            axs.append(ax)
    else:
        axs = ax if isinstance(ax, list) else [ax]

    return axs, panel_pos, draw_region_width, figure_width
def setup_axes ( ax = None , panels = 'blue' , colorbars = [ False ] , right_margin = None , left_margin = None , projection = '2d' , show_ticks = False ) : if '3d' in projection : from mpl_toolkits . mplot3d import Axes3D avail_projections = { '2d' , '3d' } if projection not in avail_projections : raise ValueError ( 'choose projection from' , avail_projections ) if left_margin is not None : raise ValueError ( 'Currently not supporting to pass `left_margin`.' ) if np . any ( colorbars ) and right_margin is None : right_margin = 1 - rcParams [ 'figure.subplot.right' ] + 0.21 elif right_margin is None : right_margin = 1 - rcParams [ 'figure.subplot.right' ] + 0.06 if not isinstance ( right_margin , list ) : right_margin_list = [ right_margin for i in range ( len ( panels ) ) ] else : right_margin_list = right_margin top_offset = 1 - rcParams [ 'figure.subplot.top' ] bottom_offset = 0.15 if show_ticks else 0.08 left_offset = 1 if show_ticks else 0.3 base_height = rcParams [ 'figure.figsize' ] [ 1 ] height = base_height base_width = rcParams [ 'figure.figsize' ] [ 0 ] if show_ticks : base_width *= 1.1 draw_region_width = base_width - left_offset - top_offset - 0.5 right_margin_factor = sum ( [ 1 + right_margin for right_margin in right_margin_list ] ) width_without_offsets = right_margin_factor * draw_region_width right_offset = ( len ( panels ) - 1 ) * left_offset figure_width = width_without_offsets + left_offset + right_offset draw_region_width_frac = draw_region_width / figure_width left_offset_frac = left_offset / figure_width right_offset_frac = 1 - ( len ( panels ) - 1 ) * left_offset_frac if ax is None : pl . figure ( figsize = ( figure_width , height ) , subplotpars = sppars ( left = 0 , right = 1 , bottom = bottom_offset ) ) left_positions = [ left_offset_frac , left_offset_frac + draw_region_width_frac ] for i in range ( 1 , len ( panels ) ) : right_margin = right_margin_list [ i - 1 ] left_positions . append ( left_positions [ - 1 ] + right_margin * draw_region_width_frac ) left_positions . append ( left_positions [ - 1 ] + draw_region_width_frac ) panel_pos = [ [ bottom_offset ] , [ 1 - top_offset ] , left_positions ] axs = [ ] if ax is None : for icolor , color in enumerate ( panels ) : left = panel_pos [ 2 ] [ 2 * icolor ] bottom = panel_pos [ 0 ] [ 0 ] width = draw_region_width / figure_width height = panel_pos [ 1 ] [ 0 ] - bottom if projection == '2d' : ax = pl . axes ( [ left , bottom , width , height ] ) elif projection == '3d' : ax = pl . axes ( [ left , bottom , width , height ] , projection = '3d' ) axs . append ( ax ) else : axs = ax if isinstance ( ax , list ) else [ ax ] return axs , panel_pos , draw_region_width , figure_width
Grid of axes for plotting, legends and colorbars.