Python matplotlib 動畫繪制詳情
最最簡單的操作
import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.subplots() x = np.linspace(0,10,100) y = np.sin(x) while True: ax.plot(x,y) plt.pause(1) ax.cla() x += np.pi/30 y = np.sin(x)
有人會問,為什么不能直接 用 plot 替代 ax 呢?
好問題,你可以一試,會發(fā)現(xiàn)這玩意沒法關(guān)掉 。。 當(dāng)然 ctrl + C等暴力手段是任何時候都o(jì)k的
Animation類
FuncAnimation
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation fig = plt.figure() ax = fig.subplots() x = np.linspace(0,10,100) y = np.sin(x) ax.set_aspect(3) ax.plot(x,y,'-.',c='red',label="the old one") line = ax.plot(x,y,c='green') plt.legend() def fun(i): global x x += 0.1 y = np.sin(x) line[0].set_ydata(y) return line animation = FuncAnimation(fig,fun,interval=100) plt.show()
這就有兩個問題需要解決一下
第一個:line到底是什么類型的東西
type(line) <class 'list'>
明顯,這就是。。列表。
第二個:set_data;set_xdata;set_ydata
你可以自己更改一下試試看,結(jié)果是顯而易見的
ArtistAnimation
它的好處是你不要費盡心機去想一個可能 勾八 的函數(shù)了
它的壞處是 :
一個能用函數(shù)表示的動畫 為什么要在新增一個列表才能表達呢?
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import ArtistAnimation fig = plt.figure() ax = fig.subplots() frames = [] x = np.linspace(0,np.pi*2,10) for i in range(20): x += np.pi*2/20 y = np.sin(x) frames.append(ax.plot(y,'-.',c='red')) animation = ArtistAnimation(fig,frames,interval=100) plt.show()
很好!現(xiàn)在只需要保存動畫就圓滿了
動畫保存
.save()函數(shù)
filename | 畫文件名+后綴 |
fps | 動畫每秒的幀數(shù) 默認(rèn)值為 原動畫的幀數(shù) |
dpi | 動畫每英寸的點數(shù) 默認(rèn)值為 原動畫的點數(shù) |
codec | 編碼格式 默認(rèn)值為’h264’ |
filename畫文件名+后綴fps動畫每秒的幀數(shù) 默認(rèn)值為 原動畫的幀數(shù)dpi動畫每英寸的點數(shù) 默認(rèn)值為 原動畫的點數(shù)codec編碼格式 默認(rèn)值為’h264’
animation.save("1.gif")
到此這篇關(guān)于Python matplotlib 動畫繪制的文章就介紹到這了,更多相關(guān)Python matplotlib 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python之pygame模塊實現(xiàn)飛機大戰(zhàn)完整代碼
這篇文章主要為大家詳細(xì)介紹了python之pygame模塊實現(xiàn)飛機大戰(zhàn)完整代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11Python判斷兩個list是否是父子集關(guān)系的實例
今天小編就為大家分享一篇Python判斷兩個list是否是父子集關(guān)系的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-0516中Python機器學(xué)習(xí)類別特征處理方法總結(jié)
類別型特征(categorical?feature)主要是指職業(yè),血型等在有限類別內(nèi)取值的特征。在這篇文章中,小編將給大家分享一下16種類別特征處理方法,需要的可以參考一下2022-09-09