我想使用 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)
有什么建議嗎?先感謝您!
uj5u.com熱心網友回復:
如果您跟蹤 quiver 藝術家,您可以使用它們洗掉之前繪制的 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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/478258.html
標籤:Python matplotlib 阴谋
