使用Matplotlib制作動態(tài)圖的示例詳解
一、簡介
matplotlib(https://matplotlib.org/)是一個著名的python繪圖庫,由于其靈活強大的繪圖功能使得在python中可視化變得非常容易,關(guān)于matplotlib的基礎(chǔ)知識這里不再介紹,有疑問可以去官網(wǎng)翻Tutorials和example學習。由于我們實際使用時常常是繪制靜態(tài)圖,忽略了matplotlib的動態(tài)圖生成功能,同時matplotlib生成動態(tài)圖的功能不是非常友善,因此大部分人在真的需要制作動態(tài)圖時都會選擇先用matplotlib生成一系列靜態(tài)圖片,然后再用其它相對比較容易使用的第三方python庫生成動態(tài)圖,如imageio(https://imageio.readthedocs.io/en/stable/#), 或者使用其它工具,如Matlab。這里打算簡單介紹一下在matplotlib庫中制作動態(tài)圖的方法。
二、模塊簡介
matplotlib的animation模塊提供了動態(tài)圖制作功能,animation類提供了兩個方法來生成動態(tài)圖,即FuncAnimation和ArtistAnimation,這里我們使用FuncAnimation方法重復調(diào)用函數(shù)來生成圖片。
1. FuncAnimation類介紹
FuncAnimation類的主要參數(shù)包括:
fig: 每一幀畫面繪制使得Figure對象
func: 定義動畫每一幀的更新函數(shù),通常這一函數(shù)需要包含額外參數(shù),此時可以用functools.partial來生成。
frames:可以是可迭代對象,整數(shù),或者生成函數(shù)或者缺省。
init_func:初始化函數(shù)
inteval:每一幀畫面的停留時間
repeat:當動態(tài)圖中所有幀都播放完了之后是否重復播放
bilt:是否使用blitting來優(yōu)化繪圖
…
2. 定義動畫更新函數(shù)
在FunAnimation類中,更新函數(shù)在每一幀中都會被重新調(diào)用,通過在更新函數(shù)中更改一些繪圖函數(shù)的數(shù)據(jù),在每一幀我們就能得到不同的圖片,然后FunAnimation的Writer(后端)將這些圖片組合就能得到動態(tài)圖片。關(guān)于更新函數(shù)的一些需要注意的地方是:
如果設(shè)置了bilt == True,更新函數(shù)的最后就需要返回所有被修改或創(chuàng)建的Artists的引用變量
生成函數(shù)的第一個傳入?yún)?shù)必須是當前的幀數(shù),其具體值可以通過frames參數(shù)定義,可以是可迭代類型或整數(shù)
三、使用matplotlib制作動畫
1.一步法制作動態(tài)圖片
由于matplotlib本身自帶強大的繪圖功能,因此我們可以不用生成圖片,直接在初始繪圖的基礎(chǔ)上通過更新函數(shù)來修改繪圖數(shù)據(jù),一步直接生成動態(tài)圖片,方便快捷,以下是代碼:
import numpy as np
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
from functools import partial
### 繪制y=sin(2pi(x+t/t_0))*sin(2pi(t/t_0))
def getSinx_t(t=0, t_0=120, x_count=1e5):
x = np.linspace(0.0, 1.0, int(x_count))
y = np.sin(2.0*np.pi*(x + t/t_0))*np.sin(t/t_0*2.0*np.pi)
return x, y
### 圖片初始化
fig, ax = plt.subplots(dpi=100)
ax.set_aspect('auto')
ax.set_xlim((0.0, 1.0))
ax.set_ylim((-1.0, 1.0))
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title(r'$y=sin[2\pi(x+t/t_0)]*sin(2\pi t/t_0)$')
### 繪制初始曲線
x, y = getSinx_t()
y_up = y[np.where(y>0)]
x_up = x[np.where(y>0)]
x_dn = x[np.where(y<0)]
y_dn = y[np.where(y<0)]
plot_line = ax.plot(x, y)
plot_hline = ax.hlines(y=0.0, xmin=0.0, xmax=1.0, linestyles='dashed', colors='grey')
fill_xy_up = ax.fill_between(x=x_up, y1=y_up, y2=0, color='red', alpha=0.3)
fill_xy_dn = ax.fill_between(x=x_dn, y1=y_dn, y2=0, color='green', alpha=0.3)
plot_text = ax.text(x=0.8, y=0.75, s='t=0', fontsize=16, fontfamily='cursive')
### 定義動畫更新函數(shù)
def UpdateFigure(num, f_plot_line, f_fill_xy_up, f_fill_xy_dn, f_plot_text):
x_update, y_update = getSinx_t(t=num)
f_plot_line[0].set_data(x_update, y_update)
f_plot_text.set_text('t={}'.format(num))
x_up = x_update[np.where(y_update>0)]
y_up = y_update[np.where(y_update>0)]
xy_up1 = np.column_stack((x_up, y_up))
xy_up2 = np.column_stack((x_up[::-1], np.zeros(x_up.shape)))
x_dn = x_update[np.where(y_update<0)]
y_dn = y_update[np.where(y_update<0)]
xy_dn1 = np.column_stack((x_dn, y_dn))
xy_dn2 = np.column_stack((x_dn[::-1], np.zeros(x_dn.shape)))
f_fill_xy_up.set_verts([np.vstack((xy_up1, xy_up2))])
f_fill_xy_dn.set_verts([np.vstack((xy_dn1, xy_dn2))])
return [f_plot_line[0], f_fill_xy_up, f_fill_xy_dn, f_plot_text]
### 創(chuàng)建FunAnimation對象
ani = FuncAnimation(fig, partial(
UpdateFigure,
f_plot_line=plot_line,
f_fill_xy_up=fill_xy_up,
f_fill_xy_dn=fill_xy_dn,
f_plot_text=plot_text),
np.arange(120),
blit=True)
### 保存動態(tài)圖片
ani.save('sinxt.gif', fps=60) 以下為得到的動態(tài)圖片:

2. 兩步法制作動態(tài)圖片
所謂兩步法是指,首先用matplotlib生成一系列靜態(tài)圖片,然后結(jié)合matplotlib.image.imread讀取圖片功能和matplotlib.axes.Axes.imshow展示圖片功能,來動態(tài)地更新圖片,這種方法相比于上一種方法稍微復雜,但是這種方法靈活性更高,同時也可以用來組合一些非matplotlib生成的圖片。以下為代碼:
import matplotlib.pyplot as plt
import matplotlib.image as mimg
from matplotlib.animation import FuncAnimation
import numpy as np
from functools import partial
import os
### 繪制y=cos(2pi(x+t/t_0))*cos(2pi(t/t_0))
def getCosx_t(t=0, t_0=120, x_count=1e5):
x = np.linspace(0.0, 1.0, int(x_count))
y = np.cos(2.0*np.pi*(x + t/t_0))*np.cos(t/t_0*2.0*np.pi)
return x, y
fig_count = 120 # 圖片總數(shù)
### 定義生成所有圖片的函數(shù)
def getFigrues(fig_count):
try:
os.mkdir('fig')
except FileExistsError:
print("Dir Exist!")
for i in range(fig_count):
fig, ax = plt.subplots(dpi=100)
ax.set_aspect('auto')
ax.set_xlim((0.0, 1.0))
ax.set_ylim((-1.0, 1.0))
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title(r'$y=cos[2\pi(x+t/t_0)]*cos(2\pi t/t_0)$')
x, y = getCosx_t(t=i)
y_up = y[np.where(y>0)]
x_up = x[np.where(y>0)]
x_dn = x[np.where(y<0)]
y_dn = y[np.where(y<0)]
ax.plot(x, y)
ax.hlines(y=0.0, xmin=0.0, xmax=1.0, linestyles='dashed', colors='grey')
ax.fill_between(x=x_up, y1=y_up, y2=0, color='red', alpha=0.3)
ax.fill_between(x=x_dn, y1=y_dn, y2=0, color='green', alpha=0.3)
ax.text(x=0.8, y=0.75, s='t={}'.format(i), fontsize=16, fontfamily='cursive')
fig.show(False)
fig.savefig('./fig/{}.jpg'.format(i))
plt.close(fig)
getFigrues(fig_count)
### 讀取圖片尺寸
def GetFigSize(fig_path='./fig/0.jpg'):
now_img = mimg.imread(fname=fig_path)
img_pxy = now_img.shape
return img_pxy[1], img_pxy[0]
### 繪圖初始化
img_px, img_py = GetFigSize()
img_dpi=100
fig, ax = plt.subplots(figsize=[img_px/img_dpi, img_py/img_dpi], dpi=img_dpi)
ax.set_aspect('equal')
ax.set_position([0.0, 0.0, 1.0, 1.0])
ax.set_axis_off()
plot_img = ax.imshow(X=np.zeros((img_py, img_px, 3)))
### 定義動畫更新函數(shù)
def UpdateImages(num, f_plot_img):
now_img_path = './fig/{}.jpg'.format(num)
now_img = mimg.imread(fname=now_img_path)
f_plot_img.set_data(now_img)
return [f_plot_img]
### 創(chuàng)建FunAnimation對象
ani = FuncAnimation(
fig, partial(UpdateImages, f_plot_img=plot_img),
np.arange(fig_count),
blit=True)
### 保存動態(tài)圖片
ani.save('cosxt.gif', fps=60) 得到的動態(tài)圖片:

到此這篇關(guān)于使用Matplotlib制作動態(tài)圖的示例詳解的文章就介紹到這了,更多相關(guān)Matplotlib動態(tài)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PyQt5連接MySQL及QMYSQL driver not loaded錯誤解決
這篇文章主要介紹了PyQt5連接MySQL及QMYSQL driver not loaded錯誤解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
Python自動化測試之RPC協(xié)議接口測試實戰(zhàn)詳解
RPC 協(xié)議接口測試通過自動化工具和框架可顯著提升效率,適合分布式系統(tǒng)和微服務(wù)場景,下面小編就來和大家詳細講講Python如何利用RPC 協(xié)議進行接口測試吧2025-07-07
Python自動化實現(xiàn)Word到圖片的轉(zhuǎn)換指南
在日常工作中,我們經(jīng)常會遇到需要將Word文檔內(nèi)容以圖片形式展示的場景,本文將深入探討如何利用Spire.Doc for Python這一庫,將Word文檔輕松轉(zhuǎn)換為高質(zhì)量圖片,感興趣的小伙伴可以了解下2026-01-01
Python實現(xiàn)樹莓派攝像頭持續(xù)錄像并傳送到主機的步驟
這篇文章主要介紹了Python實現(xiàn)樹莓派攝像頭持續(xù)錄像并傳送到主機的步驟,幫助大家更好的理解和學習python,感興趣的朋友可以了解下2020-11-11

