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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

5413閲覧

python(jupyter) アニメーション 動かない

rr9500sc

総合スコア16

Jupyter

Jupyter (旧IPython notebook)は、Notebook形式でドキュメント作成し、プログラムの記述・実行、その実行結果を記録するツールです。メモの作成や保存、共有、確認などもブラウザ上で行うことができます。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2019/06/28 07:45

編集2019/07/02 06:42

jupyter notebookにて
下記のコードで1つの点を指定した座標の通りに動くアニメーションの作成中です。

%matplotlib notebook %matplotlib nbagg from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import matplotlib.animation as animation from IPython.display import HTML import numpy as np def init(): ax.set_xlim(-10,10) ax.set_ylim(-10,10) ax.set_zlim(0,10) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") ax.set_aspect('equal') return lines x1,y1,z1 = [],[],[] x1=[1,8,3,6,3,6,4] y1=[9,1,5,3,7,3,8] z1=[0,2,5,7,1,0,5] def update_lines(num, data, lines,points): for dat,line, point in zip(data,lines, points): point.set_data(dat[0:2, num]) point.set_3d_properties(dat[2, num]) return lines # Attaching 3D axis to the figure fig = plt.figure() ax = fig.gca(projection='3d') # Lines to plot in 3D t = np.linspace(-3, 2, 30) z = np.linspace(-3, 2, 30) r = np.linspace(-3, 2, 30) x1, y1, z1 = x1, y1, z1#プロットするデータ data = np.array([[x1,y1,z1]])#プロットするデータ points = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data] line_ani = animation.FuncAnimation(fig, update_lines, 300, fargs=(data, lines, points), interval=10, init_func=init, blit=True, repeat=True)

一度は思い通りに動いたのですが
再度notebookを起動すると以下のエラーが発生してしまい、
スタートの地点から動かなくなってしまいました。

--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-10-16e17015c295> in <module> 44 points = [ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data] 45 ---> 46 line_ani = animation.FuncAnimation(fig, update_lines, 300, fargs=(data, lines, points), 47 interval=10, init_func=init, blit=True, repeat=True) 48 NameError: name 'lines' is not defined ​

解決を試みるために該当箇所のlinesを消す等の処置を行いましたが
全く動きませんでした。

解決手段をご教授願いたいです。

linspace等の数字は無意図的に動かしたら動いたというものなので
正しい設定ではないかと思っています。

また、このコードのx1,y1,z1の部分を
x2,y2,z2のようにコピペで増やしていけば同一の空間に2つ以上の点を動かすことが出来るようになるのでしょうか?
また、出来ないとしたらどのようにしたら解決するのかも教えていただきたいです。

よろしくお願いします。

2019年6月29日追記

%matplotlib notebook %matplotlib nbagg from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import matplotlib.animation as animation from IPython.display import HTML import numpy as np def init(): ax.set_xlim(-10,10) ax.set_ylim(-10,10) ax.set_zlim(0,10) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") ax.set_aspect('equal') return lines x1,y1,z1 = [],[],[] x1=[1,8,3,6,3,6,4] y1=[9,1,5,3,7,3,8] z1=[0,2,5,7,1,0,5] def update_lines(num, data, lines,points): for dat,line, point in zip(data,lines, points): point.set_data(dat[0:2, num]) point.set_3d_properties(dat[2, num]) return lines # Attaching 3D axis to the figure fig = plt.figure() ax = fig.gca(projection='3d') # Lines to plot in 3D t = np.linspace(-3, 2, 30) z = np.linspace(-3, 2, 30) r = np.linspace(-3, 2, 30) x1, y1, z1 = x1, y1, z1#プロットするデータ data = np.array([[x1,y1,z1]])#プロットするデータ ※lines=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1] )[0] for dat in data]※ points=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data] line_ani = animation.FuncAnimation(fig, update_lines, 300, fargs=(data, lines,points), interval=10, init_func=init, blit=True, repeat=True)

※この印が訂正点です※

以上のコードで動かないという問題点は解決いたしました。
しかし、複数のコードを動かすために以下のようなコードを組んだのですが
x2,y2,z2,の座標の点しか現れませんでした。
解決策の知恵をお貸し願いたいです。

%matplotlib notebook %matplotlib nbagg from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import matplotlib.animation as animation from IPython.display import HTML import numpy as np def init(): ax.set_xlim(-10,10) ax.set_ylim(-10,10) ax.set_zlim(0,10) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") ax.set_aspect('equal') return lines x1,y1,z1 = [],[],[] x2,y2,z2 = [],[],[] x1=[1,8,3,6,3,6,4] y1=[9,1,5,3,7,3,8] z1=[0,2,5,7,1,0,5] x2=[-1,-8,3-,-6,-3,-6,-4] y2=[-9,-1,-5,-3,-7,-3,-8] z2=[1,3,1,2,4,2,4] def update_lines(num, data, lines,points): for dat,line, point in zip(data,lines, points): point.set_data(dat[0:2, num+1]) point.set_3d_properties(dat[2, num+1]) return lines # Attaching 3D axis to the figure fig = plt.figure() ax = fig.gca(projection='3d') # Lines to plot in 3D t = np.linspace(-3, 2, 30) z = np.linspace(-3, 2, 30) r = np.linspace(-3, 2, 30) x1, y1, z1 = x1, y1, z1#プロットするデータ data = np.array([[x1,y1,z1]])#プロットするデータ x2, y2, z2 = x2, y2, z2#プロットするデータ data = np.array([[x2,y2,z2]])#プロットするデータ lines=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1] )[0] for dat in data] points=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data] line_ani = animation.FuncAnimation(fig, update_lines, 300, fargs=(data,lines, points), interval=10, init_func=init, blit=True, repeat=True)

よろしくお願いいたします。

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

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

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

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

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

t_obara

2019/06/28 09:42

エラーメッセージの通り、linesがどこにも定義されていないので、それをなんとかしないと動作しません。 notebookを起動する前に、どこかでlinesの定義をしたことがあったからタマタマ以前は動作したのではないでしょうか。 コピペコードなのかわかりませんが、このコードから類推してlinesはこのように定義すべきと回答するのも大変です。貴殿がlinesの定義を思い出して実装するのが一番かと。
mather

2019/06/28 11:07

> 解決を試みるために該当箇所のlinesを消す等の処置を行いましたが全く動きませんでした。 「linesを消す等の処置」って具体的には何をしたのかコードを提示したほうが良いですよ。
rr9500sc

2019/06/28 15:50

投稿ありがとうございます。 点が動かないという問題に関しては解決しました。 今から具体的なコードを載せます。 しかし、2つ以上の点を同一の空間の中で動かせるように試みましたが 上手くいきませんでした。 よろしければ知恵をお貸しください。
guest

回答1

0

ベストアンサー

-さらにコメントを受けて-

返答遅くなりました。

fancanimation関数について調べたところ、参考記事にて、複数のグラフについてアニメーションを行う場合、プロットデータを別々に書くのがよろしいと書いてありました。

参考記事:matplotlibで複数のグラフをアニメーションさせる

今回の場合、以下のようなコードにするとよいかと思います。一応アニメーション動作確認済みです。

python

1 2(前略) 3 4def update_lines(num,data1,data2,lines1,lines2, points1,points2): 5 for dat1,dat2,line1,line2,point1, point2 in zip(data1,data2,lines1,lines2, points1,points2): 6 point1.set_data(dat1[0:2, num+1]) 7 point1.set_3d_properties(dat1[2, num+1]) 8 point2.set_data(dat2[0:2, num+1]) 9 point2.set_3d_properties(dat2[2, num+1]) 10 11#下記コードのように分割して書いても可能 12# for dat1,line1,point1 in zip(data1,lines1, points1): 13# point1.set_data(dat1[0:2, num+1]) 14# point1.set_3d_properties(dat1[2, num+1]) 15# for dat2,line2,point2 in zip(data2,lines2, points2): 16# point2.set_data(dat2[0:2, num+1]) 17# point2.set_3d_properties(dat2[2, num+1]) 18 19 return lines1,lines2 20 21# Attaching 3D axis to the figure 22fig = plt.figure() 23ax = fig.gca(projection='3d') 24 25# Lines to plot in 3D 26t = np.linspace(-3, 2, 30) 27z = np.linspace(-3, 2, 30) 28r = np.linspace(-3, 2, 30) 29 30#data,lines,pointsについてそれぞれ定義する 31x1, y1, z1 = x1, y1, z1 32x2, y2, z2 = x2, y2, z2 33 34data1 = np.array([[x1,y1,z1]]) 35data2 = np.array([[x2,y2,z2]]) 36 37lines1=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1] )[0] for dat in data1] 38lines2=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1] )[0] for dat in data2] 39 40points1=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data1] 41points2=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data2] 42 43#アニメーション 44line_ani = animation.FuncAnimation(fig, update_lines, 300, fargs=(data1,data2,lines1,lines2, points1,points2), 45 interval=1000, init_func=init, blit=True, repeat=True)

動作確認・ご検討よろしくおねがいします。


-コメントを受けて-

こちらが提案したコードの問題でしょう。割とこちらも模索してる状態なので…申し訳ないです。

現状思いつく改善案を2つ上げておきます。

python

1#改善案1 2#処理順を考えると点1が動いてから点2が動く、という要望とは少し違う動作になる可能性が高い 3 4x1, y1, z1 = x1, y1, z1 5datas=[] 6#変数名の重複を防ぐためdataからdatapに変更 7datap = np.array([[x1,y1,z1]]) 8datas.append(datap) 9x2, y2, z2 = x2, y2, z2 10#同様にdatapに変更 11datap = np.array([[x2,y2,z2]]) 12datas.append(datap) 13 14lines=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1] )[0] for dat in data for data in datas]#2重のfor文に変える 15points=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data for data in datas] 16 17for data in datas: 18 line_ani = animation.FuncAnimation(fig, update_lines, 300, fargs=(data,lines, points), 19 interval=10, init_func=init, blit=True, repeat=True)

python

1#改善案2 2#2点を同時に移動させたい、がうまく動作しないかもしれない 3 4def update_lines(num, datas, lines,points): 5 for data in datas: 6 for dat,line, point in zip(data,lines, points): 7 point.set_data(dat[0:2, num+1]) 8 point.set_3d_properties(dat[2, num+1]) 9 10 return lines 11 12#中略 13 14line_ani = animation.FuncAnimation(fig, update_lines, 300, fargs=(datas,lines, points), 15 interval=10, init_func=init, blit=True, repeat=True)

動作確認・ご検討よろしくおねがいします。

-追記終わり-


追記部分に関する回答になります。

dataが上書きされているのが原因だと思います。なので、リストを作ってそこにプロットするデータを格納し、for文でデータを持ってくる形にすれば2点以上の座標をplotできるかと思います。

x1, y1, z1 = x1, y1, z1 datas=[]#データリスト datap = np.array([[x1,y1,z1]])#プロットするデータ datas.append(datap)#データをリストに入れる x2, y2, z2 = x2, y2, z2#プロットするデータ datap = np.array([[x2,y2,z2]])#プロットするデータ datas.append(datap) lines=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1] )[0] for dat in data for data in datas]#2重のfor文に変える points=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data for data in datas]

ただ、line_ani部分がどう動くか…自分はアニメーション部分の動作確認ができなかった(最終的なグラフしか確認できず)ので、もしかしたらそこで不具合が発生してるかもです。

投稿2019/07/01 02:40

編集2019/07/05 13:08
amahara_waya

総合スコア1029

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

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

rr9500sc

2019/07/01 03:33

回答ありがとうございます。 いただいた回答通りに変更してみたのですが 今まで通りにx2,y2,z2が動くのともう1つx2,y2,z2の始点にあたる部分に動かない点が存在する というものになりました。 訂正の仕方が悪かったのでしょうか。。。 %matplotlib notebook %matplotlib nbagg from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import matplotlib.animation as animation from IPython.display import HTML import numpy as np def init(): ax.set_xlim(-10,10) ax.set_ylim(-10,10) ax.set_zlim(0,10) ax.set_xlabel("x") ax.set_ylabel("y") ax.set_zlabel("z") ax.set_aspect('equal') return lines x1,y1,z1 = [],[],[] x2,y2,z2 = [],[],[] x1=[1,8,3,6,3,6,4] y1=[9,1,5,3,7,3,8] z1=[0,2,5,7,1,0,5] x2=[-1,-8,-3,-6,-3,-6,-4] y2=[-9,-1,-5,-3,-7,-3,-8] z2=[1,3,1,2,4,2,4] def update_lines(num, data, lines,points): for dat,line, point in zip(data,lines, points): point.set_data(dat[0:2, num+1]) point.set_3d_properties(dat[2, num+1]) return lines # Attaching 3D axis to the figure fig = plt.figure() ax = fig.gca(projection='3d') # Lines to plot in 3D t = np.linspace(-3, 2, 30) z = np.linspace(-3, 2, 30) r = np.linspace(-3, 2, 30) x1, y1, z1 = x1, y1, z1 datas=[]#データリスト data = np.array([[x1,y1,z1]])#プロットするデータ datas.append(data)#データをリストに入れる x2, y2, z2 = x2, y2, z2#プロットするデータ data = np.array([[x2,y2,z2]])#プロットするデータ datas.append(data) lines=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1] )[0] for dat in data for data in datas]#2重のfor文に変える points=[ax.plot(dat[0, 0:1], dat[1, 0:1], dat[2, 0:1], 'o')[0] for dat in data for data in datas] line_ani = animation.FuncAnimation(fig, update_lines, 10, fargs=(data,lines, points), interval=1000, init_func=init, blit=True, repeat=True)
rr9500sc

2019/07/02 02:32

こんにちは。 親身な回答ありがとうございます。 どちらの方法を試し、また両方同時に変更もしてみましたが x1,y1,z1の方の点が現れず。。。という形でした。 自分でも解決のために試行錯誤をしているのですが なかなか難しいみたいです。。。 何から何まで申し訳ないです。
rr9500sc

2019/07/06 02:05

度々のご回答本当に感謝しております。 先ほど動作確認をすることが出来ました。 まさに思った通りのプログラムでした。 また機会がありましたら、よろしくお願いします!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問