活动处理和采摘不适合Spyder

发布于 2025-02-07 21:22:47 字数 1198 浏览 1 评论 0 原文

当我在 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

When I run the following script in a IPython (IPython 8.2.0) console opened from Anaconda powershell I am allowed to pick points from the figure.
Then, the script continues only once I close the figure, as it should be.

However, when I run the same script on Spyder this won't happen: the script won't allow me to pick any point from the figure but it continues straight to the end, thus always resulting in printing tin = 0 and tout = 0 and returning the IPython prompt.

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')

I am running the following version of Spyder:

  • Spyder version: 5.1.5 None
  • Python version: 3.9.12 64-bit
  • Qt version: 5.9.7
  • PyQt5 version: 5.9.2
  • Operating System: Windows 10

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2025-02-14 21:22:47

有两种方法可以实现这一目标:

  1. 您可以将您的 plt.show()调用

      plt.show(block = true)
     

    但是,关闭图后,您需要按 ctrl + c 在控制台中以将提示重新放在上面。

  2. 您可以在此

There are two ways to achieve this:

  1. You can change your plt.show() call to

    plt.show(block=True)
    

    However, after closing the figure, you need to press Ctrl + C in the console to get the prompt back on it.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文