1 方波的圆周分解
在学习傅里叶变换的时候,有一个经典的示例是方波的分解。我们知道,方波可以分解为无数个正弦波的叠加。而正弦波,又可以看作是圆周运动在一条直线上的投影。当时为了理解这个事情,恐怕大家也花了不少时间。
学习了matplotlib之后,出于学以致用的考虑,我们能不能绘制出动画,来描述上述分解,便于我们来理解呢?
先上动图:
前面学习中已经掌握了matplotlib如何制作动画,以及如何绘制子图。在这个例子中我们将看到以下内容的实战:
- 分割画布为子图
- 绘制圆和波形图
- 调整图像的轴比例
- 隐藏图像的刻度轴
- 设置线型和颜色
- 生成和保存动画
2. 绘图源码
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
# 把半径放大3倍 画大一些
radius = 4 / np.pi * 3
t = np.deg2rad(list(range(0, 360, 1)))
tc = [tt + 8 for tt in t]
# 一级圆
x = np.cos(t) * radius
y = np.sin(t) * radius
# 二级圆
x1 = np.cos(3*t) * radius / 3
y1 = np.sin(3*t) * radius / 3
# 三级圆
x2 = np.cos(5*t) * radius / 5
y2 = np.sin(5*t) * radius / 5
# 四级圆
x3 = np.cos(7*t) * radius / 7
y3 = np.sin(7*t) * radius / 7
# 子图的切割
fig, ax = plt.subplots(4, 1)
fig.set_size_inches(5, 8)
fig.tight_layout()
ax[0].set(xlim=(-2, 10), ylim=(-6, 6))
#设置横纵坐标等比例且根据轴范围自适应
ax[0].set_aspect("equal", adjustable='datalim')
ax[0].set_axis_off() # 隐藏轴刻度线
# 绘制固定的那个圆
ax[0].plot(x, y, 'b-', linewidth=1)
ax[1].set(xlim=(-2, 10), ylim=(-6, 6))
ax[1].set_aspect("equal", adjustable='datalim')
ax[1].set_axis_off()
ax[1].plot(x, y, 'b-', linewidth=1)
ax[2].set(xlim=(-2, 10), ylim=(-6, 6))
ax[2].set_aspect("equal", adjustable='datalim')
ax[2].set_axis_off()
ax[2].plot(x, y, 'b-', linewidth=1)
ax[3].set(xlim=(-2, 10), ylim=(-6, 6))
ax[3].set_aspect("equal", adjustable='datalim')
ax[3].set_axis_off()
ax[3].plot(x, y, 'b-', linewidth=1)
# 动画准备
artists = []
for i in range(0, len(t)):
# container用来存储每帧要绘制的内容,通过+=叠加
container = []
container += ax[0].plot([0, x[i]], [0, y[i]], 'b-', linewidth=1)
yy = list(y[i:len(y)])
yy.extend(y[0:i])
container += ax[0].plot(tc, yy, color='blue', linewidth=1)
container += ax[0].plot([x[i], tc[0]], [y[i], yy[0]], 'b--', linewidth=1)
container += ax[1].plot([0, x[i]], [0, y[i]], 'b-', linewidth=1)
container += ax[1].plot([x1i + x[i] for x1i in x1], [y1i + y[i] for y1i in y1], 'r-', linewidth=1)
y_y1 = y+y1
yy1 = list(y_y1[i:len(y_y1)])
yy1.extend(y_y1[0:i])
container += ax[1].plot([x[i], x[i]+x1[i]], [y[i], y[i]+y1[i]], 'r-', linewidth=1)
container += ax[1].plot(tc, yy1, color='red', linewidth=1)
container += ax[1].plot([x[i]+x1[i], tc[0]], [y[i]+y1[i], yy1[0]], 'r--', linewidth=1)
container += ax[2].plot([0, x[i]], [0, y[i]], 'b-', linewidth=1)
container += ax[2].plot([x1i + x[i] for x1i in x1], [y1i + y[i] for y1i in y1], 'r-', linewidth=1)
container += ax[2].plot([x[i], x[i] + x1[i]], [y[i], y[i] + y1[i]], 'r-', linewidth=1)
container += ax[2].plot([x2i + x1[i] + x[i] for x2i in x2], [y2i + y1[i] + y[i] for y2i in y2], 'g-', linewidth=1)
container += ax[2].plot([x[i] + x1[i], x[i] + x1[i] + x2[i]], [y[i] + y1[i], y[i] + y1[i] + y2[i]], 'g-',
linewidth=1)
y_y2 = y + y1 + y2
yy2 = list(y_y2[i:len(y_y2)])
yy2.extend(y_y2[0:i])
container += ax[2].plot(tc, yy2, color='green', linewidth=1)
container += ax[2].plot([x[i] + x1[i] + x2[i], tc[0]], [y[i] + y1[i] + y2[i], yy2[0]], 'g--',
linewidth=1)
container += ax[3].plot([0, x[i]], [0, y[i]], 'b-', linewidth=1)
container += ax[3].plot([x1i + x[i] for x1i in x1], [y1i + y[i] for y1i in y1], 'r-', linewidth=1)
container += ax[3].plot([x[i], x[i] + x1[i]], [y[i], y[i] + y1[i]], 'r-', linewidth=1)
container += ax[3].plot([x2i + x1[i] + x[i] for x2i in x2], [y2i + y1[i] + y[i] for y2i in y2], 'g-', linewidth=1)
container += ax[3].plot([x[i] + x1[i], x[i] + x1[i] + x2[i]], [y[i] + y1[i], y[i] + y1[i] + y2[i]], 'g-',
linewidth=1)
container += ax[3].plot([x3i + x2[i] + x1[i] + x[i] for x3i in x3], [y3i + y2[i] + y1[i] + y[i] for y3i in y3],
'c-', linewidth=1)
container += ax[3].plot([x[i] + x1[i] + x2[i], x[i] + x1[i] + x2[i] + x3[i]],
[y[i] + y1[i] + y2[i], y[i] + y1[i] + y2[i] + y3[i]], 'c-', linewidth=1)
y_y3 = y + y1 + y2 + y3
yy3 = list(y_y3[i:len(y_y3)])
yy3.extend(y_y3[0:i])
container += ax[3].plot(tc, yy3, color='cyan', linewidth=1)
container += ax[3].plot([x[i] + x1[i] + x2[i] + x3[i], tc[0]], [y[i] + y1[i] + y2[i] + y3[i], yy3[0]],
'c--', linewidth=1)
artists.append(container)
# 生成并保存动图
ani = animation.ArtistAnimation(fig=fig, artists=artists, interval=40)
ani.save(filename="c:/users/admin/desktop/fourier.gif", writer="pillow")
plt.show()
这里还有一些优化空间,例如还没有为这幅图加上文字说明(涉及 LaTeX \LaTeX LATEX输入),以及也没有绘制出方波本身。作为一个练习,小白觉得主体的内容已经足够,留待后续优化。