微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用Line3D对象时,为什么matplotlib 3d动画不起作用

如何解决尝试使用Line3D对象时,为什么matplotlib 3d动画不起作用

我有以下代码

import matplotlib.pyplot as plt 
import mpl_toolkits.mplot3d.axes3d as plt3d
import mpl_toolkits.mplot3d.art3d as artplt3d
import matplotlib.animation as animation

import numpy as np
import math 

def animateBeta():

    def translate(segment_,xTr_,yTr_,zTr_):
        translationMatrix = np.array([
            [1,xTr_],[0,1,yTr_],zTr_]
        ])

        return np.matmul(translationMatrix,segment_)

    def rotate(vec_,xRotate_,yRotate_,zRotate_):
        xMat = np.array([
            [1,0],math.cos(xRotate_),-math.sin(xRotate_)],math.sin(xRotate_),math.cos(xRotate_)]
        ])

        yMat = np.array([
            [math.cos(yRotate_),math.sin(yRotate_)],[-math.sin(yRotate_),math.cos(yRotate_)]
        ])

        zMat = np.array([
            [math.cos(zRotate_),-math.sin(zRotate_),[math.sin(zRotate_),math.cos(zRotate_),1]
        ])

        rotationMatrix = np.matmul(zMat,np.matmul(yMat,xMat))

        return np.matmul(rotationMatrix,vec_)

    segment = [
        [0,[1,-1]
    ]

    fig = plt.figure()
    ax = plt3d.Axes3D(fig)
    line,= ax.plot(segment[0],segment[1],zs=segment[2],color = 'b')
    artplt3d.line_2d_to_3d(line)

    print(line.__class__)

    def animate(i):
        s0 = [0,1]
        s1 = [0,-1]
        s1 = translate(rotate(translate(s1,-s0[0],-s0[1],-s0[2]),-i*(math.pi/180),-i*(math.pi/180)),s0[0],s0[1],s0[2])
        segment = np.concatenate((np.reshape(s0,(3,-1)),np.reshape(s1,-1))),axis=1)

        #data = ax.plot(segment[0],segment[2],color = 'b')
        line.set_3d_properties(zs=segment[2])
        line.set_data_3d(segment[0],segment[2])

    anim = animation.FuncAnimation(fig,animate,interval = 1)
    
    plt.show()

animateBeta()

当使用带注释的data = ax.plot(segment[0],color = 'b')行而不是后面的两行时,它可以工作(但我试图这样做,以便在顶部绘制新行时不会画出先前的行)。 / p>

如果按原样使用代码,则动画看起来很奇怪。

我有一种理论认为line_2d_to_3d不能按预期工作,但我不确定。

解决方法

好的,所以我使用的是ax.plot而不是plot3D,而且我的印象是使用ax.plot时绘制更加正确

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。