自学内容网 自学内容网

python画图|散点图动态输出

【1】引言

在前述学习过程中,已经探索过曲线图动态输出,可以通过下述链接追溯:

python画图|曲线动态输出基础教程-CSDN博客

为此,有必要类比探索一下散点图动态输出。

【2】官网教程

首先来到官网,参考下述链接:

https://matplotlib.org/stable/gallery/animation/simple_scatter.html#sphx-glr-gallery-animation-simple-scatter-py

官网展示了简洁的代码和动态的点,为此很有必要读懂代码。

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

输出动画为:

e8a29a214c12416d8f8888dc64913404.gif

图1

此时的完整代码为:

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:

4af635507448432c88ea00db8957e422.gif

图2

此时的完整代码为:

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,

 运行后的输出动图为:

b9fc9f5978ed466eb8ea46a4b915049e.gif

图3

此时对应的完整代码为:

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】总结

学习了散点图动态输出基础教程,并尝试输出了构成曲线形状的散点图。

 


原文地址:https://blog.csdn.net/weixin_44855046/article/details/142757391

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!