[英]How to plot labels for some cites on the map? using cartopy
我有一個 excel 文件,其中包含一些引用的信息,現在我想 plot 那些引用在 map 上的名稱。 我編寫了以下代碼,但是,我無法正確獲取引用的名稱。 對於每個引用,我都會得到一個 label 和“名稱”的完整列表。歡迎提出任何建議。
#我無法安裝 PyGMT 或底圖甚至 geopandas,所以我只有 cartopy。
import pandas as pd
import csv
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
df = pd.read_excel('./seismogram.xlsx')
lon = df['longitude'].to_list()
lat = df['latitude '].to_list()
name = (df['code']).to_list()
def main():
fig = plt.figure(figsize=(15,15))
plt.rcParams["font.size"] = 18
ax = fig.add_subplot(1,1,1, projection=ccrs.PlateCarree())
ax.set_extent([128, 133, 30, 35], crs=ccrs.PlateCarree())
ax.set_title("Japan")
ax.coastlines(resolution='10m')
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.stock_img()
return fig, ax
fig, ax = main()
ax.scatter(lon, lat, color="r", marker="o", s = 15)
zip_object = zip(lon, lat, name)
for (lg, lt, ne) in zip_object:
ax.text(lg - .05, lt + .05,
name,
va='center',
ha='right', transform=ccrs.Geodetic(), fontweight='bold')
plt.show()
從您的 for 循環中,我 99% 確定您應該使用ne
代替name
:
zip_object = zip(lon, lat, name)
for (lg, lt, ne) in zip_object:
ax.text(lg - .05, lt + .05,
ne,
va='center',
ha='right', transform=ccrs.Geodetic(), fontweight='bold')
容易犯錯誤:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.