Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contextily/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
from . import tile_providers as sources
from .place import Place, plot_map
from .tile import *
from .plotting import add_basemap
from .plotting import add_basemap, add_attribution

__version__ = '0.99.0.dev'
19 changes: 13 additions & 6 deletions contextily/place.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import matplotlib.pyplot as plt
from warnings import warn
from .tile import howmany, bounds2raster, bounds2img, _sm2ll, _calculate_zoom
from .plotting import ATTRIBUTION, INTERPOLATION, ZOOM
from .plotting import ATTRIBUTION, INTERPOLATION, ZOOM, add_attribution

class Place(object):
"""Geocode a place by name and get its map.
Expand Down Expand Up @@ -104,7 +104,7 @@ def _get_map(self):
return im, bbox

def plot(self, ax=None, zoom=ZOOM, interpolation=INTERPOLATION,
attribution_text = ATTRIBUTION):
attribution = ATTRIBUTION):
"""
Plot a `Place` object
...
Expand All @@ -124,8 +124,8 @@ def plot(self, ax=None, zoom=ZOOM, interpolation=INTERPOLATION,
[Optional. Default='bilinear'] Interpolation
algorithm to be passed to `imshow`. See
`matplotlib.pyplot.imshow` for further details.
attribution_text : str
[Optional. Default=''] Text to be added at the
attribution : str
[Optional. Defaults to standard `ATTRIBUTION`] Text to be added at the
bottom of the axis.

Returns
Expand All @@ -152,6 +152,8 @@ def plot(self, ax=None, zoom=ZOOM, interpolation=INTERPOLATION,
axisoff = True
ax.imshow(im, extent=bbox, interpolation=interpolation)
ax.set(xlabel="X", ylabel="Y")
if attribution:
add_attribution(ax, attribution)
if title is not None:
ax.set(title=title)
if axisoff:
Expand All @@ -163,7 +165,8 @@ def __repr__(self):
self.place, self.n_tiles, self.zoom, self.im.shape[:2])
return s

def plot_map(place, bbox=None, title=None, ax=None, axis_off=True, latlon=True):
def plot_map(place, bbox=None, title=None, ax=None, axis_off=True,
latlon=True, attribution = ATTRIBUTION):
"""Plot a map of the given place.

Parameters
Expand All @@ -176,6 +179,9 @@ def plot_map(place, bbox=None, title=None, ax=None, axis_off=True, latlon=True):
The axis on which to plot. If None, one will be created.
axis_off : bool
Whether to turn off the axis border and ticks before plotting.
attribution : str
[Optional. Default to standard `ATTRIBUTION`] Text to be added at the
bottom of the axis.

Returns
-------
Expand Down Expand Up @@ -208,7 +214,8 @@ def plot_map(place, bbox=None, title=None, ax=None, axis_off=True, latlon=True):
ax.set(xlabel="X", ylabel="Y")
if title is not None:
ax.set(title=title)

if attribution:
add_attribution(ax, attribution)
if axis_off is True:
ax.set_axis_off()
return ax
40 changes: 36 additions & 4 deletions contextily/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import numpy as np
from . import tile_providers as sources
from .tile import _calculate_zoom, bounds2img, _sm2ll
from matplotlib import patheffects

ATTRIBUTION = ''
INTERPOLATION = 'bilinear'
ZOOM = 'auto'
ATTRIBUTION = ("Map tiles by Stamen Design, under CC BY 3.0. "\
"Data by OpenStreetMap, under ODbL.")

def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN,
interpolation=INTERPOLATION, attribution_text = ATTRIBUTION,
interpolation=INTERPOLATION, attribution = ATTRIBUTION,
**extra_imshow_args):
"""
Add a (web/local) basemap to `ax`
Expand All @@ -33,8 +35,8 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN,
[Optional. Default='bilinear'] Interpolation
algorithm to be passed to `imshow`. See
`matplotlib.pyplot.imshow` for further details.
attribution_text : str
[Optional. Default=''] Text to be added at the
attribution : str
[Optional. Defaults to standard `ATTRIBUTION`] Text to be added at the
bottom of the axis.
**extra_imshow_args : dict
Other parameters to be passed to `imshow`.
Expand Down Expand Up @@ -90,5 +92,35 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN,
# Plotting
ax.imshow(image, extent=extent,
interpolation=interpolation, **extra_imshow_args)
if attribution:
add_attribution(ax, attribution)
return ax

def add_attribution(ax, att=ATTRIBUTION):
'''
Utility to add attribution text
...

Arguments
---------
ax : AxesSubplot
Matplotlib axis with `x_lim` and `y_lim` set in Web
Mercator (EPSG=3857)
att : str
[Optional. Defaults to standard `ATTRIBUTION`] Text to be added at the
bottom of the axis.

Returns
-------
ax : AxesSubplot
Matplotlib axis with `x_lim` and `y_lim` set in Web
Mercator (EPSG=3857) and attribution text added
'''
minX, maxX = ax.get_xlim()
minY, maxY = ax.get_ylim()
ax.text(minX + (maxX - minX) * 0.005,
minY + (maxY - minY) * 0.005,
att, size=8,
path_effects=[patheffects.withStroke(linewidth=2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat trick! this is v. useful for annotating plots. had no idea it was in mpl.

foreground="w")])
return ax
4 changes: 4 additions & 0 deletions tests/test_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,7 @@ def test_add_basemap():
assert_array_almost_equal(ax.images[0].get_array().mean(),
184.10237852536648)

def test_attribution():
f, ax = matplotlib.pyplot.subplots(1)
ax = ctx.add_attribution(ax, 'Test')