【问题标题】:Updating line in 3D plot Animation更新 3D 绘图动画中的线
【发布时间】:2020-06-02 15:28:01
【问题描述】:

我正在尝试在 3D 动画中绘制一条线。我无法擦除我在 update_line 中创建的图。问题是我总是添加新行。我看到的最佳解决方案是在函数之外创建线条并更新这些线条的数据,但我似乎无法找到 3D 绘图的方法

第一次发帖,只用了几个小时的python。对画布一无所知。请保持简单

import matplotlib; matplotlib.use("TkAgg")
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
dotinline = 101
nframe = 7
fig = plt.figure(figsize=(16, 8))
ax = p3.Axes3D(fig)
ax.view_init(0, 90)


def init():
    ax.plot([-10, 10], [0, 0], [0, 0], "k")
    ax.plot([0, 0], [-10, 10], [0, 0], "k")
    ax.plot([0, 0], [0, 0], [-10, 10], "k")
    ax.plot([], [], [], "b")


def update_lines(index):
    seg = []
    x = np.linspace(-10, 10, dotinline)
    power = x**(-(nframe-1)/2+index)
    for i in range(len(x)-1):
        a = [x[i+1], x[i]]
        b = [0, 0]
        c = [power[i+1], power[i]]
        seg.append([a, b, c])
    for data in seg:
        ax.plot3D(data[0], data[1], data[2], "-b")


ax.set_xlim3d([-10, 10])
ax.set_xlabel('X')
ax.set_ylim3d([-10, 10])
ax.set_ylabel('Y')
ax.set_zlim3d([-10, 10])
ax.set_zlabel('Z')
ax.set_title('3D Test')


line_ani = animation.FuncAnimation(fig, update_lines, init_func=init, frames=nframe, interval=2000, blit=False)

plt.show()

【问题讨论】:

    标签: python python-3.x matplotlib animation plot


    【解决方案1】:

    检查此代码:

    import matplotlib; matplotlib.use("TkAgg")
    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as p3
    import matplotlib.animation as animation
    dotinline = 101
    nframe = 7
    fig = plt.figure(figsize=(16, 8))
    ax = p3.Axes3D(fig)
    ax.view_init(0, 90)
    
    
    def init():
        ax.plot([-10, 10], [0, 0], [0, 0], "k")
        ax.plot([0, 0], [-10, 10], [0, 0], "k")
        ax.plot([0, 0], [0, 0], [-10, 10], "k")
        ax.plot([], [], [], "b")
    
    
    def update_lines(index):
        ax.cla()
        init()
        seg = []
        x = np.linspace(-10, 10, dotinline)
        power = x**(-(nframe-1)/2+index)
        for i in range(len(x)-1):
            a = [x[i+1], x[i]]
            b = [0, 0]
            c = [power[i+1], power[i]]
            seg.append([a, b, c])
        for data in seg:
            ax.plot3D(data[0], data[1], data[2], "-b")
    
    
        ax.set_xlim3d([-10, 10])
        ax.set_xlabel('X')
        ax.set_ylim3d([-10, 10])
        ax.set_ylabel('Y')
        ax.set_zlim3d([-10, 10])
        ax.set_zlabel('Z')
        ax.set_title('3D Test')
    
    
    line_ani = animation.FuncAnimation(fig, update_lines, init_func=init, frames=nframe, interval=2000, blit=False)
    
    plt.show()
    

    要删除前面的行,只需添加ax.cla()
    但是,这也擦除了黑线轴;所以,在ax.cla() 之后,我再次运行init(),以便重新绘制轴线。
    最后,最好移动块:

    ax.set_xlim3d([-10, 10])
    ax.set_xlabel('X')
    ax.set_ylim3d([-10, 10])
    ax.set_ylabel('Y')
    ax.set_zlim3d([-10, 10])
    ax.set_zlabel('Z')
    ax.set_title('3D Test')
    

    update_lines() 函数中,以便在删除行时保留这些选项。
    使用此代码,我得到以下动画:

    【讨论】:

    • 谢谢,每次都重做所有的情节。简单,我喜欢。
    猜你喜欢
    • 2021-03-30
    • 2015-10-02
    • 2012-09-28
    • 2017-08-10
    • 2020-05-31
    • 2012-07-12
    • 2021-09-07
    • 2013-12-01
    • 1970-01-01
    相关资源
    最近更新 更多