我对编程的经验有限。我正在使用matplotlib在python中编写一个小应用程序,这将允许我数字化用于构建多边形的图形上的段。我正在使用ginput()在图形上添加点并绘制线,但我需要实现一个循环,以便有可能根据观察到的特征绘制可变数量的段。我正在使用Jupyter笔记本。应用程序到目前为止允许我在图上添加段,但我无法退出while循环。经过一些研究,我看到了使用键盘中断的可能性,但似乎没有用。我的代码如下:
t = np.arange(10)
plt.plot(t, np.sin(t)) # just some data on the plot
try:
while True: # loop to allow me to add multiple lines and plot them
xy = plt.ginput(2, show_clicks = True) # set to two as I want just two points and the possibility to draw the line progressively
x = [p[0] for p in xy]
y = [p[1] for p in xy]
line = plt.plot(x,y) #plot the line
plt.draw()
plt.show()
print ('points: ', xy)
except KeyboardInterrupt: # my attempt to exit the while loop but in reality the figure just move to the background and a new figure is generated after 30 secondsf
pass
感谢您的帮助和问候
的Fabio