繁体   English   中英

如何使用 Python 中位置的确切坐标在底图中注释文本?

[英]How can I annotate text in Basemap using the exact coordinates of the location in Python?

我使用以下代码行创建了亚洲底图。 使用的投影是“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允许我直接使用该地点的坐标绘制标记。

但是,我还想在同一位置将“Chitwan, Nepal”注释为文本。 我试过plt.annotate(text = "Chitwan, Nepal", xy = chitwan) 但是,这会将文本绘制在 Basemap 的左下角,如下所示。

在此处输入图像描述

我认为这应该是因为文本的经纬度坐标没有投影到 Basemap 的坐标上。 那么如何投影我的位置坐标,以便文本也插入与标记相同的位置? 是否可以通过直接传递该地点的确切坐标并为标记传递类似于latlon = True的参数来做到这一点?

好的。 我自己想出了办法。

我需要使用:

x, y = m(chitwan[1], chitwan[0])

x 和 y 投影到 map 位置,经度和纬度分别为 chitwan。

然后我将文本注释如下:

ax.annotate(text = "Chitwan, Nepal",
           xy = (x+5,y+5),
           )

我得到了结果 plot ,如图所示: 解决后的图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM