-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.py
More file actions
60 lines (48 loc) · 1.87 KB
/
plot.py
File metadata and controls
60 lines (48 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import json
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# JSONファイルを読み込む
with open('data.json', 'r') as file:
data = json.load(file)
# 各粒子のデータを時間順に整理
particles = {}
for time, particles_data in data.items():
for particle_id, position in particles_data.items():
if particle_id not in particles:
particles[particle_id] = {"x": [], "y": [], "z": []}
particles[particle_id]["x"].append(float(position[0]))
particles[particle_id]["y"].append(float(position[1]))
particles[particle_id]["z"].append(float(position[2]))
# 3Dプロットの準備
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 軸範囲を固定するための最小値・最大値の計算
all_x = [x for particle in particles.values() for x in particle["x"]]
all_y = [y for particle in particles.values() for y in particle["y"]]
all_z = [z for particle in particles.values() for z in particle["z"]]
x_min, x_max = min(all_x), max(all_x)
y_min, y_max = min(all_y), max(all_y)
z_min, z_max = min(all_z), max(all_z)
# 各軸の中心を計算
x_center = (x_min + x_max) / 2
y_center = (y_min + y_max) / 2
z_center = (z_min + z_max) / 2
# 各軸の範囲を統一
range_max = max(x_max - x_min, y_max - y_min, z_max - z_min)
# 軸ラベルと範囲を設定
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
ax.set_xlim(x_center - range_max / 2, x_center + range_max / 2)
ax.set_ylim(y_center - range_max / 2, y_center + range_max / 2)
ax.set_zlim(z_center - range_max / 2, z_center + range_max / 2)
# 各粒子の軌跡をプロット
for particle_id, coords in particles.items():
ax.plot(coords["x"], coords["y"], coords["z"], label=f'Particle {particle_id}')
# グリッドを表示
ax.grid(True)
# 凡例を表示
ax.legend()
# グラフを表示
plt.show()