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

时间:2018-03-28 07:10:45

标签: python matplotlib plot matplotlib-basemap

我在Python中使用Basemap绘制了一系列纬度 - 经度对。示例图片为:blue dots are the plotted stations

我需要在点上点击(或悬停)鼠标时显示地点的名称。我的电台名称包含纬度 - 经度对的文件。

首先,如何在Basemap中创建悬停功能(或更好的东西)? 其次,如何在悬停点时将文本添加为​​标签?

这是我到目前为止所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement results = doc.Descendants().Where(x => x.Name.LocalName == "Results").FirstOrDefault();
            XNamespace nsC = results.GetNamespaceOfPrefix("c");

            Dictionary<string, List<string>> dict = results.Descendants(nsC + "KeyValueOfstringstring")
               .GroupBy(x => (string)x.Element(nsC + "Key"), y => (string)y.Element(nsC + "Value"))
               .ToDictionary(x => x.Key, y => y.ToList());
        }
    }

}

1 个答案:

答案 0 :(得分:2)

手头的问题可以通过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()