我有一个包含多个子图的图。每个子图都有不同的数据。我还捕获了 motion_notify_event,目的是在悬停时显示数据标签。如何从事件中捕获当前的子图轴?
import random
import matplotlib.pyplot as plt
def on_move(event):
if event.inaxes is not None:
# Which subplot am I hovering over?
pass
fig = plt.figure()
ax1 = plt.subplot2grid((4, 4), (0, 0), colspan=3)
ax2 = plt.subplot2grid((4, 4), (1, 0), colspan=3, rowspan=3)
ax3 = plt.subplot2grid((4, 4), (1, 3), rowspan=3)
ax1.plot(random.sample(xrange(100), 10), random.sample(xrange(100), 10), 'ro')
ax2.plot(random.sample(xrange(100), 10), random.sample(xrange(100), 10), 'bo')
ax3.plot(random.sample(xrange(100), 10), random.sample(xrange(100), 10), 'go')
on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.show()
event
是 LocationEvent 的实例, 因此 event.inaxes
已经包含子图轴。只需尝试:
def on_move(event):
if event.inaxes is not None:
x = event.xdata
y = event.ydata
event.inaxes.plot(x,y,'ro')
event.canvas.draw()
我是一名优秀的程序员,十分优秀!