Make the animation 3D
authorLukáš Jiřiště <lukas.jiriste@datapartner.cz>
Fri, 5 Dec 2025 10:57:22 +0000 (11:57 +0100)
committerLukáš Jiřiště <lukas.jiriste@datapartner.cz>
Fri, 5 Dec 2025 10:57:22 +0000 (11:57 +0100)
animate_result.py

index 486cb5b5919beb910cf24f5cb8efd310069e99a9..556d579e397b5d3f85426e78e134d0d3709a6ba2 100755 (executable)
@@ -1,42 +1,53 @@
 #!/usr/bin/env python3
 
 import sys
-import matplotlib.pyplot as plt
 import numpy as np
-import matplotlib.animation as animation
 import scipy as scp
+import matplotlib.pyplot as plt
+import mpl_toolkits.mplot3d.axes3d as p3
+import matplotlib.animation as animation
 
 class ExtLine:
 
-    def __init__(self, line, name_x, name_y):
+    def __init__(self, line, name_x, name_y, name_z):
         self.line = line;
         self.name_x = name_x;
         self.name_y = name_y;
+        self.name_z = name_z;
 
 class MatAnimation:
 
     def __init__(self, data_dict = dict()):
         self.data = data_dict;
         self.lines = [];
-        self.fig, self.axes = plt.subplots();
+        self.fig = plt.figure();
+        self.axes = self.fig.add_subplot(projection="3d");
 
-    def plot(self, name_x, name_y):
+    def plot(self, name_x, name_y, name_z):
+        line, = self.axes.plot(
+            self.data[name_x],
+            self.data[name_y],
+            self.data[name_z])
         self.lines.append(
             ExtLine(
-                self.axes.plot(self.data[name_x], self.data[name_y])[0],
+                line,
                 name_x,
-                name_y));
+                name_y,
+                name_z));
 
     def show(self):
         self.animation = animation.FuncAnimation(
             self.fig, self.update, len(self.data["time"]),
-            interval = 1);
+            interval = 100,
+            blit=False);
         plt.show();
 
     def update(self, frame):
         for line in self.lines:
-            line.line.set_xdata(self.data[line.name_x][:frame]);
-            line.line.set_ydata(self.data[line.name_y][:frame]);
+            line.line.set_data([
+                self.data[line.name_x][:frame],
+                self.data[line.name_y][:frame]]);
+            line.line.set_3d_properties(self.data[line.name_z][:frame]);
         return [line.line for line in self.lines];
 
 def transpose_strings(array):
@@ -56,13 +67,15 @@ def matAniFromFile(filename):
     inds = data["dataInfo"][1];
     ani = MatAnimation();
     for i in inds:
-        ani.data[names[i - 1]] = pure_data[i - 1, :-1];
+        ani.data[names[i - 1]] = data["data_2"][i - 1, :-1];
     return (ani);
 
 
 ani = matAniFromFile(sys.argv[1]);
-ani.plot("p[1].pos[1]", "p[1].pos[2]");
-ani.plot("p[2].pos[1]", "p[2].pos[2]");
-ani.plot("p[3].pos[1]", "p[3].pos[2]");
-ani.plot("p[4].pos[1]", "p[4].pos[2]");
+for i in range(1,10):
+    ani.plot(
+        "p[" + str(i) + "].pos[1]",
+        "p[" + str(i) + "].pos[2]",
+        "p[" + str(i) + "].pos[3]"
+        );
 ani.show();