Python and Matplotlib and Annotations with Mouse Hover

谁说胖子不能爱 提交于 2019-11-30 05:25:43
root

Take a look at this question and demo :

from matplotlib.pyplot import figure, show
import numpy as npy
from numpy.random import rand


if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection)

    x, y, c, s = rand(4, 100)
    def onpick3(event):
        ind = event.ind
        print 'onpick3 scatter:', ind, npy.take(x, ind), npy.take(y, ind)

    fig = figure()
    ax1 = fig.add_subplot(111)
    col = ax1.scatter(x, y, 100*s, c, picker=True)
    #fig.savefig('pscoll.eps')
    fig.canvas.mpl_connect('pick_event', onpick3)

show()
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

lat = 20.5937
lon = 78.9629
points_with_annotation = list()

#Event is set for any movement and annotations are set to visible when on points with anotation
def on_move(event):
    visibility_changed = False
    for point, annotation in points_with_annotation:
        should_be_visible = (point.contains(event)[0] == True)

        if should_be_visible != annotation.get_visible():
            visibility_changed = True
            annotation.set_visible(should_be_visible)

    if visibility_changed:        
        plt.draw()


fig = plt.figure()
ax = plt.axes()

m = Basemap(projection='mill', llcrnrlat=-90, llcrnrlon=-180, urcrnrlat=90, urcrnrlon=180, resolution='c')
m.fillcontinents(color='white', lake_color='white')
m.drawcoastlines(linewidth=0.5, color='k')
m.drawcountries(linewidth=0.5, color='k')
m.drawmapboundary(fill_color='white')

xpt, ypt = m(lon,lat)
point, = m.plot(xpt, ypt, marker='o', markersize=5, alpha=0.85, visible=True)
annotation = ax.annotate("Lat: %s Lon: %s" % (lat,lon), 
    xy=(xpt, ypt), xycoords='data',
    xytext=(xpt + 1, ypt), textcoords='data',
    horizontalalignment="left",
    arrowprops=dict(arrowstyle="simple",
    connectionstyle="arc3,rad=-0.2"),
    bbox=dict(boxstyle="round", facecolor="w", 
    edgecolor="0.5", alpha=0.9)
        )


annotation.set_visible(False)
points_with_annotation.append([point, annotation])
on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()

Here's the code to plot a coordinate on a map and enable a popup during hover. I got the code for the on_move event from the answer to this question : Matplotlib basemap: Popup box Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!