从零开始学Python爬虫系列:Matplotlib FuncAnimation进阶篇,如何选择加入图形的元素?文字?图形?刻度?

基础的例子我在上一篇FuncAnimation讲过了。但是如果你想创造自己的图表,看完这一篇可以让你游刃有余。

(一)搞懂figure和canvas和axes。axes=canvas≠figure

首先,你得知道一开始展开一张图,可以用以下三个方法

1. 如果你想快速完成工作,你就可以使用plt接口

plt.plot(x, y)

2. 这里通过把plt赋值给ax,可以通过ax来操作plt相关的内容,也可以使用plt的相关功能。注意这里有一个隐藏的figure。

ax = plt.subplot()
ax.plot(x, y)

进行一步扩展

tupian,zhou=plt.subplot(1,2)

3. 这里把figure显示了出来,并且plot也有了命名。

figure = plt.figure(figsize=(8,4))
newplot = figure.add_subplot(111)
newplot.plot(x, y)

4. 这时候你要问了,所以他们之间到底有什么区别?

(1)plt.plot和plt.subplots()难道不是一样的吗?

        从零开始学Python爬虫系列:Matplotlib FuncAnimation进阶篇,如何选择加入图形的元素?文字?图形?刻度?_第1张图片从零开始学Python爬虫系列:Matplotlib FuncAnimation进阶篇,如何选择加入图形的元素?文字?图形?刻度?_第2张图片

(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

注意,如果你要加入下面的元素,你需要用最后一种方法。

(二)在官网可以找到你可以加入的元素,下面的modules就是你可以加入的东西。

        https://matplotlib.org/stable/api/index.html

1. 如果你想加文字,可以选择matplotlib.text

从零开始学Python爬虫系列:Matplotlib FuncAnimation进阶篇,如何选择加入图形的元素?文字?图形?刻度?_第3张图片

从零开始学Python爬虫系列:Matplotlib FuncAnimation进阶篇,如何选择加入图形的元素?文字?图形?刻度?_第4张图片

2. 如果你想在图表里面加图像,可以选择matplotlib.image

3. 如果你想在图标里加各种图标,比如箭头,长方形,多边形等等,可以选择matplotlib.patches

(三)举个例子,使用matplotlib.patches。这里有各种图形,箭头,圆形,多边形,正方形等等。

        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 = (xy) 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中的更新函数中,就可以每一帧改变长方形的参数。

(五)所以在设定动画的时候,可以根据图形自带的各种函数,来定义更新的数据。

从零开始学Python爬虫系列:Matplotlib FuncAnimation进阶篇,如何选择加入图形的元素?文字?图形?刻度?_第5张图片

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

你可能感兴趣的:(从零开始学python爬虫)