【可视化】matplotlib.animation_动图

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')

在这里插入图片描述


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值