Python和Matplotlib和鼠标悬停注解 [英] Python and Matplotlib and Annotations with Mouse Hover

查看:5457
本文介绍了Python和Matplotlib和鼠标悬停注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用这种code有弹出地图上annotatations当我在底图Matplotlib情节点点击。

I am currently employing this code to have pop up annotatations on a map when i click on a point in a Basemap Matplotlib Plot.

dcc = DataCursor(self.figure.gca())
self.figure.canvas.mpl_connect('pick_event',dcc)
plot_handle.set_picker(5)
self.figure.canvas.draw()

class DataCursor(object):

    import matplotlib.pyplot as plt

    text_template = 'x: %0.2f\ny: %0.2f' 
    x, y = 0.0, 0.0 
    xoffset, yoffset = -20 , 20
    text_template = 'A: %s\nB: %s\nC: %s'


    def __init__(self, ax): 
        self.ax = ax 
        self.annotation = ax.annotate(self.text_template,  
                xy=(self.x, self.y), xytext=(0,0),
                textcoords='axes fraction', ha='left', va='bottom', fontsize=10,
                bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=1), 
                arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0') 
                ) 
        self.annotation.set_visible(False)
        self.annotation.draggable()


    def __call__(self, event):

        self.event = event 
        self.x, self.y = event.mouseevent.xdata, event.mouseevent.ydata

        if self.x is not None:
            glim = pickle.load(open("ListA.py","rb"))
            tlim = pickle.load(open("ListB.py","rb"))
            vlim = pickle.load(open("ListC.py","rb"))
            a = glim[event.ind[0]] # ['Name'][event.ind[0]]
            b = tlim[event.ind[0]]
            c = vlim[event.ind[0]]
            temp_temp=self.text_template % (a, b, c)
            if temp_temp == self.annotation.get_text() and self.annotation.get_visible(): 
                self.annotation.set_visible(False) 
                event.canvas.draw() 
                return 
            self.annotation.xy = self.x, self.y
            self.annotation.set_text(self.text_template % (a, b, c))
            self.annotation.set_visible(True)

            event.canvas.draw()

我想知道,是如何使用鼠标悬停,而不是点击一个点来显示的注解?

What I am wondering, is how to show the annotations using mouse hover rather than clicking on a point?

我看到motion_notify_event,但它似乎当我把鼠标移动绘图区周围的code得到错误。有什么想法?

I have seen "motion_notify_event" but it seems the code gets errors when i move the mouse around the plot area. Any Thoughts?

推荐答案

在看看<一个href=\"http://stackoverflow.com/questions/7908636/possible-to-make-labels-appear-when-hovering-over-a-point-in-matplotlib\">this问题和演示

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

这篇关于Python和Matplotlib和鼠标悬停注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆