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

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

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

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

Python

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

Q&A

解決済

1回答

17943閲覧

animationがうまくいかないです。

Fallout_18

総合スコア124

Python 3.x

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

Python

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

0グッド

0クリップ

投稿2018/05/09 07:58

animationとして、以下のコード結果を動画で保存したいのですが、以下のようなエラーが出てしまいました。
いくつかネットで調べたのですが、おそらく
def plot(data):
plt cla()
のやり方の方が今回は良いかもしれませんが、以下は違う書き方を自分がしていると思います。

python

1import numpy as np 2import matplotlib.pyplot as plt 3import math 4#from mpl_toolkits.mplot3d import Axes3D 5import matplotlib.animation as animation 6 7fig = plt.figure() 8ims=[] 9 10#環境設定 11n=100 12m=100 13theta = 3*(math.pi)/12 14 15P = [[np.cos(theta),np.sin(theta)],[0,0]] 16Q = [[0,0],[np.sin(theta),-np.cos(theta)]] 17x_list=[]#xline 18t_list=[]#time 19p_list=[]#probability 20s_list=[]#state 21a = 1/math.sqrt(2) 22b = 1j/math.sqrt(2) 23 24for x in range(0,2*m+1): 25 if x == m: 26 phi = [a ,b] 27 else: 28 phi = [0,0] 29 p = np.dot(phi,np.conj(phi)) 30 31 x_list.append(x) 32 s_list.append(phi) 33 p_list.append(p) 34 35 36for t in range(0,n+1): 37 t_list.append(t) 38 if t ==0: 39 s_list 40 p_list 41 else: 42 next_s_list = [0]*len(s_list) #s_listと同じ要素の数ですべて0を用意(初期化) 43 for x in range(0,2*m+1): 44 if x == 0: 45 next_s_list[0] = np.inner(P, s_list[1]) 46 47 elif x == 2*n: 48 next_s_list[2*m] = np.inner(Q, s_list[2*m-1]) 49 50 else: 51 next_s_list[x] = np.inner(P, s_list[x+1]) + np.inner(Q, s_list[x-1]) 52 p_list[x] = np.dot(next_s_list[x],np.conj(next_s_list[x])) 53 s_list = next_s_list 54 55 56 #確率の挙動をみたいなら 57 print(t,s_list) 58 plt.xlabel("x") 59 plt.ylabel("probability") 60 plt.ylim([0,0.1]) 61 plt.xlim([-m,3*m]) 62 line = plt.plot(x_list,np.real(p_list),color="red",linewidth=0.3) 63 ims.append([line]) 64 65ani = animation.ArtistAnimation(fig,ims, interval=100) 66ani.save("quantumwalk.gif")

ちなみに、コード自体は計算されており、エラーが以下のように出ました。

ValueError: outfile must be *.htm or *.html

結構、調べたんですけど、力足らずで申し訳ないです。。
ご指導の程、宜しくお願いします。。

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

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

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

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

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

guest

回答1

0

ベストアンサー

動画の生成に必要なMovieWriterが存在しないためだと思われます。
環境にもよりますがmatplotlib のanimation を保存などに記載されているように別途ツールのインストールと設定が必要なようです。

詳細未未確認ですが.to_jshtmlにてインタラクティブなHTML形式で出力する手法だとMovieWrite不要なようです。

ちなみに当方Win10+Anaconda環境での検証結果は以下のとおりとなりました。

Python

1import numpy as np 2import matplotlib.pyplot as plt 3import matplotlib.animation as animation 4 5# 単純なアニメーション 6fig = plt.figure() 7x = np.arange(0, 10, 0.1) 8ims = [] 9for a in range(50): 10 y = np.sin(x - a) 11 line, = plt.plot(x, y, "r") 12 ims.append([line]) 13ani = animation.ArtistAnimation(fig, ims) 14 15ani.save('anim.gif') 16# MovieWriter ffmpeg unavailable. と表示されるがOK 17 18# ani.save('anim.gif', writer="imagemagick") 19# ani.save('anim.mp4') 20# ani.save('anim.mp4', writer="ffmpeg") 21# 22# 以下のようなエラーにてNG 23# 24# MovieWriter imagemagick(またはffmgpeg) unavailable. 25# Traceback (most recent call last): 26# 略 27# ValueError: outfile must be *.htm or *.html 28# 29 30#ani.save('anim.htm') 31# 32# 以下のようなエラーにてNG 33# 34# MovieWriter ffmgpeg unavailable. 35# Traceback (most recent call last): 36# 略 37# AttributeError: module 'PIL._webp' has no attribute 'HAVE_WEBPANIM' 38 39# OK 40s = ani.to_jshtml() 41with open( 'anim.html', 'w') as f: 42 f.write(s)

投稿2018/05/09 09:59

can110

総合スコア38262

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

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

Fallout_18

2018/05/09 15:41

ありがとうございます!!! 確認してみます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問