基础的例子我在上一篇FuncAnimation讲过了。但是如果你想创造自己的图表,看完这一篇可以让你游刃有余。
首先,你得知道一开始展开一张图,可以用以下三个方法
plt.plot(x, y)
ax = plt.subplot()
ax.plot(x, y)
进行一步扩展
tupian,zhou=plt.subplot(1,2)
figure = plt.figure(figsize=(8,4))
newplot = figure.add_subplot(111)
newplot.plot(x, y)
(1)plt.plot和plt.subplots()难道不是一样的吗?
(2)通过他们的返回值你会发现。plot返回的是列表。而subplots返回的是figure以及一个axes。
(3)所以tupian,zhou=plt.subplot()的意思就是把figure赋值给tupian,把axes赋值给zhou
(4)所以答案就是,plot返回的是一个列表,figure返回的是一个框架,axes返回的是轴(画布与轴相同)。axes=canvas≠figure
1. figure你可以改变框架的大小,性状,但是你不能在上面画画。一个py文件可以有很多figure,一个figure可以有很多subplots(这里subplot的意思是axes)
2. 而axes则是你的画布,或者说,它就是可以绘制和保存数据的空白布。一个轴对象只能属于一个图形。
所以你又要问了,知道这些有什么用?因为你要调用不同的函数。
如果是figure框架,你可以用这些函数:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html
如果是axes画布,你可以用这些函数:https://matplotlib.org/stable/api/axes_api.html
注意,如果你要加入下面的元素,你需要用最后一种方法。
https://matplotlib.org/stable/api/index.html
https://matplotlib.org/stable/api/patches_api.html
Arc (xy, width, height[, angle, theta1, theta2]) |
An elliptical arc, i.e. a segment of an ellipse. |
Arrow (x, y, dx, dy[, width]) |
An arrow patch. |
ArrowStyle (stylename, **kw) |
ArrowStyle is a container class which defines several arrowstyle classes, which is used to create an arrow path along a given path. |
BoxStyle (stylename, **kw) |
BoxStyle is a container class which defines several boxstyle classes, which are used for FancyBboxPatch . |
Circle (xy[, radius]) |
A circle patch. |
CirclePolygon (xy[, radius, resolution]) |
A polygon-approximation of a circle patch. |
ConnectionPatch (xyA, xyB, coordsA[, ...]) |
A patch that connects two points (possibly in different axes). |
ConnectionStyle (stylename, **kw) |
ConnectionStyle is a container class which defines several connectionstyle classes, which is used to create a path between two points. |
Ellipse (xy, width, height[, angle]) |
A scale-free ellipse. |
FancyArrow (x, y, dx, dy[, width, ...]) |
Like Arrow, but lets you set head width and head height independently. |
FancyArrowPatch ([posA, posB, path, ...]) |
A fancy arrow patch. |
FancyBboxPatch (xy, width, height[, ...]) |
A fancy box around a rectangle with lower left at xy = (x, y) with specified width and height. |
Patch ([edgecolor, facecolor, color, ...]) |
A patch is a 2D artist with a face color and an edge color. |
PathPatch (path, **kwargs) |
A general polycurve path patch. |
StepPatch (values, edges, *[, orientation, ...]) |
A path patch describing a stepwise constant function. |
Polygon (xy[, closed]) |
A general polygon patch. |
Rectangle (xy, width, height[, angle]) |
A rectangle defined via an anchor point xy and its width and height. |
RegularPolygon (xy, numVertices[, radius, ...]) |
A regular polygon patch. |
Shadow (patch, ox, oy[, props]) |
Create a shadow of the given patch. |
Wedge (center, r, theta1, theta2[, width]) |
Wedge shaped patch. |
https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Rectangle.html#matplotlib.patches.Rectangle
具体的调用例子如下:
1. dian=plt.Rectangle((1, 8), 0.6, 0.3, color='b', alpha=0.8) 是把长方形加入画布, 并赋值给dian。这里plt.Rectangle是plt的函数。
2. dian.set_x(x1[num])函数是每更新一个num,长方形x轴的位置发生改变。这个函数是长方形自己的函数,你可以在上面的网址上找到
3. dian.set_width(width[num])函数是每更新一个num,长方形的长度发生改变。这个函数也是长方形自己的函数
4. 把这些长方形自己的函数写入动画FuncAnimation中的更新函数中,就可以每一帧改变长方形的参数。
1. 比如我的这个图里更新的1是长方形的位置。2是长方形的长度。
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#背景x,y的数据
x = [1,2,3,4,5,6,7,8]
y = [2,9,8,4,6,3,2,7]
#长方形的x,y的位置。这里y轴位置不变,x轴持续往前。
x1 = np.linspace(1, 8, 30)
y1 = 8
#这里设定的是长方形的长度。
width=np.linspace(0, 1, 30)
2. 这里的dian代表的是长方形,
dian=plt.Rectangle((1, 8), 0.6, 0.3, color='b', alpha=0.8) 是把长方形加入画布, 并赋值给dian
dian.set_x(x1[num])函数是每更新一个num,长方形x轴的位置发生改变。
dian.set_width(width[num])函数是每更新一个num,长方形的长度发生改变。
set_title这里是每更新一个num,名字也发生改变。
#定义每跳动一个frame,应该取哪些数据点。
def update(num):
dian.set_x(x1[num])
dian.set_width(width[num])
ax.set_title(f'right now the frame is {num}')
return dian,
3. plt.Rectangle是把长方形加入画布。
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
dian=plt.Rectangle((1, 8), 0.6, 0.3, color='b', alpha=0.8)
ax.add_patch(dian)
plt.axis([1,8,2,9])
4. 最后制作动画,使用FuncAnimation,然后保存动画。
# 开始制作动画
donghua = animation.FuncAnimation(fig, update, np.arange(0, 30),interval=100, blit=False)
#最后保存动画
donghua.save('D:\\try.gif', fps=10)
plt.show()