0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで3D空間上に棒を動かすアニメーション

Posted at

棒の端点を時系列データで動かすことで,3D空間上にアニメーション表示する

Bar_anim3.py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D

# サンプル時系列加速度データを生成(ここに実際のデータを使用)
time_steps = 100
t = np.linspace(0, 10, time_steps)
acceleration = np.sin(2 * np.pi * t)  # 加速度データの例

# 棒の長さと節点の初期位置を設定
length = 1.0
initial_positions = np.array([[0, 0, 0], [length, 0, 0]])

# フィギュアと3Dプロットの作成
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim(-0.5, 1.5)
ax.set_ylim(-0.5, 0.5)
ax.set_zlim(-0.5, 0.5)

# 初期プロットの設定
line, = ax.plot(initial_positions[:, 0], initial_positions[:, 1], initial_positions[:, 2], lw=3)

# アニメーション初期化関数
def init():
    line.set_data(initial_positions[:, 0], initial_positions[:, 1])
    line.set_3d_properties(initial_positions[:, 2])
    return line,

# アニメーション関数
def animate(i):
    # 棒の端点の位置を更新(1点の加速度データに基づいて)
    displacement = 0.5 * acceleration[i] * t[i]**2  # 簡略化した変位計算
    new_positions = initial_positions.copy()
    new_positions[1, 2] = displacement  # Z軸方向に変位を適用
    
    line.set_data(new_positions[:, 0], new_positions[:, 1])
    line.set_3d_properties(new_positions[:, 2])
    return line,

# アニメーションの設定
ani = animation.FuncAnimation(fig, animate, init_func=init, frames=time_steps, interval=100, blit=True)

# GIFとして保存
ani.save("animation.gif", writer='pillow', fps=10)

# アニメーションの表示
plt.show()

animation.gif

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?