问题描述
我对 Matplotlib 的交互功能有疑问.我运行以下程序并收到一个冻结的空图形窗口.
将 matplotlib.pyplot 导入为 plt将 numpy 导入为 npplt.ion()x = np.arange(0, 4*np.pi, 0.1)y = [np.sin(i) for i in x]plt.plot(x, y, 'g-', linewidth=1.5,markersize=4)plt.show()
如果我删除了 'plt.ion()' 语句,那么它工作得很好.我使用 IDLE 并且 Matplotlib 版本 1.2.x 包安装在 Python 3.2.2 中.
我希望它是交互式的,但我得到了一个不友好的非交互式窗口.有人可以阐明我所缺少的东西吗?提前致谢.
我偶然发现了这个链接 这里,它回答了我的问题.
似乎通过plt.ion()
开启交互模式后,pyplot需要暂时暂停,通过plt.pause(0.0001)更新/重绘自己
代码>.这是我所做的,并且有效!
如果您在 IDLE 控制台中尝试过,请注意,到目前为止,除了图形窗口冻结且无法退出之外,所有内容都已显示.要解冻它,请输入以下最后一条语句
>>>plt.show(块=真)现在可以关闭窗口了.
I have problem with interactive feature of Matplotlib. I ran the following program and received a freezing empty graph window.
import matplotlib.pyplot as plt
import numpy as np
plt.ion()
x = np.arange(0, 4*np.pi, 0.1)
y = [np.sin(i) for i in x]
plt.plot(x, y, 'g-', linewidth=1.5, markersize=4)
plt.show()
If I removed 'plt.ion()' statement, then it worked just fine. I use IDLE and the Matplotlib version 1.2.x package is installed in Python 3.2.2.
I expect it to be interactive, but instead I got an unfriendly non-interactive window. Can someone shed some light of what I am missing? Thank you in advance.
I bumped into this link found here, which answers my problem.
It seems that after turning on interactive mode through plt.ion()
, pyplot needs to be paused temporarily for it to update/redraw itself through plt.pause(0.0001)
. Here is what I did and it works!
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> plt.ion()
>>> x = np.arange(0, 4*np.pi, 0.1)
>>> y = [np.sin(i) for i in x]
>>> plt.plot(x, y, 'g-', linewidth=1.5, markersize=4)
>>> plt.pause(0.0001)
>>> plt.plot(x, [i**2 for i in y], 'g-', linewidth=1.5, markersize=4)
>>> plt.pause(0.0001)
>>> plt.plot(x, [i**2*i+0.25 for i in y], 'r-', linewidth=1.5, markersize=4)
>>> plt.pause(0.0001)
If you tried that in your IDLE console, notice that up to this point everything got displayed except that the graph window freezes and cannot exit. To unfreeze it type the following last statement
>>> plt.show(block=True)
Now the window can be closed.
这篇关于Matplotlib ion() 函数无法交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!