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

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

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

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

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

调用方法

matplotlib.animation.FuncAnimation(figfuncframes=Noneinit_func=Nonefargs=Nonesave_count=None**kwargs)

输入参数:

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=Nonefigsize=Nonedpi=Nonefacecolor=Noneedgecolor=Noneframeon=TrueFigureClass=<class 'matplotlib.figure.Figure'>clear=False**kwargs)

输入

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

返回:

Figure: Figure实例

2. 坐标 Axes

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

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

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

输入

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.

返回:

Axes:一个Axes实例

Axes画图

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

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

返回

lines:A list of Line2D objects representing the plotted data.

猜你喜欢

转载自my.oschina.net/stanleysun/blog/1807842