简体   繁体   English

Matplotlib 图 window 在交互模式下消失

[英]Matplotlib figure window disappears when in interactive mode

I need to use interactive mode when plotting with matplotlib (it should be a script, not python or ipython console).使用 matplotlib 绘图时,我需要使用交互模式(它应该是一个脚本,而不是 python 或 ipython 控制台)。 But setting plt.ion() causes a strange bug (?).但是设置plt.ion()会导致一个奇怪的错误(?)。 When I try to plot my figure (I don't think it really matters, what I do exactly, because in non-interactive mode it works perfectly fine) I don't see it - I get a blank grey window for split-second, which momentarily disappears and the programme exits.当我尝试 plot 我的身材时(我认为这并不重要,我究竟做了什么,因为在非交互模式下它工作得非常好)我没有看到它 - 我得到一个空白的灰色 window 瞬间,它暂时消失并且程序退出。 If I explicitly add plt.draw() (and plt.pause(1) to see the result), I see that the figure appears as expected.如果我明确添加plt.draw() (和plt.pause(1)以查看结果),我会看到该图按预期显示。 If I do the same after modifications I want to do to the figure when it is visible, the figure changes.如果我在修改后做同样的事情,我想在图形可见时对它做同样的事情,图形就会改变。 But the window still disappears after the pause is over.但是window在暂停结束后仍然消失。

I run it in Spyder with Qt4Agg as a backend under Ubuntu.我在 Ubuntu 下使用 Qt4Agg 作为后端在 Spyder 中运行它。 Tried running the script from terminal as python my_script.py , the result is identical.尝试从终端运行脚本python my_script.py ,结果是相同的。

What could be the problem?可能是什么问题呢? How do I stop the figure from disappearing when in interactive mode?在交互模式下如何阻止图形消失?

UPDATE更新

Working example:工作示例:

import matplotlib.pyplot as plt
import numpy as np

plt.ion()

x = np.linspace(1, 10)
y = np.sin(x)
plt.plot(x, y)
plt.draw()
plt.pause(1)

If I run this code I see the sine plot for 1 second, then the window disappears.如果我运行此代码,我会看到正弦 plot 1 秒,然后 window 消失。

UPDATE 2更新 2

I found a solution here: https://stackoverflow.com/a/10724654/1304161 If I set the run options in Spyder correctly, it works perfectly fine.我在这里找到了一个解决方案: https://stackoverflow.com/a/10724654/1304161如果我在 Spyder 中正确设置了运行选项,它工作得非常好。 Although running it in gnome-terminal doesn't work, I don't really need it.虽然在 gnome-terminal 中运行它不起作用,但我真的不需要它。 Hopefully, there won't be a problem with this when it becomes a part of a GUI app...希望当它成为 GUI 应用程序的一部分时不会有问题......

I found a solution here: https://stackoverflow.com/a/10724654/1304161我在这里找到了一个解决方案: https : //stackoverflow.com/a/10724654/1304161

If I set the run options in Spyder correctly, it works perfectly fine.如果我在 Spyder 中正确设置了运行选项,它就可以正常工作。 Although running it in gnome-terminal doesn't work, I don't really need it.尽管在 gnome-terminal 中运行它不起作用,但我真的不需要它。 Hopefully, there won't be a problem with this when it becomes a part of a GUI app.希望当它成为 GUI 应用程序的一部分时不会有问题。 I will be back, if it will be a problem then :)我会回来的,如果这有问题,那么:)

You can make it work by adding these two lines at the end:您可以通过在最后添加这两行来使其工作:

plt.ioff()
plt.show()

So this program works fine:所以这个程序运行良好:

import matplotlib.pyplot as plt
import numpy as np

plt.ion()

x = np.linspace(1, 10)
y = np.sin(x)
plt.plot(x, y)
plt.draw()
plt.ioff()
plt.show()

To display multiple figures at different times, you can use code.interact(local=locals()) , after plt.show() to pause the Python interpreter until a Ctrl-Z is pressed in the Python Shell:要在不同时间显示多个图形,您可以在plt.show()之后使用code.interact(local=locals()) ) 来暂停 Python 解释器,直到在 Python ZEA63CEC067C34CE14A36 中按下 Ctrl-Z 为止

import code
import matplotlib.pyplot as plt
import numpy as np

# Start pyplot's "interactive mode" which lets you run plt.show multiple times
plt.ion()

x = np.linspace(1, 10)
y = np.sin(x)

# PLOT #1 - displayed
plt.plot(x, y)
plt.draw()
plt.show()

# Wait for figure to be closed AND Ctrl-Z pressed in Python Shell
code.interact(local=locals())

print("Some more code can go here...")

# PLOT #2 - displayed
plt.plot(x, y)
plt.show()

# Wait for figure to be closed AND Ctrl-Z pressed in Python Shell
code.interact(local=locals())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM