ヘディングのテキスト### 前提・実現したいこと
コード
ここに質問の内容を詳しく書いてください。
Anaconda環境でPythonの使い方を勉強しています。
以下の2つの現象が発生しました。原因と対策を教えてください。
現象1.「Pythonからはじめる数学入門」2017年1月16日 初版第4刷発行
ページ163の「6.1.2 図形のアニメーションを作る」のプログラムにおいて、
matplotlibパッケージからFuncAnimationモジュールをインポートして、
円が最初は小さくてしだいに大きくなるアニメーションを作りますが、円が描かれません。
現象2.matplotlib.org からサンプルプログラムDecay をダウンロードして実行させましたが、
アニメーションは描かれません。
発生している問題・エラーメッセージ
現象1
アニメーションが作成されません。
エラーメッセージは表示されません。
現象2
アニメーションが作成されません。
エラーメッセージは表示されません。
該当のソースコード
現象1のソースコード 許諾を得ていないので省略させていただきます。 現象2のソースコード ``````ここに言語を入力 ```===== Decay ===== This example showcases: - using a generator to drive an animation, - changing axes limits during an animation. """ import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation def data_gen(t=0): print('data-gen-1') # tsuika cnt = 0 while cnt < 1000: cnt += 1 t += 0.1 yield t, np.sin(2*np.pi*t) * np.exp(-t/10.) def init(): print('init-1') # tsuika ax.set_ylim(-1.1, 1.1) ax.set_xlim(0, 10) del xdata[:] del ydata[:] line.set_data(xdata, ydata) return line, print('fig-01') # tsuika fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) ax.grid() xdata, ydata = [], [] def run(data): print('run-1') # tsuika t, y = data xdata.append(t) ydata.append(y) xmin, xmax = ax.get_xlim() if t >= xmax: ax.set_xlim(xmin, 2*xmax) ax.figure.canvas.draw() line.set_data(xdata, ydata) return line, if __name__ == '__main__': # tsuika print('main-01') ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, repeat=False, init_func=init) print('main-02 ani =',ani) # tsuika print('main-03 run =',run) # tsuika print('main-04 data_gen =',data_gen) # tsuika plt.show() print('main-05') # tsuika # 以上 ### 試したこと 現象1 書籍の発行元のサイトで調べましたが、該当箇所に修正などの記載はありません。 現象2の実行結果 トレースのため挿入したprint文によるもの(fig-1からmain-05までが全文です)。 fig-01 main-01 main-02 ani = <matplotlib.animation.FuncAnimation object at 0x0000016E22CA5BA8> main-03 run = <function run at 0x0000016E2291FA60> main-04 data_gen = <function data_gen at 0x0000016E2291FEA0> init-1 描かれたグラフについて 罫線と横軸の目盛り0,2,4,6,8,10 縦軸の目盛り-1,-0.75,-0.5,...,0.75,1が描かれた。 関数np.sin(2*np.pi*t) * np.exp(-t/10.)は描かれない。 main-05 ### 補足情報(FW/ツールのバージョンなど) Windows10 Home バージョン1803 Python 3.7.1 IPython 7.2.0 -- An enhanced Interactive Python. 考察 print文によるトレースからdef init() はアクセスされているが、def run(data)とdef data-gen(t=0) にはアクセスされていない。したがって、FuncAnimationオブジェクトとdef run(data)とdef data-gen(t=0)のデータの受け渡しがうまく機能していないことがわかります。 ここにより詳細な情報を記載してください。 残念ながらこれ以上のことは調べることができませんでした。
インデントがなくなっておりコードが実行できないので、質問欄を編集して、markdown 記法でコードを記述していただけますか
あなたの回答
tips
プレビュー