Skip to content

Commit

Permalink
add add_basemap function to new plotting.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ljwolf committed May 23, 2018
1 parent 0b997d3 commit 4850849
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions contextily/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
from . import tile_providers as sources
from .place import Place, plot_map, calculate_zoom
from .tile import *
from .plotting import add_basemap_to_axis as add_basemap
37 changes: 37 additions & 0 deletions contextily/plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from . import sources
from .tile import bounds2img
from warnings import warn

def add_basemap_to_axis(ax, zoom, url=sources.ST_TERRAIN,
interpolation='sinc', **imshow_kws):
"""
Tool to add basemap to an axis where a geopandas dataframe has already been plotted.
Assumes the axis has been set up in spherical/web mercator coordinates.
Arguments
----------
ax : matplotlib axis object
axis on which to add the basemap. It should already have had a geodataframe
plotted on it.
zoom: int
level of detail in the basemap.
url : str
[Optional. Default:
'http://tile.stamen.com/terrain/tileZ/tileX/tileY.png'] URL for
tile provider. The placeholders for the XYZ need to be `tileX`,
`tileY`, `tileZ`, respectively. See `cx.sources`.
interpolation : string
method of image interpolation to provide to the matplotlib.pyplot.imshow.
(Default: 'sinc')
Returns
-------
ax, modified in place to have a basemap matching its bounds.
further keyword arguments supported by this function are documented by matplotlib.pyplot.imshow
"""
left, right, bottom, top = ax.axis()
basemap, bounds = bounds2img(left, bottom, right, top, zoom=zoom, url=url)
ax.imshow(basemap, extent=bounds, interpolation=interpolation, **imshow_kws)
ax.axis((left, right, bottom, top))
return ax

0 comments on commit 4850849

Please sign in to comment.