我有一个包含多个子图的图。每个子图都有不同的数据。我也在捕捉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()
答案 0 :(得分:1)
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()