簡體   English   中英

如何在 geopandas map 上添加“指北針”

[英]how to add a "North Arrow" on a geopandas map

我使用 geopandas 創建了一個 map,但我無法在 map 上添加“指北針”。

創建 map 后,我嘗試使用 matplotlib.image 模塊添加“指北針”,並嘗試了不同的方法(請參見下面的示例),但都沒有提供好的結果。 我正在尋找更好的代碼,可以在地圖上添加一個好的“指北針”

import matplotlib.image as img

from matplotlib.offsetbox import TextArea, DrawingArea, OffsetImage, 
    AnnotationBbox

im=img.imread(r'C:\Users\jnisengw\Dropbox\2019\Data 
    Science\QGIS\north_arrow1.png')

imagebox = OffsetImage(im,zoom=0.27)

ab = AnnotationBbox(imagebox, (598500,4699000))

ax.add_artist(ab)

如果只需要添加一個簡單的箭頭,還可以考慮annotate()方法

import geopandas as gpd
import matplotlib.pyplot as plt

gdf = gpd.read_file(gpd.datasets.get_path('nybb'))

fig, ax = plt.subplots(figsize=(6, 6))
gdf.plot(ax=ax)

x, y, arrow_length = 0.5, 0.5, 0.1
ax.annotate('N', xy=(x, y), xytext=(x, y-arrow_length),
            arrowprops=dict(facecolor='black', width=5, headwidth=15),
            ha='center', va='center', fontsize=20,
            xycoords=ax.transAxes)

在此處輸入圖像描述

注意:當您通過xycoords=ax.transAxes時,x,y 坐標被歸一化, x, y = 0.5, 0.5表示您將箭頭放在 map 的中間。

萬一有人還需要這個...
EOmaps v3.1現在有一個正確的指北針並且可以與 geopandas 很好地交互!

from eomaps import Maps
m = Maps()
m.add_feature.preset.ocean()
m.add_compass(style="north arrow")

EOmaps - 指南針

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM