网站首页 > 基础教程 正文
【1】引言
python画图功能强,经过一段时间的学习,对探索散点图动态输出建立了浓厚兴趣,今天和大家一起开展学习。
【2】官网教程
首先来到官网,参考下述链接:
Animated scatter saved as GIF Matplotlib 3.9.2 documentation
官网展示了简洁的代码和动态的点,为此很有必要读懂代码。
【3】代码解读
首先引入计算、画图和动画三个模块:
import matplotlib.pyplot as plt #引入画图模块
import numpy as np #引入计算模块
import matplotlib.animation as animation #引入动画模块
然后设置要画图,限定X轴的范围:
fig, ax = plt.subplots() #定义要画图
ax.set_xlim([0, 10]) #设置X轴的范围为[0,10]
再之后定义初始点和自变量:
scat = ax.scatter(1, 0) #画散点图,点位置(1, 0)
x = np.linspace(0, 10) #定义自变量
然后自定义一个函数:
def animate(i): #自定义函数
scat.set_offsets((x[i], 0)) #设置偏移量,将自变量按照顺序输出
return scat,
再之后调用animation.FuncAnimation()函数制作动画输出:
ani = animation.FuncAnimation(fig, animate, repeat=True,
frames=len(x) - 1, interval=50) #调用animation.FuncAnimation()函数画图
最后输出图形:
plt.show()
输出动画为:
此时的完整代码为:
import matplotlib.pyplot as plt #引入画图模块
import numpy as np #引入计算模块
import matplotlib.animation as animation #引入动画模块
fig, ax = plt.subplots() #定义要画图
ax.set_xlim([0, 10]) #设置X轴的范围为[0,10]
scat = ax.scatter(1, 0) #画散点图,点位置(1, 0)
x = np.linspace(0, 10) #定义自变量
def animate(i): #自定义函数
scat.set_offsets((x[i], 0)) #设置偏移量,将自变量按照顺序输出
return scat,
ani = animation.FuncAnimation(fig, animate, repeat=True,
frames=len(x) - 1, interval=50) #调用animation.FuncAnimation()函数画图
# To save the animation using Pillow as a gif
# writer = animation.PillowWriter(fps=15,
# metadata=dict(artist='Me'),
# bitrate=1800)
# ani.save('scatter.gif', writer=writer)
ani.save('scatter.gif') #保存动图
plt.show() #输出图形
【4】代码修改
前述代码中,预先定义了scat变量。
经测试,如果将下述代码变为注释,则程序不能运行:
scat = ax.scatter(1, 0) #画散点图,点位置(1, 0)
对比发现,该行代码的作用是给一个初始点。
尝试将scat的值修改为:
scat = ax.scatter(1, 10) #画散点图,点位置(1, 10)
此时直接运行代码依然无法输出,继续修改下述代码:
def animate(i): #自定义函数
scat.set_offsets((x[i], 10)) #设置偏移量,将自变量按照顺序输出
return scat,
此时可以正常输出,输出结果的Y轴数值也对应变成10:
此时的完整代码为:
import matplotlib.pyplot as plt #引入画图模块
import numpy as np #引入计算模块
import matplotlib.animation as animation #引入动画模块
fig, ax = plt.subplots() #定义要画图
ax.set_xlim([0, 10]) #设置X轴的范围为[0,10]
scat = ax.scatter(1, 10) #画散点图,点位置(1, 0)
x = np.linspace(0, 10) #定义自变量
def animate(i): #自定义函数
scat.set_offsets((x[i], 10)) #设置偏移量,将自变量按照顺序输出
return scat,
ani = animation.FuncAnimation(fig, animate, repeat=True,
frames=len(x) - 1, interval=50) #调用animation.FuncAnimation()函数画图
# To save the animation using Pillow as a gif
# writer = animation.PillowWriter(fps=15,
# metadata=dict(artist='Me'),
# bitrate=1800)
# ani.save('scatter.gif', writer=writer)
ani.save('scatter2.gif') #保存动图
plt.show() #输出图形
【5】代码优化
为尝试输出曲线类动图,修改自定义函数:
def animate(i): #自定义函数
ax.scatter(x[i], np.sin(x[i])) #设置偏移量,将自变量按照顺序输出
return scat,
运行后的输出动图为:
此时对应的完整代码为:
import matplotlib.pyplot as plt #引入画图模块
import numpy as np #引入计算模块
import matplotlib.animation as animation #引入动画模块
fig, ax = plt.subplots() #定义要画图
ax.set_xlim([0, 10]) #设置X轴的范围为[0,10]
scat = ax.scatter(0, 0) #画散点图,点位置(1, 0)
x = np.linspace(0, 10,50) #定义自变量
def animate(i): #自定义函数
ax.scatter(x[i], np.sin(x[i])) #设置偏移量,将自变量按照顺序输出
return scat,
ani = animation.FuncAnimation(fig, animate, repeat=True,
frames=len(x) - 1, interval=50) #调用animation.FuncAnimation()函数画图
# To save the animation using Pillow as a gif
# writer = animation.PillowWriter(fps=15,
# metadata=dict(artist='Me'),
# bitrate=1800)
ani.save('scatter3.gif')
plt.show()
【6】总结
学习了散点图动态输出基础教程,并尝试输出了构成曲线形状的散点图。
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_44855046/article/details/142757391
猜你喜欢
- 2024-10-31 Python基础之tuple list dict set
- 2024-10-31 python 基础之数据类型set python6种数据类型
- 2024-10-31 103. __setitem__()高级用法#python编程
- 2024-10-31 「python课程,精心总结」python集合set
- 2024-10-31 python之list(set())函数 python set与list
- 最近发表
- 标签列表
-
- jsp (69)
- gitpush (78)
- gitreset (66)
- python字典 (67)
- dockercp (63)
- gitclone命令 (63)
- dockersave (62)
- linux命令大全 (65)
- pythonif (86)
- location.href (69)
- dockerexec (65)
- tail-f (79)
- queryselectorall (63)
- location.search (79)
- bootstrap教程 (74)
- 单例 (62)
- linuxgzip (68)
- 字符串连接 (73)
- html标签 (69)
- c++初始化列表 (64)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)