【发布时间】:2018-09-06 18:24:59
【问题描述】:
我在 Python 中使用 Basemap 绘制了一系列经纬度对。示例图片为:
当鼠标在点上单击(或悬停)时,我需要显示地点的名称。我有包含纬度-经度对的文件的站名。
首先,如何在 Basemap 中实现悬停功能(或更好的功能)? 其次,当点悬停时如何将文本添加为标签?
这是我目前所拥有的:
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
lattd, lngtd = [], []
# latitude, longitude and station names are as headers rows(rows starting with "#", in plot_file.txt.
# read all the lines and make lists for latitude and longitude
inputfile = open('data-por-IN/plot_file.txt', 'r')
for i, line in enumerate(inputfile):
if line.startswith('#'):
lattd.append(int(line[56:62])/10000)
lngtd.append(int(line[65:71])/10000)
m = Basemap(width=4000000,height=4000000,projection='lcc',
resolution='c',lat_1=45.,lat_2=55,lat_0=20,lon_0=80.)
m.drawmapboundary(fill_color='aqua')
m.drawcountries()
m.drawcoastlines(linewidth=0.50)
m.fillcontinents(color='green', alpha = 0.6, lake_color='aqua')
for i in range(len(lngtd)):
lon = lngtd[i] #77.580643
lat = lattd[i] #12.972442
xpt,ypt = m(lon,lat)
lonpt, latpt = m(xpt,ypt,inverse=True)
m.plot(xpt,ypt,'yo')
ax.bluemarble()
plt.show()
【问题讨论】:
-
到目前为止你有什么尝试过的吗?您是否能够在没有底图的情况下实现单点悬停效果?如果不是,为什么要问这个复杂的案例?如果是这样,将其扩展到您的案例有什么问题?
标签: python matplotlib plot matplotlib-basemap