活动处理和采摘不适合Spyder
当我在 ipython(ipython 8.2.0)中运行以下脚本时,
从 anaconda powershell
打开的控制台我被允许从图中摘下点。
然后,脚本只有在我关闭数字后才继续。
但是,当我在 spyder
上运行相同的脚本时,这不会发生:该脚本将不允许我从图中选择任何点,但它一直持续到末尾,因此始终导致打印 tin = 0
和 tout = 0
并返回 ipython
提示。
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 2)
ax[0, 0].plot(np.random.rand(10))
ax[0, 1].plot(np.random.rand(10))
ax[1, 0].plot(np.random.rand(10))
ax[1, 1].plot(np.random.rand(10))
def onclick(event):
if onclick.index == 0:
onclick.tin = np.round(event.xdata, 3)
else:
onclick.tout = np.round(event.xdata, 3)
print('tin = ', onclick.tin, 'tout =', onclick.tout, end='\r')
onclick.index = (onclick.index+1) % 2
onclick.index = 0
onclick.tin = 0
onclick.tout = 0
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
fig.canvas.mpl_disconnect(cid)
tin = onclick.tin
tout = onclick.tout
print('\n\n tin = ', tin, 'tout =', tout, end='\r')
我正在运行以下版本的Spyder:
- Spyder版本:5.1.5无
- Python版本:3.9.12 64位
- QT版本:5.9.7
- Pyqt5版本:5.9.2
- 操作系统:Windows 10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种方法可以实现这一目标:
您可以将您的
plt.show()
调用但是,关闭图后,您需要按 ctrl + c 在控制台中以将提示重新放在上面。
您可以在此
There are two ways to achieve this:
You can change your
plt.show()
call toHowever, after closing the figure, you need to press Ctrl + C in the console to get the prompt back on it.
You can add the code in this answer just below
plt.show()
. The advantage of that is that you'll get the prompt back after closing the figure.