matplotlib 点线动画

    技术2024-04-15  70

    matplotlib 点线动画

    作者的Github [ 主写(Web Development(HTML, PHP, CSS, JS), node.js, Ruby(Sketchup API), Python(Tkinter, Django, Matplotlib), Batch Script) ]Github开源群,代码交流,分享有意思的项目若二维码过期,请扫描群主二维码,或者添加微信号 mark60213

    Matplotlib 一点一线动画


    Python 3.7.3Matplotlib 3.1.1 如题,直接上代码了 from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation def vector_animate(fig, left_x, left_y, right_x, right_y, color, interval, line=True, dot=True): def update(frame): if line: if frame != 0: plt.plot([x_res[frame - 1], x_res[frame]], [y_res[frame - 1], y_res[frame]], color=color) p.set_data(x_res[frame], y_res[frame]) return p, def init(): p.set_data(x_res[0], y_res[0]) return p, x_res = [] y_res = [] for i in range(len(left_x)): x_res.append(left_x[i] + ((right_x[i] - left_x[i]) * float(i / (len(left_x) - 1)))) y_res.append(left_y[i] + ((right_y[i] - left_y[i]) * float(i / (len(left_x) - 1)))) if dot: p, = plt.plot(x_res[0], y_res[0], "o", color=color) else: p, = plt.plot(x_res[0], y_res[0], color=color) return FuncAnimation(fig=fig, func=update, init_func=init, frames=[x for x in range(len(left_x))], interval=interval, repeat=False) fig = plt.figure() x_on_left = [0, 0, 0, 0, 0, 0] # 左点x y_on_left = [0, 2, 4, 6, 8, 10] # 左点y x_on_right = [10, 10, 10, 10, 10, 10] # 右点x y_on_right = [0, 2, 4, 6, 8, 10] # 右点y plt.xlim(0, 10) plt.ylim(0, 10) va = vector_animate(fig, x_on_left, y_on_left, x_on_right, y_on_right, "k", 400) # 400 是速度设置,数值越大,速度越慢 plt.show()
    做出来的是一点一线运动 vector_animate的参数line和dot是boolean值,若将dot设置为False, 只显示线;若将line设置为False, 只显示点。 注意下xlim和ylim必须设置,不然动画是乱的
    Processed: 0.010, SQL: 9