小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

 東西二王 2019-05-16

全文共2153字,預(yù)計(jì)學(xué)習(xí)時(shí)長4分鐘或更長

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

用Matplotlib模擬雨

動(dòng)畫是呈現(xiàn)各種現(xiàn)象的有趣方式。在描述像過去幾年的股票價(jià)格、過去十年的氣候變化、季節(jié)性和趨勢等時(shí)間序列數(shù)據(jù)時(shí),與靜態(tài)圖相比,動(dòng)畫更能說明問題。因?yàn)?,從?dòng)畫中,我們可以看到特定參數(shù)是如何隨時(shí)間而變化的。

上圖是模擬雨的圖像。此圖由Matplotlib繪圖庫繪制而成,該繪圖庫常常被認(rèn)為是python可視化數(shù)據(jù)包的原始數(shù)據(jù)組。Matplotlib通過50個(gè)分散點(diǎn)的比例和不透明度來模擬雨滴落在平面上的情景。如今,Plotly、Bokeh、Altair等一系列可視化工具均為Python中強(qiáng)大的可視化工具。這些庫可實(shí)現(xiàn)最先進(jìn)的動(dòng)畫和交互動(dòng)作。但是,本文重點(diǎn)在于研究數(shù)據(jù)庫的一個(gè)方面——?jiǎng)赢?。同時(shí),我們也將關(guān)注實(shí)現(xiàn)動(dòng)畫的方法。

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

概述

Matplotlib是一個(gè) Python 的 2D繪圖庫,也是Python中最受歡迎的繪圖數(shù)據(jù)庫。大多數(shù)人在踏上數(shù)據(jù)可視化之旅時(shí),都是首選Matplotlib。這是因?yàn)樗珊唵蔚厣衫L圖,直方圖、功率譜、條形圖、錯(cuò)誤圖、散點(diǎn)圖等。不僅如此,它還無縫連接了Pandas、Seaborn等數(shù)據(jù)庫,甚至創(chuàng)建出更為復(fù)雜的可視化數(shù)據(jù)。

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

Matplotlib有幾大優(yōu)點(diǎn):

· 其構(gòu)造和MATLAB(矩陣實(shí)驗(yàn)室)類似,所以兩者間易于切換

· 包含許多后端渲染

· 可以快速生成繪圖

· 已存在數(shù)十年以上,因此,有大量的用戶基礎(chǔ)

但是,Matplotlib除了這些優(yōu)點(diǎn)之外,也有許多不足之處:

· Matplotlib常常不可避免地存在冗繁的API(應(yīng)用程序編程接口)

· 有時(shí)默認(rèn)樣式設(shè)計(jì)不如人意

· 對web和交互式圖表的支持較低

· 處理大型及復(fù)雜的數(shù)據(jù)時(shí)速度常常較慢

對于進(jìn)修者來說,Datacamp中有關(guān)于Matplotlib的必備知識可以幫助提高基礎(chǔ)知識。

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

動(dòng)畫

Matplotlib的動(dòng)畫基類負(fù)責(zé)處理動(dòng)畫部分。其可提供構(gòu)建動(dòng)畫功能的框架。有兩個(gè)主要接口來實(shí)現(xiàn)此功能:

FuncAnimation:通過反復(fù)觸發(fā)func.功能創(chuàng)建動(dòng)畫。

ArtistAnimation:利用已定義的Artist對象創(chuàng)建動(dòng)畫。

但是,在上述兩種接口中,F(xiàn)uncAnimation更為方便。我們專注于FuncAnimation工具的研究。


要求

· 安裝numpy和matplotlib模塊。

· 安裝符合要求的 ffmpeg 或imagemagick方可將動(dòng)畫以mp4或gif的形式儲(chǔ)存。

一切準(zhǔn)備就緒,就可以開始在JupyterNotebooks中制作第一個(gè)基本動(dòng)畫了。本文的訪問密碼可在GithubRepository中獲取。


基本動(dòng)畫:移動(dòng)的正弦波

在電腦中,利用FuncAnimation創(chuàng)建正弦波的基本動(dòng)畫。動(dòng)畫源代碼可在Matplotlib動(dòng)畫教程中獲取。先來看看輸出代碼,然后將其破譯,并了解其中奧妙。

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')

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

· 在第7行到第9行,簡單地創(chuàng)建一個(gè)圖形窗口,圖中只有一個(gè)軸。然后,創(chuàng)建無內(nèi)容的行對象,其本質(zhì)上是在動(dòng)畫中可修改的對象。稍后用數(shù)據(jù)來填充行對象。

· 在第11行到13行,創(chuàng)建init函數(shù),觸發(fā)動(dòng)畫發(fā)生。此函數(shù)初始化數(shù)據(jù),并限定軸范圍。

· 最后,在第14行到第18行,定義動(dòng)畫函數(shù),該函數(shù)以幀數(shù)(i)作為參數(shù),并創(chuàng)建一個(gè)正弦波(或任意其他的動(dòng)畫),而其移動(dòng)取決于i的值。此函數(shù)返回一個(gè)已修改的plot對象的元組,告知?jiǎng)赢嬁蚣躳lot中哪些部分需要?jiǎng)赢嫽?/p>

· 在第20 行,創(chuàng)建實(shí)際的動(dòng)畫對象。Blit參數(shù)確保只重新繪制已更改的部分。

· 這是在Matplolib中創(chuàng)建動(dòng)畫的基本知識。只需對代碼稍作調(diào)整,就可以創(chuàng)建出一些有趣的可視化。接下來看看其中一些可視化的例子吧。

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

一個(gè)不斷擴(kuò)大的線圈

同樣,在GreeksforGreeks中,有一個(gè)創(chuàng)建圖形的好例子。我們一起在animation模塊的幫助下創(chuàng)造一個(gè)緩慢展開的活動(dòng)線圈。該代碼和正弦波圖極為相似,只有一些小調(diào)整。

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')

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

實(shí)時(shí)更新圖

繪制股票數(shù)據(jù)、傳感器數(shù)據(jù)等其他與時(shí)間相關(guān)的動(dòng)態(tài)數(shù)據(jù)時(shí),實(shí)時(shí)更新圖就會(huì)派上用場。我們繪制一個(gè)基圖,在更多的數(shù)據(jù)被輸入系統(tǒng)后,基圖就會(huì)自動(dòng)更新。現(xiàn)在,來繪制某假定公司某月內(nèi)的股價(jià)圖。

#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()

現(xiàn)在,打開終端并運(yùn)行python文件,可以得到如下所示可自動(dòng)更新的圖表:

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

其更新的時(shí)間間隔是1000毫秒或一秒。

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

3D圖中的動(dòng)畫

創(chuàng)建3D圖形十分常見,但是如果可以將這些圖形視角動(dòng)畫化呢?其方法是,在改變相機(jī)視圖后,利用生成后的所有圖像來創(chuàng)建動(dòng)畫。而在PythonGraph Gallery(Python圖形庫)中有個(gè)專門的部分可以完成這類工作。

首先創(chuàng)建一個(gè)名為volcano的文件夾,放在與記事本相同的目錄中。然后,將所有會(huì)用于動(dòng)畫化的圖形儲(chǔ)存在該文件夾中。

# 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 =

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文件夾中創(chuàng)建多個(gè)PNG文件。接著,利用ImageMagick(一個(gè)創(chuàng)建、編輯、合成圖片的軟件)將這些PNG文件轉(zhuǎn)化成動(dòng)畫。打開終端并導(dǎo)向Volcano文件夾,輸入以下指令:

convert -delay 10 Volcano*.pnganimated_volcano.gif

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

利用Celluloid模塊動(dòng)畫化

Celluloid是python中的一個(gè)模塊,其在matplotlib中可簡化創(chuàng)建動(dòng)畫的進(jìn)程。這個(gè)庫創(chuàng)建一個(gè)matplotlib圖并從中創(chuàng)建相機(jī)。然后,重新啟用該圖,并在創(chuàng)建每幀動(dòng)畫后,用上述相機(jī)拍快照。最后,利用所有捕捉到的幀創(chuàng)建動(dòng)畫。

安裝

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')

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

子圖

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')

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

圖例

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')

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

創(chuàng)造生動(dòng)有趣的動(dòng)畫,Matplotlib庫大顯身手

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多