分享

创造生动有趣的动画,Matplotlib库大显身手

 东西二王 2019-05-16

全文共2153字,预计学习时长4分钟或更长

创造生动有趣的动画,Matplotlib库大显身手

用Matplotlib模拟雨

动画是呈现各种现象的有趣方式。在描述像过去几年的股票价格、过去十年的气候变化、季节性和趋势等时间序列数据时,与静态图相比,动画更能说明问题。因为,从动画中,我们可以看到特定参数是如何随时间而变化的。

上图是模拟雨的图像。此图由Matplotlib绘图库绘制而成,该绘图库常常被认为是python可视化数据包的原始数据组。Matplotlib通过50个分散点的比例和不透明度来模拟雨滴落在平面上的情景。如今,Plotly、Bokeh、Altair等一系列可视化工具均为Python中强大的可视化工具。这些库可实现最先进的动画和交互动作。但是,本文重点在于研究数据库的一个方面——动画。同时,我们也将关注实现动画的方法。

创造生动有趣的动画,Matplotlib库大显身手

概述

Matplotlib是一个 Python 的 2D绘图库,也是Python中最受欢迎的绘图数据库。大多数人在踏上数据可视化之旅时,都是首选Matplotlib。这是因为它可简单地生成绘图,直方图、功率谱、条形图、错误图、散点图等。不仅如此,它还无缝连接了Pandas、Seaborn等数据库,甚至创建出更为复杂的可视化数据。

创造生动有趣的动画,Matplotlib库大显身手

Matplotlib有几大优点:

· 其构造和MATLAB(矩阵实验室)类似,所以两者间易于切换

· 包含许多后端渲染

· 可以快速生成绘图

· 已存在数十年以上,因此,有大量的用户基础

但是,Matplotlib除了这些优点之外,也有许多不足之处:

· Matplotlib常常不可避免地存在冗繁的API(应用程序编程接口)

· 有时默认样式设计不如人意

· 对web和交互式图表的支持较低

· 处理大型及复杂的数据时速度常常较慢

对于进修者来说,Datacamp中有关于Matplotlib的必备知识可以帮助提高基础知识。

创造生动有趣的动画,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')

创造生动有趣的动画,Matplotlib库大显身手

· 在第7行到第9行,简单地创建一个图形窗口,图中只有一个轴。然后,创建无内容的行对象,其本质上是在动画中可修改的对象。稍后用数据来填充行对象。

· 在第11行到13行,创建init函数,触发动画发生。此函数初始化数据,并限定轴范围。

· 最后,在第14行到第18行,定义动画函数,该函数以帧数(i)作为参数,并创建一个正弦波(或任意其他的动画),而其移动取决于i的值。此函数返回一个已修改的plot对象的元组,告知动画框架plot中哪些部分需要动画化。

· 在第20 行,创建实际的动画对象。Blit参数确保只重新绘制已更改的部分。

· 这是在Matplolib中创建动画的基本知识。只需对代码稍作调整,就可以创建出一些有趣的可视化。接下来看看其中一些可视化的例子吧。

创造生动有趣的动画,Matplotlib库大显身手

一个不断扩大的线圈

同样,在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')

创造生动有趣的动画,Matplotlib库大显身手

实时更新图

绘制股票数据、传感器数据等其他与时间相关的动态数据时,实时更新图就会派上用场。我们绘制一个基图,在更多的数据被输入系统后,基图就会自动更新。现在,来绘制某假定公司某月内的股价图。

#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文件,可以得到如下所示可自动更新的图表:

创造生动有趣的动画,Matplotlib库大显身手

其更新的时间间隔是1000毫秒或一秒。

创造生动有趣的动画,Matplotlib库大显身手

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 =

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

创造生动有趣的动画,Matplotlib库大显身手

创造生动有趣的动画,Matplotlib库大显身手

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

创造生动有趣的动画,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')

创造生动有趣的动画,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')

创造生动有趣的动画,Matplotlib库大显身手

创造生动有趣的动画,Matplotlib库大显身手

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多