Python中的圖形繪制簡(jiǎn)單動(dòng)畫實(shí)操
前言:
Matplotlib
是一個(gè)非常廣泛的庫,它也支持圖形動(dòng)畫。 動(dòng)畫工具以 matplotlib.animation
基類為中心,它提供了一個(gè)框架,圍繞該框架構(gòu)建動(dòng)畫功能。 主要接口有TimedAnimation
和FuncAnimation
,兩者中FuncAnimation
是最方便使用的。
1、畫螺旋曲線代碼
import matplotlib.pyplot as plt import matplotlib.animation as animation import numpy as np ?? # create a figure, axis and plot element fig = plt.figure() ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50)) line, = ax.plot([], [], lw=2) ?? # initialization function def init(): ? ? # creating an empty plot/frame ? ? line.set_data([], []) ? ? return line, ?? # lists to store x and y axis points xdata, ydata = [], [] ?? # animation function def animate(i): ? ? # t is a parameter ? ? t = 0.1*i ? ? ?? ? ? # x, y values to be plotted ? ? x = t*np.sin(t) ? ? y = t*np.cos(t) ? ? ?? ? ? # appending new points to x, y axes points list ? ? xdata.append(x) ? ? ydata.append(y) ? ? ?? ? ? # set/update the x and y axes data ? ? line.set_data(xdata, ydata) ? ? ?? ? ? # return line object ? ? return line, ? ? ?? # setting a title for the plot plt.title('A growing coil!') # hiding the axis details plt.axis('off') ?? # call the animator ? ? anim = animation.FuncAnimation(fig, animate, init_func=init, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?frames=500, interval=20, blit=True) ?? # save the animation as mp4 video file anim.save('animated_coil.mp4', writer = 'ffmpeg', fps = 30) ?? # show the plot plt.show()
2、輸出? ?
此圖為動(dòng)畫截圖。
3?、代碼的部分解釋
現(xiàn)在讓我們來逐段分析代碼:
fig = plt.figure() ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50)) line, = ax.plot([], [], lw=2)
- 1)首先創(chuàng)建一個(gè)圖形,即所有子圖的頂級(jí)容器。
- 2)然后創(chuàng)建一個(gè)軸元素 ax 作為子圖。 在創(chuàng)建軸元素時(shí)還定義了 x 和 y 軸的范圍/限制。
- 3)最后,創(chuàng)建名為 line, 的 plot 元素。 最初,x 和 y 軸點(diǎn)已定義為空列表,線寬 (lw) 已設(shè)置為 2。
def init(): ? ? line.set_data([], []) ? ? return line,
- 4)聲明一個(gè)初始化函數(shù)
init
。 動(dòng)畫師調(diào)用此函數(shù)來創(chuàng)建第一幀。
def animate(i): ? ? # t is a parameter ? ? t = 0.1*i ? ? # x, y values to be plotted ? ? x = t*np.sin(t) ? ? y = t*np.cos(t) ? ? # appending new points to x, y axes points list ? ? xdata.append(x) ? ? ydata.append(y) ? ?? ? ? # set/update the x and y axes data ? ? line.set_data(xdata, ydata) ? ? # return line object ? ? return line,
- 5)這是上述程序最重要的功能。
animate()
函數(shù)被動(dòng)畫師一次又一次地調(diào)用來創(chuàng)建每一幀。 調(diào)用此函數(shù)的次數(shù)由幀數(shù)決定,該幀數(shù)作為幀參數(shù)傳遞給動(dòng)畫師。 - 6)
animate()
函數(shù)以第 i 個(gè)幀的索引作為參數(shù)。
t = 0.1*i
- 7)我們巧妙地使用了當(dāng)前幀的索引作為參數(shù)!
x = t*np.sin(t) y = t*np.cos(t)
- 8)由于有了參數(shù) t,可以輕松地繪制任何參數(shù)方程。 例如,使用參數(shù)方程繪制螺旋線。
line.set_data(xdata, ydata) return line,
- 9)使用
set_data()
函數(shù)設(shè)置 x 和 y 數(shù)據(jù),然后返回繪圖對(duì)象 line, 。
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=500, interval=20, blit=True)
- 10)創(chuàng)建
FuncAnimation
對(duì)象anim
。
它需要下面解釋的各種參數(shù):
- fig:要繪制的圖形。
- animate:為每一幀重復(fù)調(diào)用的函數(shù)。
- init_func:函數(shù)用于繪制清晰的框架。它在第一幀之前被調(diào)用一次。
- frames:幀數(shù)。
- interval:幀之間的持續(xù)時(shí)間。
- blit:設(shè)置
blit=True
意味著只會(huì)繪制那些已經(jīng)改變的部分。
到此這篇關(guān)于Python中的圖形繪制簡(jiǎn)單動(dòng)畫實(shí)操的文章就介紹到這了,更多相關(guān)Python
中的圖形繪制動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django基于Models定制Admin后臺(tái)實(shí)現(xiàn)過程解析
這篇文章主要介紹了Django基于Models定制Admin后臺(tái)實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11python實(shí)現(xiàn)商品進(jìn)銷存管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)商品進(jìn)銷存管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05python利用socketserver實(shí)現(xiàn)并發(fā)套接字功能
這篇文章主要為大家詳細(xì)介紹了python利用socketserver實(shí)現(xiàn)并發(fā)套接字功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01python實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
這篇文章主要為大家詳細(xì)介紹了使用python實(shí)現(xiàn)微信自動(dòng)回復(fù)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Pandas分組聚合之使用自定義函數(shù)方法transform()、apply()
Pandas具有很多強(qiáng)大的功能,transform就是其中之一,利用它可以高效地匯總數(shù)據(jù)且不改變數(shù)據(jù)行數(shù),下面這篇文章主要給大家介紹了關(guān)于Pandas分組聚合之使用自定義函數(shù)方法transform()、apply()的相關(guān)資料,需要的朋友可以參考下2023-01-01一篇文章學(xué)會(huì)兩種將python打包成exe的方式
最近有部分小伙伴問我,python 寫的項(xiàng)目可不可以打包成exe程序,放到?jīng)]有python環(huán)境上的電腦中執(zhí)行? 答案當(dāng)然是可以的,下面這篇文章主要給大家介紹了如何通過一篇文章學(xué)會(huì)兩種將pyton打包成exe的方式,需要的朋友可以參考下2021-11-11Python實(shí)現(xiàn)的微信紅包提醒功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的微信紅包提醒功能,結(jié)合實(shí)例形式分析了Python使用微信模塊itchat實(shí)現(xiàn)微信紅包提醒操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08