优秀的编程知识分享平台

网站首页 > 技术文章 正文

python画图|散点图动态输出(python画散点图点的形状)

nanyue 2024-10-14 11:31:50 技术文章 55 ℃

【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

Tags:

最近发表
标签列表