缓慢而平滑地绘制线条python matplotlib

缓慢而平滑地绘制线条python matplotlib,python,numpy,matplotlib,lines,Python,Numpy,Matplotlib,Lines,我正在使用matplotlib绘制平滑线等图形。为我画画不是问题,但我在动画方面有问题 import numpy as np import random as random from matplotlib import pyplot as plt from matplotlib import animation 所以,我有一个数组,比如: a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45] 我需要点对点画出平滑的曲线。 现在我有这个字符串: for

我正在使用matplotlib绘制平滑线等图形。为我画画不是问题,但我在动画方面有问题

import numpy as np
import random as random
from matplotlib import pyplot as plt
from matplotlib import animation
所以,我有一个数组,比如:

a = [0,1,2,4,5,8,9,12,14,18,22,17,30,37,29,45]
我需要点对点画出平滑的曲线。 现在我有这个字符串:

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))
这使我能够在没有问题和动画的情况下绘制线条:

完整代码:

# 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()
允许我只画它,但我如何才能画它缓慢而平滑的点对点


我使用的是python 2.7,centos 7。

您可以将绘图例程精简为:

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()
注意事项:

您不需要全局变量就可以工作 您不希望在动画外部设置x和y,而是在内部设置以进行绘图。在您的案例中,您已设置了完整的x和y,以便动画仅绘制整个图形 您需要传递一个interable来设置i的动画;这是FuncAnimation-np.arange0,lena-1的第三个输入。 设置repeat=False可在一次运行后停止,并避免“关闭”曲线 我增加了间隔,使情节发展得更慢 你需要在你的动画功能中使用i,否则它只是一遍又一遍地绘制相同的东西,这就是你所看到的

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)
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)
plot_me_x = []
plot_me_y = []
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))
    plot_me_x.append(np.array(destx))   # keep a copy of the intermediaries to plot later
    plot_me_y.append(np.array(desty))
# 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=plot_me_x[i]
    y=plot_me_y[i]
    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=len(a)-1, interval=20, blit=True)
plt.show()
为了使情节在每个动画中看起来不同,我必须索引在每个迭代中看起来不同的内容。尽管desty和destx在构建它们的迭代中逐渐追加,但最终的结果不是这个逐步构建,而是一件事,因此您需要保存这个构建的中介。我是用plot_me_x和y做的


我写了这个答案,对OP的代码做了最小的修改,以明确错误所在。总的来说,@Schorsch所做的更改导致了一种更为干净的方法,例如,不使用全局变量。

感谢您提及matplotlib.animation API。谢谢,非常有用的答案!