2010-12-15 13 views
10

私はいくつかの線分(LineCollection)といくつかの点を持つグラフを持っています。これらの線と点には、グラフ化されていない値が関連付けられています。私はマウスオーバーツールチップや、ポイントとラインの関連値を簡単に見つける他の方法を追加できるようにしたいと思います。これは点または線分のいずれに対しても可能ですか?matplotlibのポイントとラインのツールチップですか?

+2

を([ここでmatplotlibのからの一例です] http://matplotlib.sourceforge.net/ examples/pylab_examples/cursor_demo.html)、これは私がGoogleで見つけたものです。 [それから別の人気のあるSOの答えがあります。](http://stackoverflow.com/a/4674445/1020470)[まあ、これも、matplotlibの例を指しています。](http://stackoverflow.com/ a/7909589/1020470) –

答えて

6

はポイントの場合、私は方法を発見した、しかし、あなたはWXのバックエンド

"""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() 
+1

より一般的には、このような機能をEnthoughtがサポートしていれば、非常にうれしいでしょう... – aestrivex

1

おそらくthis recipe上の変化は、あなたがポイントのために何をしたいだろうを使用する必要がありますか?少なくとも、それはwxバックエンドに限定されません。

2

それは古いスレッドですが、ケースには、誰もがラインにツールチップを追加する方法を探している、これは動作します:

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() 
関連する問題