Ejemplo n.º 1
0
def plot_map(pdata):
    '''
    Draw global map where dateline is on the center
    '''
    abc = 'abcdefgh'

    ###--- Create a figure
    fig = plt.figure()
    fig.set_size_inches(7, 8.5)  ## (xsize,ysize)

    ###--- Suptitle
    suptit = pdata['suptit']
    fig.suptitle(suptit,
                 fontsize=16,
                 y=0.97,
                 va='bottom',
                 stretch='semi-condensed')

    ###--- Map Projection
    center = 180  # Want to draw a map where dateline is on the center
    proj = ccrs.PlateCarree(central_longitude=center)
    data_crs = ccrs.PlateCarree()

    map_extent = [0., 359.9, -60, 60]  # Range to be shown
    img_range = pdata['img_bound']

    ###--- Set range of values to be shown
    #val_min, val_max= 0,0.5   <-- it is unnecessary for BoundaryNorm
    p_val_levels = [0, 0.01, 0.02, 0.05, 0.1, 0.2]

    ###--- Color map
    colors = ['darkred', '#FA325A', 'darkorange', 'forestgreen', '#C9CD71']
    cm = cls.LinearSegmentedColormap.from_list("cm5", colors)
    cm.set_under('1.')
    cm.set_over('1.')
    cm.set_bad('0.8')  # For the gridcell of NaN
    norm = cls.BoundaryNorm(p_val_levels, ncolors=cm.N, clip=False)

    left, right, top, bottom = 0.07, 0.97, 0.93, 0.12
    npnx, gapx, npny, gapy = 1, 0.05, len(pdata['data']), 0.06
    lx = (right - left - gapx * (npnx - 1)) / npnx
    ly = (top - bottom - gapy * (npny - 1)) / npny
    ix, iy = left, top

    props_imshow = dict(norm=norm,
                        origin='lower',
                        cmap=cm,
                        extent=img_range,
                        transform=data_crs)

    for i, (data, vnm) in enumerate(zip(pdata['data'], pdata['var_names'])):
        ax1 = fig.add_axes([ix, iy - ly, lx, ly], projection=proj)
        ax1.set_extent(map_extent, crs=data_crs)
        map1 = ax1.imshow(data, **props_imshow)

        subtit = '({}) {}'.format(abc[i], vnm)
        vf.map_common(ax1, subtit, data_crs, xloc=60, yloc=20)

        iy = iy - ly - gapy
    cb = vf.draw_colorbar(fig,
                          ax1,
                          map1,
                          type='horizontal',
                          size='panel',
                          gap=0.06,
                          extend='max')
    cb.set_label('Significance level', fontsize=11)
    cb.ax.set_xticklabels(
        ['{:.0f}%'.format((1 - val) * 100) for val in p_val_levels])
    cb.ax.tick_params(labelsize=10)

    ##-- Seeing or Saving Pic --##
    outfnm = pdata['out_fnm']
    print(outfnm)
    #fig.savefig(outfnm,dpi=100)   # dpi: pixels per inch
    fig.savefig(outfnm, dpi=150, bbox_inches='tight')  # dpi: pixels per inch
    # Defalut: facecolor='w', edgecolor='w', transparent=False
    plt.show()
    return
def plot_map(pdata):
    '''
    Draw PC time series on the top, and
       draw global map where dateline is on the center
    '''

    ###--- Create a figure
    fig = plt.figure()
    fig.set_size_inches(6, 10)  ## (xsize,ysize)

    ###--- Suptitle
    fig.suptitle(pdata['suptit'],
                 fontsize=16,
                 y=0.97,
                 va='bottom',
                 stretch='semi-condensed')

    ###--- Axes setting
    nk = len(pdata['tgt_nums'])  # Number of data to show
    left, right, top, bottom = 0.07, 0.93, 0.925, 0.1
    npnx, gapx, npny, gapy = 1, 0.05, nk + 1, 0.064
    lx = (right - left - gapx * (npnx - 1)) / npnx
    ly = (top - bottom - gapy * (npny - 1)) / npny
    ix, iy = left, top

    ###--- Top panel: PC time series
    ax1 = fig.add_axes([ix, iy - ly, lx, ly])
    colors = plt.cm.tab10(np.linspace(0.05, 0.95, 10))
    for i, k in enumerate(pdata['tgt_nums']):
        ax1.plot_date(pdata['mon_list'],
                      pdata['pc'][:, k],
                      fmt='-',
                      c=colors[i],
                      lw=2.5,
                      alpha=0.85,
                      label='PC{}'.format(k))
    iy = iy - ly - gapy
    subtit = '(a) '
    ax1.set_title(subtit, fontsize=12, ha='left', x=0.0)
    ax1.legend(bbox_to_anchor=(0.08, 1.02, .92, .10),
               loc='lower left',
               ncol=nk,
               mode="expand",
               borderaxespad=0.,
               fontsize=10)
    ax1.axhline(y=0., c='k', lw=0.8, ls='--')
    ax1.grid(ls=':')
    ax1.xaxis.set_major_formatter(DateFormatter('%b%Y'))
    ax1.yaxis.set_minor_locator(AutoMinorLocator(2))
    ax1.yaxis.set_ticks_position('both')
    ax1.tick_params(axis='both', labelsize=10)

    ###--- Next, draw global maps
    ###--- Map Projection
    center = 180  # Want to draw a map where dateline is on the center
    proj = ccrs.PlateCarree(central_longitude=center)
    data_crs = ccrs.PlateCarree()

    map_extent = [0., 359.9, -61, 61]  # Range to be shown
    img_range = pdata['img_bound']

    val_max = max(np.nanmin(pdata['ev_map']) * -1, np.nanmax(pdata['ev_map']))
    val_min, val_max = val_max * -0.9, val_max * 0.9
    abc = 'abcdefgh'

    ###--- Color map
    cm = plt.cm.get_cmap('RdBu_r').copy()
    cm.set_bad('0.9')  # For the gridcell of NaN

    props = dict(vmin=val_min,
                 vmax=val_max,
                 origin='lower',
                 extent=img_range,
                 cmap=cm,
                 transform=data_crs)

    for i, (data, k) in enumerate(zip(pdata['ev_map'], pdata['tgt_nums'])):
        ax2 = fig.add_axes([ix, iy - ly, lx, ly], projection=proj)
        ax2.set_extent(map_extent, crs=data_crs)
        map1 = ax2.imshow(data, **props)

        subtit = '({}) EOF{}'.format(abc[i + 1], k)
        vf.map_common(ax2,
                      subtit,
                      data_crs,
                      xloc=60,
                      yloc=20,
                      gl_lab_locator=[True, True, False, True])

        iy = iy - ly - gapy
    vf.draw_colorbar(fig,
                     ax2,
                     map1,
                     type='horizontal',
                     size='panel',
                     gap=0.06,
                     extend='both',
                     width=0.02)

    ##-- Seeing or Saving Pic --##
    outfnm = pdata['out_fnm']
    print(outfnm)
    #fig.savefig(outfnm,dpi=100)   # dpi: pixels per inch
    fig.savefig(outfnm, dpi=150, bbox_inches='tight')  # dpi: pixels per inch
    # Defalut: facecolor='w', edgecolor='w', transparent=False
    plt.show()
    return