且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Matplotlib 动画:绘制不同颜色的线条

更新时间:2023-01-27 09:10:12

这是@MrT的答案在使用FuncAnimation时的样子.优点是你不需要关心自动缩放;这是即时自动完成的.

Here is how @MrT's answer would look like using FuncAnimation. The advantage is that you do not need to care about autoscaling; that is done automatically on the fly.

import matplotlib.pyplot as plt
import matplotlib.animation as anim
import mpl_toolkits.mplot3d.axes3d as p3

fig = plt.figure()
ax = fig.gca(projection='3d')

x = [1, 3, 8, 11, 17]
y = [7, 2, -5, 3, 5]
z = [5, 7, 9, 13, 18]

#colour map
colors = ["green", "blue", "red", "orange"]

def init():
    ax.clear()

def update(i): 
    newsegm, = ax.plot([x[i], x[i + 1]], [y[i], y[i + 1]], [z[i], z[i + 1]], colors[i])

ani = anim.FuncAnimation(fig, update, init_func=init, 
                         frames = range(len(x)-1), interval = 300, repeat=True)
plt.show()