質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

1206閲覧

pythonで3Dアニメーションの作り方について

NR4200

総合スコア41

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/12/27 05:15

前提・実現したいこと

計算したデータから3Dアニメーションを作ろうとしています.

3Dは初めてなので,2Dのアニメーション制作と同様の方法で作りましたがエラーが出ます.

プログラム内
q1,q2,q3,q4は計算した値が入ります.

発生している問題・エラーメッセージ

Traceback (most recent call last): File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook\__init__.py", line 216, in process func(*args, **kwargs) File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 953, in _start self._init_draw() File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 1732, in _init_draw self._draw_frame(next(self.new_frame_seq())) File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 1755, in _draw_frame self._drawn_artists = self._func(framedata, *self._args) File "test_anime_2.py", line 501, in animate line.set_data(thisx, thisy, thisz) File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\lines.py", line 659, in set_data x, y = args ValueError: too many values to unpack (expected 2) Traceback (most recent call last): File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\cbook\__init__.py", line 216, in process func(*args, **kwargs) File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 1269, in _handle_resize self._init_draw() File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 1732, in _init_draw self._draw_frame(next(self.new_frame_seq())) File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 1755, in _draw_frame self._drawn_artists = self._func(framedata, *self._args) File "test_anime_2.py", line 501, in animate line.set_data(thisx, thisy, thisz) File "C:\Users\N\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\lines.py", line 659, in set_data x, y = args ValueError: too many values to unpack (expected 2)

該当のソースコード

python

1#アニメーショングラフ 2fig = plt.figure(figsize=(10,10)) 3ax = fig.gca(projection='3d') 4 5#軸の範囲の指定 6ax.set_xlim(-3, 3) 7ax.set_ylim(-3, 3) 8ax.set_zlim(-3, 3) 9 10 11line, = ax.plot([], [], [], 'o-', lw=2,c="b") 12def animate(i): 13 x1 = l2*np.cos(q2[i])*np.cos(q1[i]) 14 x2 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i]))*np.cos(q1[i]) 15 x3 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i])+l4*np.cos(q2[i]+q3[i]+q4[i]))*np.cos(q1[i]) 16 y1 = l2*np.cos(q2[i])*np.sin(q1[i]) 17 y2 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i]))*np.sin(q1[i]) 18 y3 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i])+l4*np.cos(q2[i]+q3[i]+q4[i]))*np.sin(q1[i]) 19 z1 = l2*np.sin(q2[i]) 20 z2 = l2*np.sin(q2[i])+l3*np.sin(q2[i]+q3[i]) 21 z3 = l2*np.sin(q2[i])+l3*np.sin(q2[i]+q3[i])+l4*np.sin(q2[i]+q3[i]+q4[i]) 22 thisx = [0, x1, x2,x3] 23 thisy = [0, y1, y2,y3] 24 thisz = [0, z1, z2,z3] 25 26 line.set_data(thisx, thisy, thisz) 27 return line, 28 29ani = FuncAnimation(fig, animate, frames=np.arange(0, len(t)),interval=10, blit = True) 30 31plt.show()

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2019/12/27 05:27

質問を編集して完全なコードをご提示ください。これだけでは動きません。そのままコピペで走るように。
guest

回答1

0

自己解決

python

1def animate(i): 2 x1 = l2*np.cos(q2[i])*np.cos(q1[i]) 3 x2 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i]))*np.cos(q1[i]) 4 x3 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i])+l4*np.cos(q2[i]+q3[i]+q4[i]))*np.cos(q1[i]) 5 y1 = l2*np.cos(q2[i])*np.sin(q1[i]) 6 y2 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i]))*np.sin(q1[i]) 7 y3 = (l2*np.cos(q2[i])+l3*np.cos(q2[i]+q3[i])+l4*np.cos(q2[i]+q3[i]+q4[i]))*np.sin(q1[i]) 8 z1 = l2*np.sin(q2[i])+1 9 z2 = l2*np.sin(q2[i])+l3*np.sin(q2[i]+q3[i])+1 10 z3 = l2*np.sin(q2[i])+l3*np.sin(q2[i]+q3[i])+l4*np.sin(q2[i]+q3[i]+q4[i])+1 11 thisx = [0,0, x1, x2,x3] 12 thisy = [0,0, y1, y2,y3] 13 thisz = [0,1, z1, z2,z3] 14 15 line.set_data(thisx, thisy) 16 line.set_3d_properties(thisz) 17 return line, 18

3次元目のZ軸をline.set_3d_properties(thisz)
に変更することで解決しました.

投稿2019/12/27 05:45

NR4200

総合スコア41

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問