我正在尝试建立一个互动情节.如果在轴内单击并在随机位置绘制圆圈,则应该清除该图形.代码如下:
import matplotlib.pyplot as plt
import random
def draw_circle(event):
if event.inaxes:
print(event.xdata, event.ydata)
plt.cla()
a = random.randint(0,100)
b = random.randint(0,100)
s, = plt.plot(a,b,'o', ms=100, color="blue",visible=True )
plt.show()
fig = plt.figure()
ax = plt.subplot(111)
s, = plt.plot(1,2,'o', ms=100, color="blue",visible=True )
plt.connect("button_press_event", draw_circle)
plt.show()
Run Code Online (Sandbox Code Playgroud)
点击42次后,程序中断,我得到以下回溯:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 286, in button_press_event
FigureCanvasBase.button_press_event(self, x, y, num, guiEvent=event)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1632, in button_press_event
self.callbacks.process(s, mouseevent)
File …Run Code Online (Sandbox Code Playgroud) 我想到的用例如下:我想使用ThreadPoolExecutor启动一系列作业,然后当作业完成时,我想向队列添加一个新作业。我还想知道下一项工作何时完成并重复上述过程。当我有机会观察到预定数量的结果后,我想正确地终止一切。例如,考虑以下代码,其中run_con方法中注释掉的位显示了我想要实现的目标。
import numpy as np
import time
from concurrent import futures
MAX_WORKERS = 20
seed = 1234
np.random.seed(seed)
MSG = "Wall time: {:.2f}s"
def expensive_function(x):
time.sleep(x)
return x
def run_con(func, ts):
t0 = time.time()
workers = min(MAX_WORKERS, len(ts))
with futures.ThreadPoolExecutor(workers) as executor:
jobs = []
for t in ts:
jobs.append(executor.submit(func, t))
done = futures.as_completed(jobs)
for future in done:
print("Job complete: ", future.result())
# depending on some condition, add new job to jobs, e.g.
# jobs.append(func, np.random.random()) …Run Code Online (Sandbox Code Playgroud)