You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修改Contextily中默认属性文本的显示位置

Repositioning Contextily Basemap Attribution Text

Great question! Unfortunately, Contextily's add_basemap function doesn't have a built-in parameter to reposition the attribution text (like Leaflet's flexible options) out of the box. But there's a straightforward workaround to achieve exactly what you want—here's how:

Workaround: Hide Default Attribution & Add Custom Text Manually

The core idea is to disable Contextily's automatic attribution, then use Matplotlib's native text tools to place the attribution wherever you want (right side, top, etc.).

Modified Code Example

Here's your original code updated to place the attribution in the top-right corner:

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)

# Hide the default attribution from Contextily
ctx.add_basemap(ax, crs=gdf.crs.to_string(), attribution=False)

# Get the default attribution text for your basemap (OpenStreetMap Mapnik here)
attr_text = ctx.providers.OpenStreetMap.Mapnik['attribution']

# Add the attribution to the top-right corner
# Using `ax.transAxes` for relative coordinates (0,0 = bottom-left; 1,1 = top-right)
ax.text(
    x=0.98, y=0.98,
    s=attr_text,
    transform=ax.transAxes,
    ha='right', va='top',  # Align text to the right and top of the position
    fontsize=8,
    color='gray'
)

Customize the Position

You can adjust the x and y values (along with ha/va alignment) to place the attribution anywhere:

  • Top center: Set x=0.5, y=0.98, ha='center'
  • Bottom right: Set x=0.98, y=0.02, ha='right', va='bottom'
  • Left middle: Set x=0.02, y=0.5, ha='left', va='center'

Why This Works

Contextily relies on Matplotlib under the hood, so leveraging Matplotlib's text placement gives you full control over the attribution's position—just like how Leaflet lets you configure attribution placement via its API. This method is far more flexible than any hypothetical built-in parameter, since you can tweak font size, color, and alignment too.

内容的提问来源于stack exchange,提问作者mins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 09:12:26