从motion_notify_event获取当前坐标轴
问题描述:
我有一个包含多个子图的图。每个子图都有不同的数据。我也希望在悬停时显示数据标签,以捕捉motion_notify_event。如何从事件中捕获当前子图轴?从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()