gpt4 book ai didi

python - 在 iPython 中,在 matplotlib.pyplot.show() 之后保持 Python Shell 可用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:52 24 4
gpt4 key购买 nike

以下 Python 脚本绘制(函数 x^2)并显示它:

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,10,11)

y=x**2

plt.plot(x,y)
plt.show()

一切都很好,但我注意到 Python shell 在运行此脚本后不会保持“可用”状态。具体来说(见下图),光标在最后一个命令提示符(“>>>”)下方闪烁,我键入的任何内容都不会出现在命令提示符旁边。只有关闭图形后,我才能再次输入命令提示符。

有人知道如何同时保留绘图和命令提示符吗? (我在 Linux 上运行 iPython;我在 Windows 上使用 Enthought Python Distribution 运行过类似的脚本,但没有遇到此类问题)。

Python shell after running the script above

附言我已经尝试将 plt.ion() 添加到脚本中,尽管它确实使命令行可用,但它会产生一个空白图(见下文)。在 shell 中输入“%run plot_test.py”会出现“SyntaxError: invalid syntax”,并突出显示“%”。

Python shell after running the script above with plt.ion()

最佳答案

尝试使用ion作为

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(0,10,11)

y=x**2

plt.ion() # here
plt.plot(x,y) # now it shows plot window
plt.draw()

plt.plot(x,y*10) # will be updated
plt.draw()
plt.plot(x,y*100) # will be updated
plt.draw()

关于python - 在 iPython 中,在 matplotlib.pyplot.show() 之后保持 Python Shell 可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23380576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com