全文共2153字,預計學習時長4分鐘或更長
用Matplotlib模擬雨
動畫是呈現各種現象的有趣方式。在描述像過去幾年的股票價格、過去十年的氣候變化、季節性和趨勢等時間序列資料時,與靜態圖相比,動畫更能說明問題。因為,從動畫中,我們可以看到特定引數是如何隨時間而變化的。
上圖是模擬雨的影象。此圖由Matplotlib繪相簿繪製而成,該繪相簿常常被認為是python視覺化資料包的原始資料組。Matplotlib透過50個分散點的比例和不透明度來模擬雨滴落在平面上的情景。如今,Plotly、Bokeh、Altair等一系列視覺化工具均為Python中強大的視覺化工具。這些庫可實現最先進的動畫和互動動作。但是,本文重點在於研究資料庫的一個方面——動畫。同時,我們也將關注實現動畫的方法。
概述
Matplotlib是一個 Python 的 2D繪相簿,也是Python中最受歡迎的繪圖資料庫。大多數人在踏上資料視覺化之旅時,都是首選Matplotlib。這是因為它可簡單地生成繪圖,直方圖、功率譜、條形圖、錯誤圖、散點圖等。不僅如此,它還無縫連線了Pandas、Seaborn等資料庫,甚至創建出更為複雜的視覺化資料。
Matplotlib有幾大優點:
· 其構造和MATLAB(矩陣實驗室)類似,所以兩者間易於切換
· 包含許多後端渲染
· 可以快速生成繪圖
· 已存在數十年以上,因此,有大量的使用者基礎
但是,Matplotlib除了這些優點之外,也有許多不足之處:
· Matplotlib常常不可避免地存在冗繁的API(應用程式程式設計介面)
· 有時預設樣式設計不如人意
· 對web和互動式圖表的支援較低
· 處理大型及複雜的資料時速度常常較慢
對於進修者來說,Datacamp中有關於Matplotlib的必備知識可以幫助提高基礎知識。
動畫
Matplotlib的動畫基類負責處理動畫部分。其可提供構建動畫功能的框架。有兩個主要介面來實現此功能:
FuncAnimation:透過反覆觸發func。功能建立動畫。
ArtistAnimation:利用已定義的Artist物件建立動畫。
但是,在上述兩種介面中,FuncAnimation更為方便。我們專注於FuncAnimation工具的研究。
要求
· 安裝numpy和matplotlib模組。
· 安裝符合要求的 ffmpeg 或imagemagick方可將動畫以mp4或gif的形式儲存。
一切準備就緒,就可以開始在JupyterNotebooks中製作第一個基本動畫了。本文的訪問密碼可在GithubRepository中獲取。
基本動畫:移動的正弦波
在電腦中,利用FuncAnimation建立正弦波的基本動畫。動畫原始碼可在Matplotlib動畫教程中獲取。先來看看輸出程式碼,然後將其破譯,並瞭解其中奧妙。
import numpy as np
from matplotlib import pyplot as plt
from matplotlib。animation import FuncAnimation
plt。style。use(‘seaborn-pastel’)
fig = plt。figure()
ax = plt。axes(xlim=(0, 4), ylim=(-2, 2))
line, = ax。plot([], [], lw=3)
def init():
line。set_data([], [])
return line,
def animate(i):
x = np。linspace(0, 4, 1000)
y = np。sin(2 * np。pi * (x - 0。01 * i))
line。set_data(x, y)
return line,
anim = FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
anim。save(‘sine_wave。gif’, writer=‘imagemagick’)
· 在第7行到第9行,簡單地建立一個圖形視窗,圖中只有一個軸。然後,建立無內容的行物件,其本質上是在動畫中可修改的物件。稍後用資料來填充行物件。
· 在第11行到13行,建立init函式,觸發動畫發生。此函式初始化資料,並限定軸範圍。
· 最後,在第14行到第18行,定義動畫函式,該函式以幀數(i)作為引數,並建立一個正弦波(或任意其他的動畫),而其移動取決於i的值。此函式返回一個已修改的plot物件的元組,告知動畫框架plot中哪些部分需要動畫化。
· 在第20 行,建立實際的動畫物件。Blit引數確保只重新繪製已更改的部分。
· 這是在Matplolib中建立動畫的基本知識。只需對程式碼稍作調整,就可以創建出一些有趣的視覺化。接下來看看其中一些視覺化的例子吧。
一個不斷擴大的線圈
同樣,在GreeksforGreeks中,有一個建立圖形的好例子。我們一起在animation模組的幫助下創造一個緩慢展開的活動線圈。該程式碼和正弦波圖極為相似,只有一些小調整。
import matplotlib。pyplot as plt
import matplotlib。animation as animation
import numpy as np
plt。style。use(‘dark_background’)
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)
line。set_data(xdata, ydata)
return line,
# setting a title for the plot
plt。title(‘Creating a growing coil with matplotlib!’)
# 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(‘coil。gif’,writer=‘imagemagick’)
實時更新圖
繪製股票資料、感測器資料等其他與時間相關的動態資料時,實時更新圖就會派上用場。我們繪製一個基圖,在更多的資料被輸入系統後,基圖就會自動更新。現在,來繪製某假定公司某月內的股價圖。
#importing libraries
import matplotlib。pyplot as plt
import matplotlib。animation as animation
fig = plt。figure()
#creating a subplot
ax1 = fig。add_subplot(1,1,1)
def animate(i):
data = open(‘stock。txt’,‘r’)。read()
lines = data。split(‘\n’)
xs = []
ys = []
for line in lines:
x, y = line。split(‘,’) # Delimiter is comma
xs。append(float(x))
ys。append(float(y))
ax1。clear()
ax1。plot(xs, ys)
plt。xlabel(‘Date’)
plt。ylabel(‘Price’)
plt。title(‘Live graph with matplotlib’)
ani = animation。FuncAnimation(fig, animate, interval=1000)
plt。show()
現在,開啟終端並執行python檔案,可以得到如下所示可自動更新的圖表:
其更新的時間間隔是1000毫秒或一秒。
3D圖中的動畫
建立3D圖形十分常見,但是如果可以將這些圖形視角動畫化呢?其方法是,在改變相機檢視後,利用生成後的所有影象來建立動畫。而在PythonGraph Gallery(Python圖形庫)中有個專門的部分可以完成這類工作。
首先建立一個名為volcano的資料夾,放在與記事本相同的目錄中。然後,將所有會用於動畫化的圖形儲存在該資料夾中。
# library
from mpl_toolkits。mplot3d import Axes3D
import matplotlib。pyplot as plt
import pandas as pd
import seaborn as sns
# Get the data (csv file is hosted on the web)
url = ‘https://python-graph-gallery。com/wp-content/uploads/volcano。csv’
data = pd。read_csv(url)
# Transform it to a long format
df=data。unstack()。reset_index()
df。columns=[“X”,“Y”,“Z”]
# And transform the old column name in something numeric
df[‘X’]=pd。Categorical(df[‘X’])
df[‘X’]=df[‘X’]。cat。codes
# We are going to do 20 plots, for 20 different angles
for angle in range(70,210,2):
# Make the plot
fig = plt。figure()
ax = fig。gca(projection=‘3d’)
ax。plot_trisurf(df[‘Y’], df[‘X’], df[‘Z’], cmap=plt。cm。viridis, linewidth=0。2)
ax。view_init(30,angle)
filename=‘Volcano/Volcano_step’+str(angle)+‘。png’
plt。savefig(filename, dpi=96)
plt。gca()
這樣就可以在Volcano資料夾中建立多個PNG檔案。接著,利用ImageMagick(一個建立、編輯、合成圖片的軟體)將這些PNG檔案轉化成動畫。開啟終端並導向Volcano資料夾,輸入以下指令:
convert -delay 10 Volcano*。pnganimated_volcano。gif
利用Celluloid模組動畫化
Celluloid是python中的一個模組,其在matplotlib中可簡化建立動畫的程序。這個庫建立一個matplotlib圖並從中建立相機。然後,重新啟用該圖,並在建立每幀動畫後,用上述相機拍快照。最後,利用所有捕捉到的幀建立動畫。
安裝
pip install celluloid
下面是利用Celluloid模組的例子:
極小值
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt。figure()
camera = Camera(fig)
for i in range(10):
plt。plot([i] * 10)
camera。snap()
animation = camera。animate()
animation。save(‘celluloid_minimal。gif’, writer = ‘imagemagick’)
子圖
import numpy as np
from matplotlib import pyplot as plt
from celluloid import Camera
fig, axes = plt。subplots(2)
camera = Camera(fig)
t = np。linspace(0, 2 * np。pi, 128, endpoint=False)
for i in t:
axes[0]。plot(t, np。sin(t + i), color=‘blue’)
axes[1]。plot(t, np。sin(t - i), color=‘blue’)
camera。snap()
animation = camera。animate()
animation。save(‘celluloid_subplots。gif’, writer = ‘imagemagick’)
圖例
import matplotlib
from matplotlib import pyplot as plt
from celluloid import Camera
fig = plt。figure()
camera = Camera(fig)
for i in range(20):
t = plt。plot(range(i, i + 5))
plt。legend(t, [f‘line {i}’])
camera。snap()
animation = camera。animate()
animation。save(‘celluloid_legends。gif’, writer = ‘imagemagick’)
留言 點贊 關注
我們一起分享AI學習與發展的乾貨
歡迎關注全平臺AI垂類自媒體 “讀芯術”