Skip to content
\n
# all imports should go here\nimport pandas as pd\nimport sys\nimport os\nimport subprocess\nimport datetime\nimport platform\nimport datetime\nimport math\n\nimport matplotlib.pyplot as plt\n#import seaborn as sb\n\nfrom geemap import cartoee\n\nimport cartopy\nimport cartopy.crs as ccrs\nfrom cartopy.io.img_tiles import OSM\nimport cartopy.feature as cfeature\nfrom cartopy.io import shapereader\nfrom cartopy.io.img_tiles import StamenTerrain\nfrom cartopy.io.img_tiles import GoogleTiles\nfrom owslib.wmts import WebMapTileService\n\nfrom matplotlib.path import Path\nimport matplotlib.patheffects as PathEffects\nfrom matplotlib import patheffects\nimport matplotlib.patches as mpatches\nimport matplotlib.lines as mlines\n\nimport numpy as np\n\n\nfig = plt.figure(figsize=(10,12))\n\n# ------------------------------- Surrounding frame ------------------------------\n# set up frame full height, full width of figure, this must be called first\n\nleft = -0.05\nbottom = -0.05\nwidth = 1.1\nheight = 1.05\nrect = [left,bottom,width,height]\nax3 = plt.axes(rect)\n\n\n\n# turn on the spines we want, ie just the surrounding frame\nplt.axis('off')\n\n\nax3.text(0.01,0.01,'© Don Cameron, 2017: net-analysis.com. '+\n            'Map generated at '+datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"), fontsize=8)\n\n\n\n# ---------------------------------  Main Map -------------------------------------\n#\n# set up main map almost full height (allow room for title), right 80% of figure\nBORDERS2_10m = cartopy.feature.NaturalEarthFeature(\n    'cultural', 'admin_1_states_provinces','10m', edgecolor='black', facecolor='none'\n)\n\nLAND_10m = cartopy.feature.NaturalEarthFeature(\n    'physical', 'land', '10m',edgecolor='face',facecolor=cartopy.feature.COLORS['land']\n)\n\nRIVERS_10m = cartopy.feature.NaturalEarthFeature(\n    'physical', 'rivers_lake_centerlines', '10m',edgecolor=cartopy.feature.COLORS['water'],facecolor='none'\n)\n\nleft = 0.2\nbottom = 0\nwidth = 0.8\nheight = 0.90\nrect = [left,bottom,width,height]\n\nax = plt.axes(rect, projection=ccrs.PlateCarree())\nax.set_extent((150, 155, -30, -23))\n\n\nax.coastlines(resolution='10m', zorder=2)\n\n#land polygons, including major islands, use cartopy default color\nax.add_feature(LAND_10m)\nax.add_feature(RIVERS_10m)\nax.add_feature(BORDERS2_10m, edgecolor='grey')\n\nax.stock_img()\n# stock image is good enough for example, but OCEAN_10m could be used, but very slow\n#       ax.add_feature(OCEAN_10m)\n\nax.gridlines(draw_labels=True, xlocs=[150, 152, 154, 155])\n\ncartoee.add_scale_bar(ax, length=100, fontsize=16)\ncartoee.add_north_arrow(ax, xy = (0.9, 0.95))\n\n\n# ---------------------------------Locating Map ------------------------\n#\n# set up index map 20% height, left 16% of figure\nleft = 0\nbottom = 0\nwidth = 0.16\nheight = 0.2\nrect = [left,bottom,width,height]\n\n\nax2 = plt.axes(rect, projection=ccrs.PlateCarree())\nax2.set_extent((110,160, -45, -10))\n#  ax2.set_global()  will show the whole world as context\n\nax2.coastlines(resolution='110m', zorder=2)\nax2.add_feature(cfeature.LAND)\nax2.add_feature(cfeature.OCEAN)\n\nax2.gridlines()\n\n\nlon0,lon1,lat0,lat1 = ax.get_extent()\nbox_x = [lon0, lon1, lon1, lon0, lon0]\nbox_y = [lat0, lat0, lat1, lat1, lat0]\n\nplt.plot(box_x, box_y, color='red',  transform=ccrs.Geodetic())\n\n\n# -------------------------------- Title -----------------------------\n# set up map title top 4% of figure, right 80% of figure\n\nleft = 0.2\nbottom = 0.95\nwidth = 0.8\nheight = 0.04\nrect = [left,bottom,width,height]\nax6 = plt.axes(rect)\nax6.text(0.5, 0.0,'Multi-Axes Map Example', ha='center', fontsize=20)\nplt.axis('off')\n\n\n# ------------------------------------  Legend -------------------------------------\n\n# legends can be quite long, so set near top of map (0.4 - bottom + 0.5 height = 0.9 - near top)\nleft = 0\nbottom = 0.4\nwidth = 0.16\nheight = 0.5\nrect = [left,bottom,width,height]\nrect = [left,bottom,width,height]\nax5 = plt.axes(rect)\n\n# create an array of color patches and associated names for drawing in a legend\n# colors are the predefined colors for cartopy features (only for example, Cartopy names are unusual)\ncolors = sorted(cartopy.feature.COLORS.keys())\n\n# handles is a list of patch handles\nhandles = []\n# names is the list of corresponding labels to appear in the legend\nnames = []\n\n# for each cartopy defined color, draw a patch, append handle to list, and append color name to names list\nfor c in colors:\n    patch = mpatches.Patch(color=cfeature.COLORS[c], label=c)\n    handles.append(patch)\n    names.append(c)\n#end for\n\n# do some example lines with colors\nriver = mlines.Line2D([], [], color=cfeature.COLORS['water'], marker='',\n                            markersize=15, label='river')\ncoast = mlines.Line2D([], [], color='black', marker='',\n                            markersize=15, label='coast')\nbdy  = mlines.Line2D([], [], color='grey', marker='',\n                    markersize=15, label='state boundary')\nhandles.append(river)\nhandles.append(coast)\nhandles.append(bdy)\nnames.append('river')\nnames.append('coast')\nnames.append('state boundary')\n\n\n# create legend\nax5.legend(handles, names)\nax5.set_title('Legend',loc='left')\nplt.axis('off')\n\nplt.savefig(\"test.png\", bbox_inches='tight')\n\nplt.show()\n\n
\n

\"test\"

","upvoteCount":1,"url":"https://github.com/gee-community/geemap/discussions/238#discussioncomment-254697"}}}

How to use get_map() on a sub-axis? #238

Answered by ErikSeras
ErikSeras asked this question in Q&A
Discussion options

You must be logged in to vote

This is the code that summarizes the example, I tested it in Google Colab.

# Install
! sudo add-apt-repository -y ppa:ubuntugis/ubuntugis-unstable
! sudo apt-get update
! sudo apt-get install libudunits2-dev libgdal-dev libgeos-dev libproj-dev
! sudo apt-get install libprotobuf-dev protobuf-compiler libv8-dev libjq-dev

! pip install OWSLib

! pip install cartopy geemap
# all imports should go here
import pandas as pd
import sys
import os
import subprocess
import datetime
import platform
import datetime
import math

import matplotlib.pyplot as plt
#import seaborn as sb

from geemap import cartoee

import cartopy
import cartopy.crs as ccrs
from cartopy.io.img_tiles import OSM
import carto…

Replies: 4 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@giswqs
Comment options

@ErikSeras
Comment options

Answer selected by ErikSeras
Comment options

You must be logged in to vote
1 reply
@giswqs
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants