前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >matplotlib动画入门(1):基本概念

matplotlib动画入门(1):基本概念

作者头像
Stanley Sun
发布2019-09-23 15:17:51
7840
发布2019-09-23 15:17:51
举报

Matplotlib是python的一个图形库,它的动画功能基本上都是基于matplotlib.animation.Animation这个类来开发的。

matplotlib动画主要有两种方法,一种是基于时间的 TimedAnimation ,另一种是基于功能的FuncAnimation

TimedAnimation: 使用一系列的 Artist 对象.

FuncAnimation: 不断地重复调用func函数。

调用方法

matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs)

输入参数:

代码语言:javascript
复制
fig : matplotlib.figure.Figure
    The figure object that is used to get draw, resize, and any other needed events.
func : callable
    The function to call at each frame. The first argument will be the next value in frames. Any additional positional arguments can be supplied via the fargs parameter.
    The required signature is:  def func(frame, *fargs) -> iterable_of_artists:
frames : iterable, int, generator function, or None, optional
init_func : callable, optional
fargs : tuple or None, optional
    Additional arguments to pass to each call to func.
save_count : int, optional
    The number of values from frames to cache.

interval : number, optional
    Delay between frames in milliseconds. Defaults to 200.
repeat_delay : number, optional
    If the animation in repeated, adds a delay in milliseconds before repeating the animation. Defaults to None.
repeat : bool, optional
    Controls whether the animation should repeat when the sequence of frames is completed. Defaults to True.
blit : bool, optional
    Controls whether blitting is used to optimize drawing. Defaults to False.

一些重要概念

1. Figure 图像

matplotlib.figure.Figure类.一个画板上可以有多个Figure,每个Figure占一部分区域。比如要画4个图像,那么每个图像在画板上占四分之一的空间。每个Figure都有一个编号,这4个Figure的编号可以是1,2,3,4.

创建一个Figure的方法是:

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)

输入

代码语言:javascript
复制
num: 整型或字符串,表示figure的编号
figsize: tuple of integers。表示宽和高,单位:inch
dpi:分辨率
facecolor:背景颜色
edgecolor:边框颜色
frameon:是否画边框,默认true
FigureClass: matplotlib.figure.Figure的子类,用于实现自定义的Figure实例。
clear:是否清除已存在的Figure, 默认false

返回:

代码语言:javascript
复制
Figure: Figure实例

2. 坐标 Axes

matplotlib.axes.Axes类。Axes包含了大部分Figure元素,比如坐标轴(Axis)、记号(Tick)、二维线条(Line2D)、文本(Text)、多边形(polygon)等等,以及一系列的坐标系统。

在Figure中创建一个Axes的方法是:

matplotlib.pyplot.axes(arg=None, **kwargs)

输入

代码语言:javascript
复制
arg : None or 4-tuple or Axes
    None: A new full window axes is added using subplot(111, **kwargs)

    4-tuple of floats rect = [left, bottom, width, height]. A new axes is added with dimensions rect in normalized (0, 1) units using add_axes on the current figure.

    Axes: This is equivalent to pyplot.sca. It sets the current axes to arg. Note: This implicitly changes the current figure to the parent of arg.

返回:

代码语言:javascript
复制
Axes:一个Axes实例

Axes画图

matplotlib.axes.Axes.plot(*args, data=None, **kwargs),调用方法如下

代码语言:javascript
复制
plot([x], y, [fmt], data=None, **kwargs)
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
代码语言:javascript
复制
x, y : array-like or scalar,表示x和y坐标的数值
fmt : str,线条类型,比如‘ro’ 表示红色圆圈
data : indexable object,线条数据

返回

代码语言:javascript
复制
lines:A list of Line2D objects representing the plotted data.
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一些重要概念
    • 1. Figure 图像
      • 2. 坐标 Axes
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档