Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point and line tooltips in matplotlib?

Tags:

matplotlib

I have a graph with some line segments (LineCollection) and some points. These lines and points have some values associated with them that are not graphed. I would like to be able to add a mouse-over tool-tip or other method of easily finding the associated value for the points and line. Is this possible for either points or lines segments?

like image 439
John Salvatier Avatar asked Dec 15 '10 17:12

John Salvatier


People also ask

Is Plotly better than matplotlib?

Plotly has several advantages over matplotlib. One of the main advantages is that only a few lines of codes are necessary to create aesthetically pleasing, interactive plots. The interactivity also offers a number of advantages over static matplotlib plots: Saves time when initially exploring your dataset.

What is the use of PLT show () in Python?

If you are using Matplotlib from within a script, the function plt. show() is your friend. plt. show() starts an event loop, looks for all currently active figure objects, and opens one or more interactive windows that display your figure or figures.

Which matplotlib function used to draw a line chart is Line?

To create a matplotlib line chart, you need to use the vaguely named plt. plot() function.


3 Answers

For points, I have found a way, but you have to use the WX backend

"""Example of how to use wx tooltips on a matplotlib figure window.
Adapted from http://osdir.com/ml/python.matplotlib.devel/2006-09/msg00048.html"""

import matplotlib as mpl
mpl.use('WXAgg')
mpl.interactive(False)

import pylab as pl
from pylab import get_current_fig_manager as gcfm
import wx
import numpy as np
import random


class wxToolTipExample(object):
    def __init__(self):
        self.figure = pl.figure()
        self.axis = self.figure.add_subplot(111)

        # create a long tooltip with newline to get around wx bug (in v2.6.3.3)
        # where newlines aren't recognized on subsequent self.tooltip.SetTip() calls
        self.tooltip = wx.ToolTip(tip='tip with a long %s line and a newline\n' % (' '*100))
        gcfm().canvas.SetToolTip(self.tooltip)
        self.tooltip.Enable(False)
        self.tooltip.SetDelay(0)
        self.figure.canvas.mpl_connect('motion_notify_event', self._onMotion)

        self.dataX = np.arange(0, 100)
        self.dataY = [random.random()*100.0 for x in xrange(len(self.dataX))]
        self.axis.plot(self.dataX, self.dataY, linestyle='-', marker='o', markersize=10, label='myplot')

    def _onMotion(self, event):
        collisionFound = False
        if event.xdata != None and event.ydata != None: # mouse is inside the axes
            for i in xrange(len(self.dataX)):
                radius = 1
                if abs(event.xdata - self.dataX[i]) < radius and abs(event.ydata - self.dataY[i]) < radius:
                    top = tip='x=%f\ny=%f' % (event.xdata, event.ydata)
                    self.tooltip.SetTip(tip) 
                    self.tooltip.Enable(True)
                    collisionFound = True
                    break
        if not collisionFound:
            self.tooltip.Enable(False)



example = wxToolTipExample()
pl.show()
like image 106
idrinkpabst Avatar answered Oct 21 '22 15:10

idrinkpabst


It's an old thread, but in case anyone is looking for how to add tooltips to lines, this works:

import matplotlib.pyplot as plt
import numpy as np
import mpld3

f, ax = plt.subplots()
x1 = np.array([0,100], int)
x2 = np.array([10,110], int)
y = np.array([0,100], int)

line = ax.plot(x1, y)
mpld3.plugins.connect(f, mpld3.plugins.LineLabelTooltip(line[0], label='label 1'))

line = ax.plot(x2, y)
mpld3.plugins.connect(f, mpld3.plugins.LineLabelTooltip(line[0], label='label 2'))

mpld3.show()
like image 39
Ivan Chaer Avatar answered Oct 21 '22 15:10

Ivan Chaer


Perhaps a variation on this recipe would do what you want for points? At least it isn't restricted to wx backend.

like image 43
RuiDC Avatar answered Oct 21 '22 14:10

RuiDC