show annotations only on hovering

Hi all.
I'm using matplotlib to draw some graph in a pyqt application.
Is there a way to show infos only when the mouse hover the plot nodes?
At the moment I'm stuck using this (ugly, and conceptually wrong) piece of
code, written by me...

basically I add hidden annotations where I need, then (ab)use the
fig.canvas.mpl_connect function with the 'motion_notify_event' option.

Is there a "motion_mouse_enter_plot" / "motion_mouse_exit_plot" or something
similar? I see there is the "pick_event" interface, but I don't want to
show infos "on click", but "on hovering"

#!/usr/bin/env python

class show_annotation(object):
    def __init__(self, annotations):
        '''"fake" class, mostly a "function with memory"'''
        self.annotations = annotations

    def __call__(self, mouse_event):
        ax = mouse_event.inaxes
        if not ax:
            return
        line = ax.get_lines()[0]
        contained, infos = line.contains(mouse_event)
        if not contained: # eventually exited
            for annotation in self.annotations.values():
                annotation.set_visible(False)
        else:
            xdata, ydata = line.get_data()
            ind = infos['ind'][0]
            annotation = self.annotations[xdata[ind], ydata[ind]]
            if not annotation.get_visible(): # is entered
                annotation.set_visible(True)
        ax.figure.canvas.draw()

if __name__ == '__main__':
    from random import randint
    x = range(10)
    y = [ randint(0, i) for i in x ]
    labels = [ 'label_%d' % i for i in x ]

    from matplotlib import pyplot
    fig = pyplot.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(x, y)

    # waiting for a "dict comprehension" sintax
    annotations = {}
    for label, xe, ye in zip(labels, x, y):
        annotations[xe,ye] = ax.annotate(label, (xe,ye), visible=False)

    fig.canvas.mpl_connect('motion_notify_event',
            show_annotation(annotations))
    pyplot.show()

···

--
By ZeD

Vito De Tullio schrieb:

Hi all.
I'm using matplotlib to draw some graph in a pyqt application.
Is there a way to show infos only when the mouse hover the plot nodes?
At the moment I'm stuck using this (ugly, and conceptually wrong) piece of
code, written by me...

basically I add hidden annotations where I need, then (ab)use the fig.canvas.mpl_connect function with the 'motion_notify_event' option.

Is there a "motion_mouse_enter_plot" / "motion_mouse_exit_plot" or something
similar? I see there is the "pick_event" interface, but I don't want to
show infos "on click", but "on hovering"
  

I didn't understand what you mean by 'plot nodes', but there exist figure_enter_event, figure_leave_event, axes_enter_event and axes_leave_event, see
http://matplotlib.sourceforge.net/examples/event_handling/figure_axes_enter_leave.html

Gregor