问题描述
关于matplotlib,pylab,pyplot,ipython的问题很多,所以很抱歉如果您厌倦了看到这个问题.我将尽我所能,因为我一直在研究人们的问题并查看pyplot和pylab的文档,但我仍然不确定自己做错了什么.上的代码:
There are a lot of questions about matplotlib, pylab, pyplot, ipython, so I'm sorry if you're sick of seeing this asked. I'll try to be as specific as I can, because I've been looking through people's questions and looking at documentation for pyplot and pylab, and I still am not sure what I'm doing wrong. On with the code:
目标:每0.5秒绘制一个图形,并在调用plot命令后立即更新图形.
Goal: plot a figure every .5 seconds, and update the figure as soon as the plot command is called.
我尝试对此进行编码(在ipython -pylab上运行):
My attempt at coding this follows (running on ipython -pylab):
import time
ion()
x=linspace(-1,1,51)
plot(sin(x))
for i in range(10):
plot([sin(i+j) for j in x])
#see **
print i
time.sleep(1)
print 'Done'
它可以正确绘制每条线,但是要等到退出for循环后才能绘制.我尝试通过将draw()
放在**
位置来强制重绘,但这似乎也不起作用.理想情况下,我想让它简单地添加每一行,而不是进行完整的重绘.但是,如果需要重新绘制,就可以了.
It correctly plots each line, but not until it has exited the for loop. I have tried forcing a redraw by putting draw()
where **
is, but that doesn't seem to work either. Ideally, I'd like to have it simply add each line, instead of doing a full redraw. If redrawing is required however, that's fine.
其他尝试解决的问题:
在ion()
之后,尝试将hold(True)
添加到无效.
对于踢球,尝试对**
进行踢球show()
我找到的最接近答案的答案是绘图线不会阻止执行,但是show()
没有做任何事情.
Additional attempts at solving:
just after ion()
, tried adding hold(True)
to no avail.
for kicks tried show()
for **
The closest answer I've found to what I'm trying to do was at plotting lines without blocking execution, but show()
isn't doing anything.
如果这是一个简单的要求,我深表歉意,而我正在寻找一些显而易见的东西.对于它的价值,这是在我尝试将Matlab代码从类转换为某些python供我自己使用时出现的.我一直在尝试转换的原始matlab(删除了初始化)如下:
I apologize if this is a straightforward request, and I'm looking past something so obvious. For what it's worth, this came up while I was trying to convert matlab code from class to some python for my own use. The original matlab (initializations removed) which I have been trying to convert follows:
for i=1:time
plot(u)
hold on
pause(.01)
for j=2:n-1
v(j)=u(j)-2*u(j-1)
end
v(1)= pi
u=v
end
任何帮助,即使只是查找this_method"也将是很好的,因此,我至少可以将工作范围缩小到弄清楚如何使用该方法.如果还有其他有用的信息,请告诉我.
Any help, even if it's just "look up this_method" would be excellent, so I can at least narrow my efforts to figuring out how to use that method. If there's any more information that would be useful, let me know.
推荐答案
所链接问题的第二个答案提供了答案:在每个plot()
之后调用draw()
使其立即显示;例如:
The second answer to the question you linked provides the answer: call draw()
after every plot()
to make it appear immediately; for example:
import time
ion()
x = linspace(-1,1,51)
plot(sin(x))
for i in range(10):
plot([sin(i+j) for j in x])
# make it appear immediately
draw()
time.sleep(1)
如果这不起作用,请尝试在此页面上执行以下操作: http://www.scipy.org/Cookbook/Matplotlib/Animations
If that doesn't work... try what they do on this page: http://www.scipy.org/Cookbook/Matplotlib/Animations
import time
ion()
tstart = time.time() # for profiling
x = arange(0,2*pi,0.01) # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
line.set_ydata(sin(x+i/10.0)) # update the data
draw() # redraw the canvas
print 'FPS:' , 200/(time.time()-tstart)
页面提到line.set_ydata()
功能是关键部分.
The page mentions that the line.set_ydata()
function is the key part.
这篇关于matplotlib.pyplot/pylab在使用ipython -pylab进行isinteractive()时不更新图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!