![](/img/trans.png)
[英]How do you display the scale in meters, the north arrow and the axes in latitude and longitude on a map with Geopandas?
[英]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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.