Python中的圖形繪制簡(jiǎn)單動(dòng)畫實(shí)操
前言:
Matplotlib 是一個(gè)非常廣泛的庫(kù),它也支持圖形動(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)在讓我們來(lái)逐段分析代碼:
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ù)來(lái)創(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)用來(lái)創(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)文章
Pytorch使用MNIST數(shù)據(jù)集實(shí)現(xiàn)基礎(chǔ)GAN和DCGAN詳解
今天小編就為大家分享一篇Pytorch使用MNIST數(shù)據(jù)集實(shí)現(xiàn)基礎(chǔ)GAN和DCGAN詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-01-01
Python實(shí)現(xiàn)在Word中創(chuàng)建表格并填入數(shù)據(jù)與圖片
在Word中,表格是一個(gè)強(qiáng)大的工具,本文主要為大家介紹了如何使用Python在Word中創(chuàng)建表格并填入數(shù)據(jù)、圖片,以及設(shè)置表格樣式等,感興趣的可以了解下2024-03-03
使用python 將圖片復(fù)制到系統(tǒng)剪貼中
今天小編就為大家分享一篇使用python 將圖片復(fù)制到系統(tǒng)剪貼中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-12-12
python中delattr刪除對(duì)象方法的代碼分析
在本篇文章里小編給大家分享了一篇關(guān)于python中delattr刪除對(duì)象方法的代碼分析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12
Python+tkinter實(shí)現(xiàn)音樂下載軟件的制作
平常我們下載的歌曲,都是各種妖魔鬼怪的格式橫行,想下載下來(lái)用一下都不行,還只能在它的播放器內(nèi)聽,這誰(shuí)受得了~本文就來(lái)用Python制作個(gè)音樂下載軟件,需要的可以參考一下2022-09-09

