Matplotlib ion()函数无法交互 [英] Matplotlib ion() function fails to be interactive

查看:108
本文介绍了Matplotlib ion()函数无法交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Matplotlib的交互功能有疑问.我运行了以下程序,并收到了一个冻结的空白图形窗口.

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

如果我删除了'plt.ion()'语句,那么它就可以正常工作.我使用IDLE,并且在Python 3.2.2中安装了Matplotlib版本1.2.x软件包.

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.

似乎通过plt.ion()启用交互模式后,需要暂时暂停pyplot才能通过plt.pause(0.0001)更新/重绘自身.这是我所做的,并且有效!

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)

如果您在IDLE控制台中尝试过此操作,请注意,到目前为止,所有内容都已显示,但图形窗口冻结且无法退出.要解冻它,请键入以下最后一条语句

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)

现在可以关闭窗口了.

这篇关于Matplotlib ion()函数无法交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆