缓慢而流畅的绘制线条python matplotlib

时间:2014-12-09 17:58:20

标签: 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 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。

2 个答案:

答案 0 :(得分:2)

您可以将绘图程序缩小到:

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个变量即可使用
  • 你不想在动画之外设置xy,而是在里面设置要开发的情节(在你的情况下,你有完整的x和{{1设置,以便动画只绘制整个图形)
  • 您需要将可转换的内容传递给yanimate);这是i - FuncAnimation的第三个输入。
  • 设置np.arange(0,len(a)-1)在一次运行后停止并避免“关闭”曲线
  • 我增加了repeat=False以使情节变得更慢

答案 1 :(得分:0)

你需要在你的动画函数中使用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()

对于每个动画看起来不同的情节,i必须索引在每次迭代时看起来不同的东西。尽管在构建它们的迭代中逐渐附加destydestx,但最终结果不是这个渐进式构建而是单个事物,因此您需要保存此构建的中介。我是使用plot_me_xy完成此操作的。

我写了这个答案,对OP的代码进行了很少的修改,以便明确错误的位置。总的来说,@ Schorsch所做的改变导致整体更清洁(例如,没有全局)。