[英]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.