如何在鼠标点击/悬停时在地图上显示标签?

2024-10-03 13:17:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python中的Basemap绘制了一系列经纬度对。示例图像是:blue dots are the plotted stations

当鼠标点击(或悬停)在点上时,我需要显示地点的名称。我有包含经纬度对的文件中的站名。在

首先,我如何在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()

Tags: andin功能forplotlinecolor经纬度
1条回答
网友
1楼 · 发布于 2024-10-03 13:17:02

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

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import math


# 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)

##fake coordinates and labels
lattd, lngtd, labels = zip(*[
    (20.6, 79.0, 'point 1'),
    (21.3, 77.5, 'point 2'),
    (13.0, 77.6, 'point 3'),
])

##a list for keeping track of all annotations
annotations = [None for label in labels]

##defining size of markers:
markersize = 5
markersize_inches = markersize/72.

##setting up figure
fig, ax = plt.subplots()
m = Basemap(
    width=4000000,height=4000000,projection='lcc',
    resolution='c',lat_1=45.,lat_2=55,lat_0=20,lon_0=80.,
    ax = ax,
)
m.drawcountries()
m.drawcoastlines(linewidth=0.50)
m.bluemarble()

##data coordinates
xdata, ydata = zip(*[m(lon,lat) for lon,lat in zip(lngtd,lattd)])
ax.plot(xdata,ydata,'bo', mec='k', ms = markersize)

##figure coordinates in inches
trans = ax.transData+fig.dpi_scale_trans.inverted()

##function for checking mouse coordinates and annotating
def on_move(event):
    if event.inaxes:
        x0, y0 = trans.transform((event.xdata, event.ydata))
        xfig, yfig = zip(*[trans.transform((x,y)) for x,y in zip(xdata,ydata)])
        dists = [math.sqrt((x-x0)**2+(y-y0)**2) for x,y in zip(xfig, yfig)]

        for n,(x,y,dist,label) in enumerate(zip(xdata,ydata,dists, labels)):
            if dist < markersize_inches and annotations[n] is None:
                annotations[n]=ax.annotate(
                    label,
                    [x,y], xycoords='data',
                    xytext = (10,10), textcoords='offset points',
                    ha='left', va='center',
                    bbox=dict(facecolor='white', edgecolor='black', boxstyle='round'),
                    zorder=10,
                )
                fig.canvas.draw()

            elif dist > markersize_inches and annotations[n] is not None:
                annotations[n].remove()
                annotations[n] = None
                fig.canvas.draw()

##connecting the event handler
cid = fig.canvas.mpl_connect('motion_notify_event', on_move)


plt.show()

相关问题 更多 >