Python matplotlib 動(dòng)畫繪制詳情
最最簡(jiǎn)單的操作
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)
有人會(huì)問(wèn),為什么不能直接 用 plot 替代 ax 呢?
好問(wèn)題,你可以一試,會(huì)發(fā)現(xiàn)這玩意沒(méi)法關(guān)掉 。。 當(dāng)然 ctrl + C等暴力手段是任何時(shí)候都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()
這就有兩個(gè)問(wèn)題需要解決一下
第一個(gè):line到底是什么類型的東西
type(line) <class 'list'>
明顯,這就是。。列表。
第二個(gè):set_data;set_xdata;set_ydata
你可以自己更改一下試試看,結(jié)果是顯而易見的
ArtistAnimation
它的好處是你不要費(fèi)盡心機(jī)去想一個(gè)可能 勾八 的函數(shù)了
它的壞處是 :
一個(gè)能用函數(shù)表示的動(dòng)畫 為什么要在新增一個(gè)列表才能表達(dá)呢?
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)在只需要保存動(dòng)畫就圓滿了
動(dòng)畫保存
.save()函數(shù)
filename | 畫文件名+后綴 |
fps | 動(dòng)畫每秒的幀數(shù) 默認(rèn)值為 原動(dòng)畫的幀數(shù) |
dpi | 動(dòng)畫每英寸的點(diǎn)數(shù) 默認(rèn)值為 原動(dòng)畫的點(diǎn)數(shù) |
codec | 編碼格式 默認(rèn)值為’h264’ |
filename畫文件名+后綴fps動(dòng)畫每秒的幀數(shù) 默認(rèn)值為 原動(dòng)畫的幀數(shù)dpi動(dòng)畫每英寸的點(diǎn)數(shù) 默認(rèn)值為 原動(dòng)畫的點(diǎn)數(shù)codec編碼格式 默認(rèn)值為’h264’
animation.save("1.gif")
到此這篇關(guān)于Python matplotlib 動(dòng)畫繪制的文章就介紹到這了,更多相關(guān)Python matplotlib 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python之pygame模塊實(shí)現(xiàn)飛機(jī)大戰(zhàn)完整代碼
這篇文章主要為大家詳細(xì)介紹了python之pygame模塊實(shí)現(xiàn)飛機(jī)大戰(zhàn)完整代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Python判斷兩個(gè)list是否是父子集關(guān)系的實(shí)例
今天小編就為大家分享一篇Python判斷兩個(gè)list是否是父子集關(guān)系的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-0516中Python機(jī)器學(xué)習(xí)類別特征處理方法總結(jié)
類別型特征(categorical?feature)主要是指職業(yè),血型等在有限類別內(nèi)取值的特征。在這篇文章中,小編將給大家分享一下16種類別特征處理方法,需要的可以參考一下2022-09-09Python 實(shí)現(xiàn)PS濾鏡中的徑向模糊特效
這篇文章主要介紹了Python 實(shí)現(xiàn) PS 濾鏡中的徑向模糊特效,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-12-12Python+Pygame實(shí)現(xiàn)神廟逃亡游戲
這篇文章主要為大家介紹了如何利用Python和Pygame動(dòng)畫制作一個(gè)神廟逃亡類似的小游戲。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-05-05