matplotlib--animation
matplotlib -- animation
2023-09-14 09:13:09 时间
'''
Author: 365JHWZGo
Description: matplotlib--animation
Date: 2021/11/5 21:45
FilePath: day1105-6.py
'''
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
fig,ax = plt.subplots()
x = np.arange(0,2*np.pi,0.01)
line, = ax.plot(x,np.sin(x)) #加,是因为ax.plot()返回的是一个列表
#自定义动画函数
def animate(i):
line.set_ydata(np.sin(x+i/10.0))
return line,
def init():
line.set_ydata(np.sin(x))
return line,
ani = animation.FuncAnimation(
fig=fig,
func=animate,
init_func=init,
frames=100,
interval=20,
blit=False #blit用于是否更新全部的数据,False表示只更新有变化的点
)
plt.show()
学习到了,那么我们来自己搞一个
'''
Author: 365JHWZGo
Description: matplotlib--animation2
Date: 2021/11/6 22:30
FilePath: day1106-2.py
'''
首先画一个静态的圆
# import library
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
# 圆心坐标
a, b = (0., 0.)
# 初始圆半径
r = 1
#圆心角θ
theta = np.linspace(0, 2 * np.pi, 200)
#绘制figure
fig, ax = plt.subplots(figsize=(3, 3))
x = a + r * np.cos(theta)
y = b + r * np.sin(theta)
circle, = ax.plot(x, y, color='red', linewidth=2)
ax.set_xticks(())
ax.set_yticks(())
plt.show()
使用刚刚学到的技能让它动起来吧!
'''
Author: 365JHWZGo
Description: matplotlib--animation2
Date: 2021/11/6 22:30
FilePath: day1106-2.py
'''
# import library
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
# 圆心坐标
a, b = (0., 0.)
# 初始圆半径
r = 1
theta = np.linspace(0, 2 * np.pi, 200)
fig, ax = plt.subplots(figsize=(3, 3))
x = a + r * np.cos(theta)
y = b + r * np.sin(theta)
circle, = ax.plot(x, y, color='red', linewidth=2)
ax.set_xticks(())
ax.set_yticks(())
def animate(i):
circle.set_xdata(a + r * (i / 50.0) * np.cos(theta)) #主要的目的就是使其的半径发生变化
circle.set_ydata(b + r * (i / 50.0) * np.sin(theta))
return circle,
def init():
circle.set_ydata(y)
circle.set_xdata(x)
return circle,
ani = animation.FuncAnimation(
fig = fig,
func=animate,
init_func=init,
frames=100,
interval=10,
blit=False
)
#plt.rcParams['animation.convert_path']='E:\ImageMagick-7.1.0-Q16-HDRI\magick.exe'
ani.save('circle.gif',writer='imagemagick')
plt.show()
一个圆的动画就已经生成了!
相关文章
- 量化笔记:matplotlib绘制K线图、成交量的组合图
- 绘图: matplotlib核心剖析
- mac pro下安装安装 SymPy 和 matplotlib报错解决方案
- Matplotlib:mpl_toolkits.mplot3d工具包
- Py之matplotlib:python包之matplotlib库图表绘制包的简介、安装、使用方法(matplotlib颜色大全)详细攻略
- 成功解决ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,问题
- 深入浅出matplotlib(71): 界面交互之按钮
- 深入浅出matplotlib(59): 理解pcolormesh ()函数的使用一
- 深入浅出matplotlib(9):知道两点坐标画直线
- 深入浅出matplotlib(4):网格线的颜色和刻度
- 深入浅出matplotlib(3):设置绘图区域大小
- matplotlib--3d data
- 跟我一起学点数据分析 -- 第五天:数据可视化(matplotlib部)
- Python科学库sklearn.numpy.scipy. matplotlib.pandas