网站首页 > 技术文章 正文
【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-14 Python之Matplotlib数据可视化一:简易线形图
- 2024-10-14 圆:circle-sin-cos动画的matplotlib
- 2024-10-14 python 100天 68 利用Python绘制两个波形正弦sin函数相关性
- 2024-10-14 画直线不简单!python-matplotlib告诉你为什么
- 2024-10-14 用Python下一场流星雨,女生看了都哭了
- 2024-10-14 手把手教你使用Numpy、Matplotlib、Scipy等5个Python库
- 2024-10-14 走进Matplotlib世界(一)(matplotlib.org)
- 2024-10-14 Python 数据分析——matplotlib 坐标变换和注释
- 2024-10-14 利用axe对象绘制地图局部缩放图(下面几种建模对象能通过基本实体工具直接绘制的是)
- 2024-10-14 Python动态绘图的方法(上)(canvas python动态绘图)
- 最近发表
- 标签列表
-
- cmd/c (64)
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- sqlset (64)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)