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 3×3 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:
JavaScript
x
60
60
1
import random as rnd
2
import numpy as np
3
import matplotlib.pyplot as plt
4
from matplotlib.animation import FuncAnimation
5
6
7
def timerTick_Event(i):
8
#%% DATA SIMULATION
9
temp1 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
10
temp1 = temp1 / np.linalg.norm(temp1)
11
12
v2 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
13
v2 = v2 / np.linalg.norm(v2)
14
15
v3 = np.cross(temp1, v2)
16
v3 = v3 / np.linalg.norm(v3)
17
18
v1 = np.cross(v2, v3)
19
v1 = v1 / np.linalg.norm(v1)
20
21
mat = np.column_stack((v1, v2, v3))
22
23
24
#%% DATA REPRESENTATION
25
f = plt.gcf()
26
ax = f.gca()
27
28
29
#INSERT DELETE FUNCTION HERE
30
31
32
u = ax.quiver(0, 0, 0, mat[0,0], mat[0,1], mat[0,2], color="r")
33
v = ax.quiver(0, 0, 0, mat[1,0], mat[1,1], mat[1,2], color="g")
34
w = ax.quiver(0, 0, 0, mat[2,0], mat[2,1], mat[2,2], color="b")
35
36
plt.show()
37
38
39
f1 = plt.figure(1)
40
ax = f1.add_subplot(projection='3d')
41
42
u = ax.quiver(0, 0, 0, 0, 0, 0, color="r")
43
v = ax.quiver(0, 0, 0, 0, 0, 0, color="g")
44
w = ax.quiver(0, 0, 0, 0, 0, 0, color="b")
45
# set empty line plots with colors associate to the
46
# quivers. Doing so we can show a legend.
47
ax.plot([], [], [], color="r", label="X")
48
ax.plot([], [], [], color="g", label="Y")
49
ax.plot([], [], [], color="b", label="Z")
50
51
ax.set_xlim(-1.1, 1.1); ax.set_ylim(-1.1, 1.1); ax.set_zlim(-1.1, 1.1)
52
ax.set_xlabel("X_AXIS"); ax.set_ylabel("Y_AXIS"); ax.set_zlabel("Z_AXIS")
53
ax.legend();
54
55
56
57
timerTickInterval = 1000
58
ani = FuncAnimation(f1, timerTick_Event, interval=timerTickInterval)
59
plt.show()
60
This code works fine except for the missing delete function. In a 2d plot, I used this code to delete annotations
JavaScript
1
4
1
for child in ax.get_children():
2
if isinstance(child, mTxt.Annotation):
3
child.remove()
4
and this command to delete the first plotted line
JavaScript
1
2
1
ax.lines.pop(0)
2
Any suggestion? Thank you in advance!
Advertisement
Answer
If you keep track of the quiver artists, you can use them to remove the previously plotted quivers:
JavaScript
1
54
54
1
import random as rnd
2
import numpy as np
3
import matplotlib.pyplot as plt
4
from matplotlib.animation import FuncAnimation
5
6
quivers_artist = []
7
8
def timerTick_Event(i):
9
global quivers_artist
10
#%% DATA SIMULATION
11
temp1 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
12
temp1 = temp1 / np.linalg.norm(temp1)
13
v2 = np.array([rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0), rnd.uniform(-1.0, 1.0)]).astype(float)
14
v2 = v2 / np.linalg.norm(v2)
15
v3 = np.cross(temp1, v2)
16
v3 = v3 / np.linalg.norm(v3)
17
v1 = np.cross(v2, v3)
18
v1 = v1 / np.linalg.norm(v1)
19
mat = np.column_stack((v1, v2, v3))
20
#%% DATA REPRESENTATION
21
f = plt.gcf()
22
ax = f.gca()
23
#INSERT DELETE FUNCTION HERE
24
if len(quivers_artist) > 0:
25
for q in quivers_artist:
26
q.remove()
27
quivers_artist.clear()
28
quivers_artist.append(ax.quiver(0, 0, 0, mat[0,0], mat[0,1], mat[0,2], color="r"))
29
quivers_artist.append(ax.quiver(0, 0, 0, mat[1,0], mat[1,1], mat[1,2], color="g"))
30
quivers_artist.append(ax.quiver(0, 0, 0, mat[2,0], mat[2,1], mat[2,2], color="b"))
31
32
33
f1 = plt.figure(1)
34
ax = f1.add_subplot(projection='3d')
35
36
u = ax.quiver(0, 0, 0, 0, 0, 0, color="r")
37
v = ax.quiver(0, 0, 0, 0, 0, 0, color="g")
38
w = ax.quiver(0, 0, 0, 0, 0, 0, color="b")
39
# set empty line plots with colors associate to the
40
# quivers. Doing so we can show a legend.
41
ax.plot([], [], [], color="r", label="X")
42
ax.plot([], [], [], color="g", label="Y")
43
ax.plot([], [], [], color="b", label="Z")
44
45
ax.set_xlim(-1.1, 1.1); ax.set_ylim(-1.1, 1.1); ax.set_zlim(-1.1, 1.1)
46
ax.set_xlabel("X_AXIS"); ax.set_ylabel("Y_AXIS"); ax.set_zlabel("Z_AXIS")
47
ax.legend();
48
49
50
51
timerTickInterval = 1000
52
ani = FuncAnimation(f1, timerTick_Event, interval=timerTickInterval)
53
plt.show()
54