網站首頁 編程語言 正文
前言:
? ? ? Matplotlib
是一個非常廣泛的庫,它也支持圖形動畫。 動畫工具以 matplotlib.animation
基類為中心,它提供了一個框架,圍繞該框架構建動畫功能。 主要接口有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、輸出? ?
此圖為動畫截圖。
3?、代碼的部分解釋
?? ?現在讓我們來逐段分析代碼:
fig = plt.figure() ax = plt.axes(xlim=(-50, 50), ylim=(-50, 50)) line, = ax.plot([], [], lw=2)
- ?? ?1)首先創建一個圖形,即所有子圖的頂級容器。
- ?? ?2)然后創建一個軸元素 ax 作為子圖。 在創建軸元素時還定義了 x 和 y 軸的范圍/限制。
- ?? ?3)最后,創建名為 line, 的 plot 元素。 最初,x 和 y 軸點已定義為空列表,線寬 (lw) 已設置為 2。
def init(): ? ? line.set_data([], []) ? ? return line,
- ?? ?4)聲明一個初始化函數
init
。 動畫師調用此函數來創建第一幀。
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()
函數被動畫師一次又一次地調用來創建每一幀。 調用此函數的次數由幀數決定,該幀數作為幀參數傳遞給動畫師。 - ?? ?6)
animate()
函數以第 i 個幀的索引作為參數。
t = 0.1*i
- ?? ?7)我們巧妙地使用了當前幀的索引作為參數!
x = t*np.sin(t) y = t*np.cos(t)
- ?? ?8)由于有了參數 t,可以輕松地繪制任何參數方程。 例如,使用參數方程繪制螺旋線。
line.set_data(xdata, ydata) return line,
- ?? ?9)使用
set_data()
函數設置 x 和 y 數據,然后返回繪圖對象 line, 。
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=500, interval=20, blit=True)
- ?? ?10)創建
FuncAnimation
對象anim
。
它需要下面解釋的各種參數:
- ?? ?fig:要繪制的圖形。
- ?? ?animate:為每一幀重復調用的函數。
- ?? ?init_func:函數用于繪制清晰的框架。它在第一幀之前被調用一次。
- ?? ?frames:幀數。
- ?? ?interval:幀之間的持續時間。
- ?? ?blit:設置
blit=True
意味著只會繪制那些已經改變的部分。
原文鏈接:https://blog.51cto.com/u_14857544/4913835
相關推薦
- 2022-05-06 C#利用反射實現多數據庫訪問_C#教程
- 2022-05-04 python直接調用和使用swig法方調用c++庫_python
- 2023-01-26 Python+Sklearn實現異常檢測_python
- 2022-07-28 C++超詳細講解逗號操作符_C 語言
- 2022-11-02 Go?并發編程協程及調度機制詳情_Golang
- 2022-07-02 SpringBoot-?@SessionAttributes--使用/實例
- 2022-11-07 Python運算符之Inplace運算符的使用教程_python
- 2022-07-07 opencv學習筆記C++繪制灰度直方圖_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支