【发布时间】:2018-01-25 15:24:26
【问题描述】:
我有一个显示两个子图的简单代码,并让用户在记录这些点击的x,y 坐标时左键单击第二个子图。
问题是单击选择要缩放的区域和拖动子图也被识别为左键。
有没有办法区分和过滤掉这些左键?
import numpy as np
import matplotlib.pyplot as plt
def onclick(event, ax):
# Only clicks inside this axis are valid.
if event.inaxes == ax:
if event.button == 1:
print(event.xdata, event.ydata)
# Draw the click just made
ax.scatter(event.xdata, event.ydata)
ax.figure.canvas.draw()
elif event.button == 2:
# Do nothing
print("scroll click")
elif event.button == 3:
# Do nothing
print("right click")
else:
pass
fig, (ax1, ax2) = plt.subplots(1, 2)
# Plot some random scatter data
ax2.scatter(np.random.uniform(0., 10., 10), np.random.uniform(0., 10., 10))
fig.canvas.mpl_connect(
'button_press_event', lambda event: onclick(event, ax2))
plt.show()
【问题讨论】:
标签: python matplotlib