matplotlib-从3D颤动图中删除行

发布于 2025-01-30 13:42:38 字数 2040 浏览 3 评论 0原文

我想以Matplotlib和Quiver函数为图形地表示三维参考框架的时间趋势。在一个事件中,我通过3x3旋转矩阵的定义模拟了代表参考框架的数据。每次发生事件时,都应删除先前的参考框架并显示新的参考框架。这是我在Python中的代码:

import random as rnd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


def timerTick_Event(i):
   #%% DATA SIMULATION
   temp1 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
   temp1 = temp1 / np.linalg.norm(temp1)
   
   v2 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
   v2 = v2 / np.linalg.norm(v2)
   
   v3 = np.cross(temp1, v2)
   v3 = v3 / np.linalg.norm(v3)
   
   v1 = np.cross(v2, v3)
   v1 = v1 / np.linalg.norm(v1)    
       
   mat = np.column_stack((v1, v2, v3))
   
   
   #%% DATA REPRESENTATION
   f = plt.gcf()
   ax = f.gca()    


   #INSERT DELETE FUNCTION HERE


   u = ax.quiver(0, 0, 0, mat[0,0], mat[0,1], mat[0,2], color="r")
   v = ax.quiver(0, 0, 0, mat[1,0], mat[1,1], mat[1,2], color="g")
   w = ax.quiver(0, 0, 0, mat[2,0], mat[2,1], mat[2,2], color="b")
    
   plt.show()


f1 = plt.figure(1)
ax = f1.add_subplot(projection='3d')

u = ax.quiver(0, 0, 0, 0, 0, 0, color="r")
v = ax.quiver(0, 0, 0, 0, 0, 0, color="g")
w = ax.quiver(0, 0, 0, 0, 0, 0, color="b")
# set empty line plots with colors associate to the
# quivers. Doing so we can show a legend.
ax.plot([], [], [], color="r", label="X")
ax.plot([], [], [], color="g", label="Y")
ax.plot([], [], [], color="b", label="Z")

ax.set_xlim(-1.1, 1.1); ax.set_ylim(-1.1, 1.1); ax.set_zlim(-1.1, 1.1)
ax.set_xlabel("X_AXIS"); ax.set_ylabel("Y_AXIS"); ax.set_zlabel("Z_AXIS")
ax.legend();



timerTickInterval = 1000
ani = FuncAnimation(f1, timerTick_Event, interval=timerTickInterval)
plt.show()

此代码工作正常,除了缺少的删除函数。在2D图中,我使用此代码删除注释

for child in ax.get_children():
        if isinstance(child, mTxt.Annotation):
            child.remove()

,并将此命令删除第一个绘制行的

ax.lines.pop(0)

建议吗?先感谢您!

I would like to graphically represent the time trend of a three-dimensional reference frame using Matplotlib and the quiver function. In an event, I simulated the data representing the reference frame through the definition of a 3x3 rotation matrix. Each time the event occurs, it should delete the previous reference frame and display the new one. Here is my code in Python:

import random as rnd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation


def timerTick_Event(i):
   #%% DATA SIMULATION
   temp1 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
   temp1 = temp1 / np.linalg.norm(temp1)
   
   v2 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
   v2 = v2 / np.linalg.norm(v2)
   
   v3 = np.cross(temp1, v2)
   v3 = v3 / np.linalg.norm(v3)
   
   v1 = np.cross(v2, v3)
   v1 = v1 / np.linalg.norm(v1)    
       
   mat = np.column_stack((v1, v2, v3))
   
   
   #%% DATA REPRESENTATION
   f = plt.gcf()
   ax = f.gca()    


   #INSERT DELETE FUNCTION HERE


   u = ax.quiver(0, 0, 0, mat[0,0], mat[0,1], mat[0,2], color="r")
   v = ax.quiver(0, 0, 0, mat[1,0], mat[1,1], mat[1,2], color="g")
   w = ax.quiver(0, 0, 0, mat[2,0], mat[2,1], mat[2,2], color="b")
    
   plt.show()


f1 = plt.figure(1)
ax = f1.add_subplot(projection='3d')

u = ax.quiver(0, 0, 0, 0, 0, 0, color="r")
v = ax.quiver(0, 0, 0, 0, 0, 0, color="g")
w = ax.quiver(0, 0, 0, 0, 0, 0, color="b")
# set empty line plots with colors associate to the
# quivers. Doing so we can show a legend.
ax.plot([], [], [], color="r", label="X")
ax.plot([], [], [], color="g", label="Y")
ax.plot([], [], [], color="b", label="Z")

ax.set_xlim(-1.1, 1.1); ax.set_ylim(-1.1, 1.1); ax.set_zlim(-1.1, 1.1)
ax.set_xlabel("X_AXIS"); ax.set_ylabel("Y_AXIS"); ax.set_zlabel("Z_AXIS")
ax.legend();



timerTickInterval = 1000
ani = FuncAnimation(f1, timerTick_Event, interval=timerTickInterval)
plt.show()

This code works fine except for the missing delete function. In a 2d plot, I used this code to delete annotations

for child in ax.get_children():
        if isinstance(child, mTxt.Annotation):
            child.remove()

and this command to delete the first plotted line

ax.lines.pop(0)

Any suggestion? Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你的手,一向走下去 2025-02-06 13:42:38

如果您跟踪Quiver艺术家,则可以使用它们来删除先前绘制的颤音:

import random as rnd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

quivers_artist = []

def timerTick_Event(i):
    global quivers_artist
    #%% DATA SIMULATION
    temp1 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
    temp1 = temp1 / np.linalg.norm(temp1)
    v2 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
    v2 = v2 / np.linalg.norm(v2)
    v3 = np.cross(temp1, v2)
    v3 = v3 / np.linalg.norm(v3)
    v1 = np.cross(v2, v3)
    v1 = v1 / np.linalg.norm(v1)    
    mat = np.column_stack((v1, v2, v3))
    #%% DATA REPRESENTATION
    f = plt.gcf()
    ax = f.gca()    
    #INSERT DELETE FUNCTION HERE
    if len(quivers_artist) > 0:
        for q in quivers_artist:
            q.remove()
        quivers_artist.clear()
    quivers_artist.append(ax.quiver(0, 0, 0, mat[0,0], mat[0,1], mat[0,2], color="r"))
    quivers_artist.append(ax.quiver(0, 0, 0, mat[1,0], mat[1,1], mat[1,2], color="g"))
    quivers_artist.append(ax.quiver(0, 0, 0, mat[2,0], mat[2,1], mat[2,2], color="b"))


f1 = plt.figure(1)
ax = f1.add_subplot(projection='3d')

u = ax.quiver(0, 0, 0, 0, 0, 0, color="r")
v = ax.quiver(0, 0, 0, 0, 0, 0, color="g")
w = ax.quiver(0, 0, 0, 0, 0, 0, color="b")
# set empty line plots with colors associate to the
# quivers. Doing so we can show a legend.
ax.plot([], [], [], color="r", label="X")
ax.plot([], [], [], color="g", label="Y")
ax.plot([], [], [], color="b", label="Z")

ax.set_xlim(-1.1, 1.1); ax.set_ylim(-1.1, 1.1); ax.set_zlim(-1.1, 1.1)
ax.set_xlabel("X_AXIS"); ax.set_ylabel("Y_AXIS"); ax.set_zlabel("Z_AXIS")
ax.legend();



timerTickInterval = 1000
ani = FuncAnimation(f1, timerTick_Event, interval=timerTickInterval)
plt.show()

If you keep track of the quiver artists, you can use them to remove the previously plotted quivers:

import random as rnd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

quivers_artist = []

def timerTick_Event(i):
    global quivers_artist
    #%% DATA SIMULATION
    temp1 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
    temp1 = temp1 / np.linalg.norm(temp1)
    v2 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
    v2 = v2 / np.linalg.norm(v2)
    v3 = np.cross(temp1, v2)
    v3 = v3 / np.linalg.norm(v3)
    v1 = np.cross(v2, v3)
    v1 = v1 / np.linalg.norm(v1)    
    mat = np.column_stack((v1, v2, v3))
    #%% DATA REPRESENTATION
    f = plt.gcf()
    ax = f.gca()    
    #INSERT DELETE FUNCTION HERE
    if len(quivers_artist) > 0:
        for q in quivers_artist:
            q.remove()
        quivers_artist.clear()
    quivers_artist.append(ax.quiver(0, 0, 0, mat[0,0], mat[0,1], mat[0,2], color="r"))
    quivers_artist.append(ax.quiver(0, 0, 0, mat[1,0], mat[1,1], mat[1,2], color="g"))
    quivers_artist.append(ax.quiver(0, 0, 0, mat[2,0], mat[2,1], mat[2,2], color="b"))


f1 = plt.figure(1)
ax = f1.add_subplot(projection='3d')

u = ax.quiver(0, 0, 0, 0, 0, 0, color="r")
v = ax.quiver(0, 0, 0, 0, 0, 0, color="g")
w = ax.quiver(0, 0, 0, 0, 0, 0, color="b")
# set empty line plots with colors associate to the
# quivers. Doing so we can show a legend.
ax.plot([], [], [], color="r", label="X")
ax.plot([], [], [], color="g", label="Y")
ax.plot([], [], [], color="b", label="Z")

ax.set_xlim(-1.1, 1.1); ax.set_ylim(-1.1, 1.1); ax.set_zlim(-1.1, 1.1)
ax.set_xlabel("X_AXIS"); ax.set_ylabel("Y_AXIS"); ax.set_zlabel("Z_AXIS")
ax.legend();



timerTickInterval = 1000
ani = FuncAnimation(f1, timerTick_Event, interval=timerTickInterval)
plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文