Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and Matplotlib and Annotations with Mouse Hover

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?

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

like image 363
mcfly Avatar asked Sep 25 '12 05:09

mcfly


People also ask

Can matplotlib be interactive?

But did you know that it is also possible to create interactive plots with matplotlib directly, provided you are using an interactive backend? This article will look at two such backends and how they render interactivity within the notebooks, using only matplotlib.

Is gnuplot better than matplotlib?

Matplotlib = ease of use, Gnuplot = (slightly better) performance. I know this post is old and answered but I was passing by and wanted to put my two cents. Here is my conclusion: if you have a not-so-big data set, you should use Matplotlib. It's easier and looks better.

What is the difference between %Matplotlib inline and %Matplotlib notebook?

%matplotlib notebook will lead to interactive plots embedded within the notebook. %matplotlib inline will lead to static images of your plot embedded in the notebook.


1 Answers

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()
like image 107
root Avatar answered Oct 08 '22 06:10

root