缓慢且平滑的画线python matplotlib [英] Slow and smooth drawing lines python matplotlib
本文介绍了缓慢且平滑的画线python matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用matplotlib绘制诸如平滑线之类的图形.帮我画画没问题,但是动画方面有问题.
I'm using matplotlib for drawing graphs such as smooth lines. it is not problem to draw for me, but I have problems with animations.
import numpy as np
import random as random
from matplotlib import pyplot as plt
from matplotlib import animation
所以,我有数组,例如:
So, i have array, such as:
a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45]
我需要点对点绘制它平滑. 现在我有了这个字符串:
And I need to draw it smooth point-to-point. Now I have this strings:
for i in range(len(a)-1):
desty = np.append(desty,np.linspace(a[i],a[i+1],n))
destx = np.append(destx,np.linspace(i,i+1,n))
这使我可以毫无问题地画线(和动画:))
that allows me to draw lines without problems (and animations :))
完整代码:
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 50), ylim=(0, 50))
line, = ax.plot([], [], lw=2)
global x
global y
global n
global a
n=5
a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45]
global desty,destx
desty = np.linspace(0,0,n)
destx = np.linspace(0,0,n)
for i in range(len(a)-1):
desty = np.append(desty,np.linspace(a[i],a[i+1],n))
destx = np.append(destx,np.linspace(i,i+1,n))
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
def animate(i):
global destx
global desty
x=destx
y=desty
line.set_data(x, y)
return line,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
plt.show()
仅允许我绘制它,但如何才能点对点缓慢,平滑地绘制它?
Allows me to draw it only, but how can i draw it slow and smooth point to point?
我正在使用python 2.7,centos 7.
I'm using python 2.7, centos 7.
推荐答案
您可以将绘图例程简化为:
You can slim your plotting routine down to:
import numpy as np
import random as random
from matplotlib import pyplot as plt
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 50), ylim=(0, 50))
line, = ax.plot([], [], lw=2)
n=5
a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45]
x = []
y = []
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
def animate(i):
x.append(np.linspace(i,i+1,n))
y.append(np.linspace(a[i],a[i+1],n))
line.set_data(x,y)
return line,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, np.arange(0,len(a)-1) ,init_func=init,
interval=200, blit=True, repeat=False)
plt.show()
注意事项:
- 您不需要
global
变量即可运行 - 您不想在动画外部设置
x
和y
,而是在内部进行开发(在您的情况下,您已经设置了完整的x
和y
以便动画只会绘制整个图) - 您需要将变量传递给
animate
(i
);这是FuncAnimation
-np.arange(0,len(a)-1)
的第三个输入. - 将
repeat=False
设置为在一次运行后停止并避免闭合"曲线 - 我增加了
interval
以使绘图进度变慢
- You don't require
global
variables for this to work - You don't want to set up
x
andy
outside of the animation but inside for the plot to develop (in your case you had the completex
andy
set up so that the animation would only plot the entire graph) - You need to pass an interable to
animate
(thei
); this is the third input toFuncAnimation
-np.arange(0,len(a)-1)
. - set
repeat=False
to stop after one run and to avoid 'closing' the curve - I increased
interval
to make the plot develop slower
这篇关于缓慢且平滑的画线python matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文