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()
import matplotlib.pyplot as plt
# Need to create as global variable so our callback(on_plot_hover) can access
fig = plt.figure()
plot = fig.add_subplot(111)
# create some curves
for i in range(4):
# Giving unique ids to each data member
plot.plot(
[i*1,i*2,i*3,i*4],
gid=i)
def on_plot_hover(event):
# Iterating over each data member plotted
for curve in plot.get_lines():
# Searching which data member corresponds to current mouse position
if curve.contains(event)[0]:
print "over %s" % curve.get_gid()
fig.canvas.mpl_connect('motion_notify_event', on_plot_hover)
plt.show()
这里的其他答案似乎都没有回答这个问题。所以这里有一个代码,它使用一个散点,并在悬停在散点上时显示一个注释。
由于人们还希望将此解决方案用于线
plot
而不是散点,因此以下是plot
的相同解决方案(其工作方式略有不同)。如果有人在寻找双轴直线的解决方案,请参考How to make labels appear when hovering over a point in multiple axis?
如果有人正在寻找条形图的解决方案,请参阅this answer。
来自http://matplotlib.sourceforge.net/examples/event_handling/pick_event_demo.html:
当悬停一行而无需单击它时,此解决方案有效:
相关问题 更多 >
编程相关推荐