我使用下面的代码行创建了亚洲的基本地图。使用的投影是"marcator“。我想在这张地图上展示的地区是尼泊尔的奇特旺。还提供了坐标。
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
chitwan = (27.618556599999998, 84.45385600798173)
fig = plt.figure()
ax = fig.add_axes([0.5, 0.5, 2, 2]) #left, bottom, width, height
m = Basemap(
llcrnrlon = 30,
llcrnrlat = 0,
urcrnrlon = 120,
urcrnrlat = 60,
projection = "merc",
resolution = "l"
)
m.plot(chitwan[1], chitwan[0],
latlon = True,
marker = "s",
color = "blue",
markersize = 20)
m.drawcoastlines()
m.fillcontinents()
m.drawcountries()
m.drawstates()
plt.annotate(text = "Chitwan, Nepal", xy = chitwan)
plt.show()我可以在尼泊尔的奇特旺添加一个蓝色方格标记
m.plot(chitwan[1], chitwan[0],
latlon = True,
marker = "s",
color = "blue",
markersize = 40)通过latlon = True,我可以直接使用位置的坐标绘制标记。
不过,我也想在同一地点将“尼泊尔奇特旺”作为一个文本加以说明。我试过plt.annotate(text = "Chitwan, Nepal", xy = chitwan)。但是,这会将文本绘制在基地图的左下角,如下所示。

我认为这应该是因为文本的纬度和经度坐标没有投影到基本地图上。那么,我如何投影位置的坐标,以便文本也被插入到与标记相同的位置?是否可以通过直接传递位置的确切坐标并为标记传递类似于latlon = True的参数来做到这一点?
发布于 2022-01-23 11:40:45
好的。我自己想出了解决办法。
我需要用:
x, y = m(chitwan[1], chitwan[0])X和y分别投影到chitwan的经度和纬度的地图位置。
然后,我对案文作了如下注解:
ax.annotate(text = "Chitwan, Nepal",
xy = (x+5,y+5),
)我得到的结果如下所示:

https://stackoverflow.com/questions/70821035
复制相似问题