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

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

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

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

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

Q&A

解決済

1回答

1249閲覧

matplotlib.animation を使わずにアニメーションを保存する方法

Ryuking885

総合スコア3

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

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

0グッド

0クリップ

投稿2022/03/27 21:45

matplotlib.animation を使用せず、matplotlib.pyplot で描画しては消して、という方法でアニメーションを作成しました。
(実際のコードを見ていただいた方が意図を理解しやすいと思います。)

出力結果を動画として保存するにはどのようにすれば良いでしょうか。
そのような方法がありましたら、教えていただきたいです。

Python

1#!/usr/bin/env python3 2 3import matplotlib.pyplot as plt 4import numpy as np 5 6show_animation = True 7 8 9def update(x): 10 x[0] += 0.1 11 x[1] += 0.0 12 return x 13 14 15def main(): 16 print(__file__ + 'Start!') 17 18 # initialization 19 p_start = np.array([0.0, 2.0]) 20 p_goal = np.array([10.0, 2.0]) 21 22 x = np.array([0.0, 2.0]) 23 24 while True: 25 # update 26 x = update(x) 27 28 if show_animation: 29 plt.cla() 30 31 plt.gcf().gca().set_aspect('equal') 32 plt.grid(True) 33 plt.xlim(-0.5, 10.5) 34 plt.ylim(-0.5, 4.5) 35 36 plt.plot(p_start[0], p_start[1], '.r', markersize=12) 37 plt.plot(p_goal[0], p_goal[1], '.b', markersize=12) 38 39 plt.plot(x[0], x[1], '^g') 40 41 plt.pause(0.001) 42 43 if x[0] >= 10: 44 print('Goal!') 45 break 46 47 plt.show() 48 49 50if __name__ == '__main__': 51 main() 52

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

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

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

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

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

guest

回答1

0

ベストアンサー

imageio を使う方法です。

python

1#!/usr/bin/env python3 2 3import matplotlib.pyplot as plt 4import numpy as np 5import imageio 6import io 7 8show_animation = True 9 10def update(x): 11 x[0] += 0.1 12 x[1] += 0.0 13 return x 14 15def main(): 16 print(__file__ + 'Start!') 17 18 # initialization 19 p_start = np.array([0.0, 2.0]) 20 p_goal = np.array([10.0, 2.0]) 21 22 x = np.array([0.0, 2.0]) 23 anim = imageio.get_writer('anim.gif', mode='I', duration=0.1) 24 25 while True: 26 # update 27 x = update(x) 28 29 if show_animation: 30 plt.cla() 31 32 plt.gcf().gca().set_aspect('equal') 33 plt.grid(True) 34 plt.xlim(-0.5, 10.5) 35 plt.ylim(-0.5, 4.5) 36 37 plt.plot(p_start[0], p_start[1], '.r', markersize=12) 38 plt.plot(p_goal[0], p_goal[1], '.b', markersize=12) 39 40 plt.plot(x[0], x[1], '^g') 41 42 # append plot data to animation gif file 43 plt.gcf().canvas.draw() 44 img = np.frombuffer(plt.gcf().canvas.tostring_rgb(), dtype=np.uint8) 45 img = img.reshape(plt.gcf().canvas.get_width_height()[::-1] + (3,)) 46 anim.append_data(img) 47 48 plt.pause(0.001) 49 50 if x[0] >= 10: 51 print('Goal!') 52 break 53 54 plt.show() 55 anim.close() 56 57if __name__ == '__main__': 58 main()

イメージ説明

投稿2022/03/28 02:09

melian

総合スコア21118

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

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

Ryuking885

2022/03/31 18:11

非常に助かりました。ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問