<h2>时间问题</h2>
<p>您遇到的问题是由于pick_event函数的调用方式造成的,matplotlib测试每个艺术家,如果它是立即选择的,则调用handler函数。测试顺序取决于定义孔和桩的顺序(可以通过更改桩和孔的定义顺序来验证)。在</p>
<p>因此,避免这个问题的一个好方法是使用matplotlib提供的计时器。在第一次出现拾取事件时,您只需将艺术家添加到队列中,然后每隔<em>x</em>毫秒处理一次新数据。在</p>
<p>下面是这种实现的一个示例:</p>
<pre><code>import matplotlib.pyplot as plt
import sys
class peg_tester():
def __init__(self):
self.fig = plt.figure(figsize=(3,1))
self.ax = self.fig.add_axes([0,0,1,1])
self.ax.set_xlim([-0.5,2.5])
self.ax.set_ylim([-0.25,0.25])
self.ax.text(-0.4, 0.15,
'One click on the hole, and I get 2 events not 1',
fontsize=8)
self.holes = self.ax.scatter([1], [0], color='black', picker=0)
self.pegs = self.ax.scatter([0], [0], s=100, facecolor='#dd8800',
edgecolor='black', picker=0)
self.fig.canvas.mpl_connect('pick_event', self.handler)
# Creating a timer with a interval of 100 ms
self.timer=self.fig.canvas.new_timer(interval=100)
# Queue of pegs and holes that have been picked and waiting to be processed
self.list_pegs_holes=[]
self.timer.add_callback(self.update_pos,self.list_pegs_holes)
self.timer.start()
plt.show()
# Add the artist to the queue of artists awaiting to be processed
def handler(self, event):
self.list_pegs_holes.append(event.artist)
# Management of the queue
def update_pos(self,list_pegs_holes):
while len(list_pegs_holes)!=0:
artist=list_pegs_holes.pop(0)
if artist is self.holes:
# If I get a hole event, then move a peg (to that hole) ...
# but then I get a peg event also with no extra clicks!
offs = self.pegs.get_offsets()
offs[0,:] = [1,0] # Moves left peg to the middle
self.pegs.set_offsets(offs)
self.fig.canvas.draw()
print 'picked a hole, moving left peg to center'
elif artist is self.pegs:
print 'picked a peg'
sys.stdout.flush() # Necessary when in ipython qtconsole
if __name__ == "__main__":
pt = peg_tester()
</code></pre>
<p>大部分代码都是您提供的,我只是添加了计时器实现。在</p>
<h2>非最佳(过时)</h2>
<p>可以通过为艺术家定义特定的选取器来修复此行为。但是你不能在同一地点选择几个项目。在</p>
<p>请看下面这个例子,该示例解决了不应拾取桩的部分:</p>
<p>-将钉的定义替换为:</p>
^{pr2}$
<p>-添加函数pegs\u picker:</p>
<pre><code>def pegs_picker(figure,pegs,event):
# Check that the pointer is not on holes
if figure.holes.contains(event)[0]:
return False, dict()
else:
return True, dict()
</code></pre>
<p>有了那个钉子,只有当它们没有和洞重叠的时候才是可挑选的。在</p>
<p>我认为这可能是获得你想要的行为的方式,但是由于我不知道它到底是什么,我无法为您提供更精细的选择函数。在</p>