gpt4 book ai didi

matplotlib - matplotlib 中的点和线工具提示?

转载 作者:行者123 更新时间:2023-12-04 02:13:36 28 4
gpt4 key购买 nike

我有一个带有一些线段( LineCollection )和一些点的图表。这些线和点有一些与它们相关的值,但没有绘制出来。我希望能够添加鼠标悬停工具提示或其他方法来轻松找到点和线的关联值。这对于点或线段是否可行?

最佳答案

对于积分,我找到了一种方法,但是您必须使用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()

关于matplotlib - matplotlib 中的点和线工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4453143/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com