我用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()
手头的问题可以用matplotlib event handling,annotations,和(对于图形大小无关的标记拾取)transformations来解决。下面是一个例子,它显示了当鼠标指针移动到其中一个蓝色标记上时的标签。由于标记大小是以点表示的(一个点是1/72英寸),我将数据坐标转换为图形坐标,遵循matplotlib transformation tutorial中的阴影效果转换。希望这有帮助。在
相关问题 更多 >
编程相关推荐