Matplotlib - 从 3d 箭袋图中删除线条
Matplotlib - Delete lines from a 3d quiver plot
我想用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()
除了缺少删除功能外,这段代码工作正常。在二维图中,我使用此代码删除注释
for child in ax.get_children():
if isinstance(child, mTxt.Annotation):
child.remove()
此命令用于删除第一条绘制线
ax.lines.pop(0)
有什么建议吗?提前致谢!
如果您跟踪箭袋艺术家,您可以使用它们来删除之前绘制的箭袋:
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()
我想用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()
除了缺少删除功能外,这段代码工作正常。在二维图中,我使用此代码删除注释
for child in ax.get_children():
if isinstance(child, mTxt.Annotation):
child.remove()
此命令用于删除第一条绘制线
ax.lines.pop(0)
有什么建议吗?提前致谢!
如果您跟踪箭袋艺术家,您可以使用它们来删除之前绘制的箭袋:
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()