Change default attribution text location in Contextily

Question:

Using this code:

import contextily as ctx
from shapely.geometry import box
import geopandas as gpd

minx, miny = 1.612359e+06, 0.950860e+07
maxx, maxy = 4.320331e+06, 1.007808e+07
rectangle = box(minx, miny, maxx, maxy)
gdf = gpd.GeoDataFrame(
    index=['Amazon'],
    geometry=[rectangle],
    crs='EPSG:5641')

dy = 10e4
ax = gdf.plot(figsize=(8,3), facecolor='none', edgecolor='lime')
ax.set_ylim(miny-dy, maxy+dy)
ctx.add_basemap(ax, crs=gdf.crs.to_string())

To get this plot:

enter image description here

I know it’s possible to change the size of the attribution text in contextily, and even to not plot it, but is there a way to change its location, e.g. to write it on the right or at the top?

Similar to how to change leaflet attribution, but for contextily.

Asked By: mins

||

Answers:

It looks like you can’t define the attribution in the add_basemap function, and while contextily does provide a more adaptable add_attribution function, that doesn’t allow you to specify the text position. There is another way though: manipulate the matplotlib text object generated by add_basemap:

txt = ax.texts[-1]
txt.set_position([0.99,0.98])
txt.set_ha('right')
txt.set_va('top')

Example output below. Best do this immediately after the add_basemap call to be sure you don’t pick up any other text object.

The original image but with attribution position in top right using the code above.

Answered By: jasmit
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.