Skip to content

Commit 5c8b911

Browse files
authored
Merge pull request #43 from darribas/add#25
Adding attribution text
2 parents 8d787da + 5e9709f commit 5c8b911

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

contextily/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
from . import tile_providers as sources
66
from .place import Place, plot_map
77
from .tile import *
8-
from .plotting import add_basemap
8+
from .plotting import add_basemap, add_attribution
99

1010
__version__ = '0.99.0.dev'

contextily/place.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import matplotlib.pyplot as plt
55
from warnings import warn
66
from .tile import howmany, bounds2raster, bounds2img, _sm2ll, _calculate_zoom
7-
from .plotting import ATTRIBUTION, INTERPOLATION, ZOOM
7+
from .plotting import ATTRIBUTION, INTERPOLATION, ZOOM, add_attribution
88

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

106106
def plot(self, ax=None, zoom=ZOOM, interpolation=INTERPOLATION,
107-
attribution_text = ATTRIBUTION):
107+
attribution = ATTRIBUTION):
108108
"""
109109
Plot a `Place` object
110110
...
@@ -124,8 +124,8 @@ def plot(self, ax=None, zoom=ZOOM, interpolation=INTERPOLATION,
124124
[Optional. Default='bilinear'] Interpolation
125125
algorithm to be passed to `imshow`. See
126126
`matplotlib.pyplot.imshow` for further details.
127-
attribution_text : str
128-
[Optional. Default=''] Text to be added at the
127+
attribution : str
128+
[Optional. Defaults to standard `ATTRIBUTION`] Text to be added at the
129129
bottom of the axis.
130130
131131
Returns
@@ -152,6 +152,8 @@ def plot(self, ax=None, zoom=ZOOM, interpolation=INTERPOLATION,
152152
axisoff = True
153153
ax.imshow(im, extent=bbox, interpolation=interpolation)
154154
ax.set(xlabel="X", ylabel="Y")
155+
if attribution:
156+
add_attribution(ax, attribution)
155157
if title is not None:
156158
ax.set(title=title)
157159
if axisoff:
@@ -163,7 +165,8 @@ def __repr__(self):
163165
self.place, self.n_tiles, self.zoom, self.im.shape[:2])
164166
return s
165167

166-
def plot_map(place, bbox=None, title=None, ax=None, axis_off=True, latlon=True):
168+
def plot_map(place, bbox=None, title=None, ax=None, axis_off=True,
169+
latlon=True, attribution = ATTRIBUTION):
167170
"""Plot a map of the given place.
168171
169172
Parameters
@@ -176,6 +179,9 @@ def plot_map(place, bbox=None, title=None, ax=None, axis_off=True, latlon=True):
176179
The axis on which to plot. If None, one will be created.
177180
axis_off : bool
178181
Whether to turn off the axis border and ticks before plotting.
182+
attribution : str
183+
[Optional. Default to standard `ATTRIBUTION`] Text to be added at the
184+
bottom of the axis.
179185
180186
Returns
181187
-------
@@ -208,7 +214,8 @@ def plot_map(place, bbox=None, title=None, ax=None, axis_off=True, latlon=True):
208214
ax.set(xlabel="X", ylabel="Y")
209215
if title is not None:
210216
ax.set(title=title)
211-
217+
if attribution:
218+
add_attribution(ax, attribution)
212219
if axis_off is True:
213220
ax.set_axis_off()
214221
return ax

contextily/plotting.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import numpy as np
44
from . import tile_providers as sources
55
from .tile import _calculate_zoom, bounds2img, _sm2ll
6+
from matplotlib import patheffects
67

7-
ATTRIBUTION = ''
88
INTERPOLATION = 'bilinear'
99
ZOOM = 'auto'
10+
ATTRIBUTION = ("Map tiles by Stamen Design, under CC BY 3.0. "\
11+
"Data by OpenStreetMap, under ODbL.")
1012

1113
def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN,
12-
interpolation=INTERPOLATION, attribution_text = ATTRIBUTION,
14+
interpolation=INTERPOLATION, attribution = ATTRIBUTION,
1315
**extra_imshow_args):
1416
"""
1517
Add a (web/local) basemap to `ax`
@@ -33,8 +35,8 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN,
3335
[Optional. Default='bilinear'] Interpolation
3436
algorithm to be passed to `imshow`. See
3537
`matplotlib.pyplot.imshow` for further details.
36-
attribution_text : str
37-
[Optional. Default=''] Text to be added at the
38+
attribution : str
39+
[Optional. Defaults to standard `ATTRIBUTION`] Text to be added at the
3840
bottom of the axis.
3941
**extra_imshow_args : dict
4042
Other parameters to be passed to `imshow`.
@@ -90,5 +92,35 @@ def add_basemap(ax, zoom=ZOOM, url=sources.ST_TERRAIN,
9092
# Plotting
9193
ax.imshow(image, extent=extent,
9294
interpolation=interpolation, **extra_imshow_args)
95+
if attribution:
96+
add_attribution(ax, attribution)
9397
return ax
9498

99+
def add_attribution(ax, att=ATTRIBUTION):
100+
'''
101+
Utility to add attribution text
102+
...
103+
104+
Arguments
105+
---------
106+
ax : AxesSubplot
107+
Matplotlib axis with `x_lim` and `y_lim` set in Web
108+
Mercator (EPSG=3857)
109+
att : str
110+
[Optional. Defaults to standard `ATTRIBUTION`] Text to be added at the
111+
bottom of the axis.
112+
113+
Returns
114+
-------
115+
ax : AxesSubplot
116+
Matplotlib axis with `x_lim` and `y_lim` set in Web
117+
Mercator (EPSG=3857) and attribution text added
118+
'''
119+
minX, maxX = ax.get_xlim()
120+
minY, maxY = ax.get_ylim()
121+
ax.text(minX + (maxX - minX) * 0.005,
122+
minY + (maxY - minY) * 0.005,
123+
att, size=8,
124+
path_effects=[patheffects.withStroke(linewidth=2,
125+
foreground="w")])
126+
return ax

tests/test_ctx.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,7 @@ def test_add_basemap():
193193
assert_array_almost_equal(ax.images[0].get_array().mean(),
194194
184.10237852536648)
195195

196+
def test_attribution():
197+
f, ax = matplotlib.pyplot.subplots(1)
198+
ax = ctx.add_attribution(ax, 'Test')
199+

0 commit comments

Comments
 (0)