搜尋
首頁後端開發Python教學Python透過matplotlib簡單繪製動畫實例

Python透過matplotlib簡單繪製動畫實例

Dec 14, 2017 am 10:42 AM
matplotlibpython繪製

Matplotlib是一個Python的2D繪圖庫,它以各種硬拷貝格式和跨平台的互動式環境產生出版品質等級的圖形。透過Matplotlib,開發者可以只需要幾行程式碼,便可以產生繪圖,直方圖,功率譜,長條圖,錯誤圖,散佈圖等。本文主要介紹了Python透過matplotlib繪製動畫簡單實例,具有一定藉鏡價值,需要的朋友可以參考下,希望能幫助大家。

matplotlib從1.1.0版本以後就開始支援繪製動畫,具體使用可以參考官方說明文件。以下是一個很基本的範例:

"""
A simple example of an animated plot
"""
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
# create our line object which will be modified in the animation
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
# we simply plot an empty line: we'll add data to the line later
line, = ax.plot([], [], lw=2) 
# initialization function: plot the background of each frame
def init():
 line.set_data([], [])
 return line,
# animation function. This is called sequentially
# It takes a single parameter, the frame number i 
def animate(i):
 x = np.linspace(0, 2, 1000)
 y = np.sin(2 * np.pi * (x - 0.01 * i)) # update the data
 line.set_data(x, y)
 return line,
# Makes an animation by repeatedly calling a function func
# frames can be a generator, an iterable, or a number of frames.
# interval draws a new frame every interval milliseconds.
# blit=True means only re-draw the parts that have changed.
# 在这里设置一个200帧的动画,每帧之间间隔20毫秒
anim = animation.FuncAnimation(fig, animate, init_func=init,
        frames=200, interval=20, blit=True)
# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show() # plt.show() 会一直循环播放动画

結果:

如果要將動畫儲存為mp4格式的影片文件,則需要先安裝FFmpeg。 FFmpeg是一套可以用來錄製、轉換數位音頻、視頻,並能將其轉換為串流的開源電腦程式。採用LGPL或GPL授權。它提供了錄製、轉換以及串流影音的完整解決方案。

在這裡下載windows的版本:DownloadFFmpegforWindows,解壓縮,然後將bin目錄加入系統環境變數的路徑中。如:C:\ProgramFiles\ffmpeg-3.2.2-win64-static\bin。然後測試是否配置OK:輸入ffmpeg-version

相信大家對matplotlib又有進一步的了解,大家可以動手嘗試一下。

相關推薦:

python matplotlib座標軸設定的方法

matplotlib繪製符合論文要求的圖片

#python使用matplotlib繪圖詳解

#

以上是Python透過matplotlib簡單繪製動畫實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Python中有可能理解嗎?如果是,為什麼以及如果不是為什麼?Python中有可能理解嗎?如果是,為什麼以及如果不是為什麼?Apr 28, 2025 pm 04:34 PM

文章討論了由於語法歧義而導致的Python中元組理解的不可能。建議使用tuple()與發電機表達式使用tuple()有效地創建元組。 (159個字符)

Python中的模塊和包裝是什麼?Python中的模塊和包裝是什麼?Apr 28, 2025 pm 04:33 PM

本文解釋了Python中的模塊和包裝,它們的差異和用法。模塊是單個文件,而軟件包是帶有__init__.py文件的目錄,在層次上組織相關模塊。

Python中的Docstring是什麼?Python中的Docstring是什麼?Apr 28, 2025 pm 04:30 PM

文章討論了Python中的Docstrings,其用法和收益。主要問題:Docstrings對於代碼文檔和可訪問性的重要性。

什麼是lambda功能?什麼是lambda功能?Apr 28, 2025 pm 04:28 PM

文章討論了Lambda功能,與常規功能的差異以及它們在編程方案中的效用。並非所有語言都支持他們。

什麼是休息時間,繼續並通過python?什麼是休息時間,繼續並通過python?Apr 28, 2025 pm 04:26 PM

文章討論了休息,繼續並傳遞Python,並解釋了它們在控制循環執行和程序流中的作用。

Python的通行證是什麼?Python的通行證是什麼?Apr 28, 2025 pm 04:25 PM

本文討論了Python中的“ Pass”語句,該語句是函數和類等代碼結構中用作佔位符的空操作,允許在沒有語法錯誤的情況下實現將來實現。

我們可以在Python中傳遞作為參數的函數嗎?我們可以在Python中傳遞作為參數的函數嗎?Apr 28, 2025 pm 04:23 PM

文章討論了將功能作為Python中的參數,突出了模塊化和用例(例如分類和裝飾器)等好處。

Python中的 /和//有什麼區別?Python中的 /和//有什麼區別?Apr 28, 2025 pm 04:21 PM

文章在Python中討論 /和//運營商: / for for True Division,//用於地板部門。主要問題是了解它們的差異和用例。 Character數量:158

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器