import numpy as np
import pandas as pd
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots() # 创建图表和axes
def update(i):
‘’’
函数为更新axes信息
i 可以理解为迭代词数
返回一个axes
'''
return table
ani = FuncAnimation(fig=fig, # 更新的画布
func=update, # 更新函数
frames=np.linspace(0, 3*np.pi, 300), # 生成动态图的帧数
interval=10, # 帧间时间间隔,单位:ms
blit= True, # 让输出显示更快
repeat=False # 不循环播放
)
ani.save(“test_1.gif”,writer=‘pillow’)
动态折线图
talbe = ax.plot(xdata,ydata, 'b-')
talbe
[<matplotlib.lines.Line2D at 0x121864d20f0>]
fig, ax = plt.subplots() #
xdata, ydata = [], []
ax.set_xlim(0,10)
ax.set_ylim(-1,1)
def update(i):
# ax.clear()
xdata.append(i)
ydata.append(np.sin(i))
talbe = ax.plot(xdata,ydata, 'b-')
return talbe
ani = FuncAnimation(fig=fig, # 更新的画布
func=update, # 更新函数
frames=np.linspace(0, 3*np.pi, 300), # 生成动态图的帧数
interval=10, # 帧间时间间隔,单位:ms
blit= True, # 让输出显示更快
repeat=False # 不循环播放
)
ani.save("test_2.gif",writer='pillow')
动态柱状
fig, ax = plt.subplots(figsize=(20,8))
def update(i):
names = x
value = y=a[:,i]
ax.clear()
rects = ax.bar(x=names,height=value,width=0.8,align='center')
ax.set_ylim(0,10)
ax.set_xlabel('x_label',color='black',size=15)
ax.set_ylabel('prob', color='black',size=15)
ax.set_title(f' timestamp:{i}time',color='blue',size=20)
# 添加网格显示
ax.grid(linestyle='--',alpha=0.5)
return rects
ani = FuncAnimation(fig=fig, # 更新的画布
func=update, # 更新函数
frames=20, # 生成动态图的帧数
interval=1000, # 帧间时间间隔,单位:ms
blit=False, # 让输出显示更快
repeat=True # 不循环播放
)

动态雷达
import numpy as np
import matplotlib.pyplot as plt
fig,ax = plt.subplots(subplot_kw=dict(projection='polar'))
def update(i):
ax.clear()
labels = np.array(['艺术A','调研I','实际R','常规C','企业E','社会S'])
#数据个数
dataLenth = 6
#数据
data = np.random.randint(5,10,(6))
angles = np.linspace(0, 2*np.pi, dataLenth, endpoint=False)
data = np.concatenate((data, [data[0]])) # 闭合
angles = np.concatenate((angles, [angles[0]])) # 闭合
# ax = fig.add_subplot(111, polar=True)# polar参数!!
table = ax.plot(angles, data, 'bo-', linewidth=2)# 画线
ax.fill(angles, data, facecolor='r', alpha=0.25)# 填充
ax.set_thetagrids(angles * 180/np.pi, labels, fontproperties="SimHei")
ax.set_title(f"matplotlib雷达图{i}", va='bottom', fontproperties="SimHei")
ax.set_rlim(0,10)
ax.grid(True)
return table
ani = FuncAnimation(fig=fig, # 更新的画布
func=update, # 更新函数
frames=20, # 生成动态图的帧数
interval=1000, # 帧间时间间隔,单位:ms
blit=False, # 让输出显示更快
repeat=True # 不循环播放
)
ani.save("test_1.gif",writer='pillow')

本文详细介绍如何使用Python的Matplotlib库创建动态折线图、柱状图及雷达图。通过FuncAnimation函数,结合NumPy和Pandas,实现数据的实时更新与图表的动态展示。
627

被折叠的 条评论
为什么被折叠?



