Example #1
0
def make_map(llcrnrlon=-51,
             urcrnrlon=-30.2,
             llcrnrlat=-15,
             urcrnrlat=7.1,
             projection='merc',
             resolution='i',
             figsize=(6, 6),
             inset=True,
             axe=None,
             steplat=2,
             steplon=2,
             inloc=1,
             contcolor='0.85'):
    '''
    This function creates a basemap map easily with option
    of inset axe with geolocation.

    It's all based on make_map from Filipe Fernandes, but with some
    changes
    '''

    if axe == None:
        fig, ax = plt.subplots(figsize=figsize)
        m = Basemap(llcrnrlon=llcrnrlon,
                    urcrnrlon=urcrnrlon,
                    llcrnrlat=llcrnrlat,
                    urcrnrlat=urcrnrlat,
                    projection=projection,
                    resolution=resolution)
    else:
        m = Basemap(llcrnrlon=llcrnrlon,
                    urcrnrlon=urcrnrlon,
                    llcrnrlat=llcrnrlat,
                    urcrnrlat=urcrnrlat,
                    projection=projection,
                    resolution=resolution,
                    ax=axe)
        ax = axe
        print('Map Created!')

    m.drawstates(zorder=22)
    m.drawcoastlines(zorder=21)
    m.fillcontinents(color=contcolor, zorder=20)
    meridians = np.arange(np.floor(llcrnrlon), np.ceil(urcrnrlon) + 2, steplon)
    parallels = np.arange(np.floor(llcrnrlat), np.ceil(urcrnrlat) + 2, steplat)
    m.drawparallels(parallels, linewidth=0.4, labels=[1, 0, 0, 0], zorder=23)
    m.drawmeridians(meridians, linewidth=0.4, labels=[0, 0, 0, 1], zorder=24)
    m.llcrnrlon = llcrnrlon
    m.urcrnrlon = urcrnrlon
    m.llcrnrlat = llcrnrlat
    m.urcrnrlat = urcrnrlat
    m.ax = ax

    if inset:
        axin = inset_axes(m.ax, width="30%", height="30%", loc=inloc)
        # Global inset map.
        inmap = Basemap(projection='ortho',
                        lon_0=np.mean(-42),
                        lat_0=np.mean(-4),
                        ax=axin,
                        anchor='NE')
        inmap.drawcountries(color='white')
        inmap.fillcontinents(color='gray')
        bx, by = inmap(m.boundarylons, m.boundarylats)
        xy = list(zip(bx, by))
        mapboundary = Polygon(xy, edgecolor='r', linewidth=1, fill=False)
        inmap.ax.add_patch(mapboundary)
    if axe == None:
        return fig, m
    else:
        return m