matplotlib 中的点和线工具提示?

发布于 2024-10-07 23:14:04 字数 238 浏览 1 评论 0原文

我有一个带有一些线段的图表(LineCollection)和一些点。这些线和点有一些与之相关的值,但未用图表表示。我希望能够添加鼠标悬停工具提示或其他方法来轻松查找点和线的关联值。对于点或线段来说这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

待天淡蓝洁白时 2024-10-14 23:14:04

对于积分,我已经找到办法了,但是你必须使用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()

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()
梨涡 2024-10-14 23:14:04

这是一个旧线程,但如果有人正在寻找如何向行添加工具提示,这有效:

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

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()
伏妖词 2024-10-14 23:14:04

也许这个食谱的变体可以满足你想要的积分?至少不限于wx后端。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文