Matplotlib 画动态图: animation模块的使用

本文通过四个实例介绍如何使用Python的matplotlib库及动画模块绘制动态图表,包括动态sin曲线、sin函数上的动点轨迹、单摆运动模拟及滚动球的路径展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

例子1. 动态画出sin函数曲线

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = ax.plot([], [], 'r-', animated=False)

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return ln,

def update(frame):
    xdata.append(frame)
    ydata.append(np.sin(frame))
    ln.set_data(xdata, ydata)
    return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
                    init_func=init, blit=True)
plt.show()

sin函数动态图

画这类图的关键是要给出不断更新的函数,这里就是update 函数了。注意, line, = ax.plot([], [], 'r-', animated=False) 中的, 表示创建tuple类型。迭代更新的数据frame 取值从frames 取得。

例子2. 动态显示一个动点,它的轨迹是sin函数。

import numpy as np 
import matplotlib.pyplot as plt
from matplotlib import animation

"""
animation example 2
author: Kiterun
"""

fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
l = ax.plot(x, y)
dot, = ax.plot([], [], 'ro')

def init():
    ax.set_xlim(0, 2*np.pi)
    ax.set_ylim(-1, 1)
    return l

def gen_dot():
    for i in np.linspace(0, 2*np.pi, 200):
        newdot = [i, np.sin(i)]
        yield newdot

def update_dot(newd):
    dot.set_data(newd[0], newd[1])
    return dot,

ani = animation.FuncAnimation(fig, update_dot, frames = gen_dot, interval = 100, init_func=init)
ani.save('sin_dot.gif', writer='imagemagick', fps=30)

plt.show()

这里我们把生成的动态图保存为gif图片,前提要预先安装imagemagic。 
sin_dot

例子3. 单摆(没阻尼&有阻尼)

无阻尼的单摆力学公式: 

d2θdt2+glsinθ=0d2θdt2+glsin⁡θ=0


附加阻尼项: 

d2θdt2+bmldθdt+glsinθ=0d2θdt2+bmldθdt+glsin⁡θ=0


这里需要用到scipy.integrate的odeint模块,具体用法找时间再专门写一篇blog吧,动态图代码如下:

 

# -*- coding: utf-8 -*-

from math import sin, cos
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import matplotlib.animation as animation

g = 9.8
leng = 1.0
b_const = 0.2

# no decay case:
def pendulum_equations1(w, t, l):
    th, v = w
    dth = v
    dv  = - g/l * sin(th)
    return dth, dv

# the decay exist case:
def pendulum_equations2(w, t, l, b):
    th, v = w
    dth = v
    dv = -b/l * v - g/l * sin(th)
    return dth, dv

t = np.arange(0, 20, 0.1)
track = odeint(pendulum_equations1, (1.0, 0), t, args=(leng,))
#track = odeint(pendulum_equations2, (1.0, 0), t, args=(leng, b_const))
xdata = [leng*sin(track[i, 0]) for i in range(len(track))]
ydata = [-leng*cos(track[i, 0]) for i in range(len(track))]

fig, ax = plt.subplots()
ax.grid()
line, = ax.plot([], [], 'o-', lw=2)
time_template = 'time = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)

def init():
    ax.set_xlim(-2, 2)
    ax.set_ylim(-2, 2)
    time_text.set_text('')
    return line, time_text

def update(i):
    newx = [0, xdata[i]]
    newy = [0, ydata[i]]
    line.set_data(newx, newy)
    time_text.set_text(time_template %(0.1*i))
    return line, time_text

ani = animation.FuncAnimation(fig, update, range(1, len(xdata)), init_func=init, interval=50)
#ani.save('single_pendulum_decay.gif', writer='imagemagick', fps=100)
ani.save('single_pendulum_nodecay.gif', writer='imagemagick', fps=100)
plt.show()

无衰减 
衰减

例子4. 滚动的球

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation

fig = plt.figure(figsize=(6, 6))
ax = plt.gca()
ax.grid()
ln1, = ax.plot([], [], '-', lw=2)
ln2, = ax.plot([], [], '-', color='r', lw=2)
theta = np.linspace(0, 2*np.pi, 100)
r_out = 1
r_in = 0.5

def init():
    ax.set_xlim(-2, 2)
    ax.set_ylim(-2, 2)
    x_out = [r_out*np.cos(theta[i]) for i in range(len(theta))]
    y_out = [r_out*np.sin(theta[i]) for i in range(len(theta))]
    ln1.set_data(x_out, y_out)
    return ln1,

def update(i):
    x_in = [(r_out-r_in)*np.cos(theta[i])+r_in*np.cos(theta[j]) for j in range(len(theta))]
    y_in = [(r_out-r_in)*np.sin(theta[i])+r_in*np.sin(theta[j]) for j in range(len(theta))]
    ln2.set_data(x_in, y_in)
    return ln2,

ani = animation.FuncAnimation(fig, update, range(len(theta)), init_func=init, interval=30)
ani.save('roll.gif', writer='imagemagick', fps=100)

plt.show()

这里写图片描述

<think>好的,用户想要生成matplotlib动态图代码,我需要详细地解释清楚。首先,得确定用户对matplotlib的熟悉程度,可能他们已经有基本的绘图经验,但动态图可能不太熟悉。我应该从最基础的例子开始,逐步讲解。 动态图matplotlib中通常使用animation模块,特别是FuncAnimation类。我需要先介绍必要的导入语句,比如导入matplotlib的pyplot和animation模块,还有numpy,因为生成数据会用到。 然后,要说明设置matplotlib的后端,比如使用TkAgg或者其他交互式后端,这样动态图才能正常显示。不过有时候在Jupyter笔记本中可能需要不同的设置,可能需要提醒用户根据环境调整。 接下来是创建图形和坐标轴,这跟静态图一样。然后初始化一个空的线对象,或者其他的图形元素,准备在动中更新。初始化函数init()用来设置初始状态,可能清空数据或者设置坐标轴范围。 然后是核心的更新函数animate(i),这里i是帧数,每次调用时生成新的数据,更新线对象的坐标,并返回需要重绘的元素。例如,用正弦函数生成数据,随着帧数改变相位,形成波动效果。 创建FuncAnimation对象时,需要指定图形、更新函数、帧数、间隔时间等参数。保存动可能需要安装额外的库,比如ffmpeg,用户可能需要注意这一点。 最后,展示图形用plt.show()。可能还需要提醒用户,如果在非交互式环境中,可能需要将动保存为文件再查看。另外,动态图对性能有一定要求,如果数据量太大,可能需要优化或者减少帧数。 可能用户的需求是实时显示数据变化,比如传感器数据或模拟过程,所以给出一个简单易懂的例子很重要。还要提到可能的常见问题,比如动不显示,或者速度慢,以及如何解决。确保代码结构清晰,注释详细,用户能一步步跟着操作。</think>在Python中使用matplotlib制作动态图主要通过`animation`模块实现。以下是一个完整的动态正弦波绘制示例代码(含详细注释): ```python import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation # 设置matplotlib使用可交互的后端(重要!若在IDE中运行可能需要此配置) plt.switch_backend('TkAgg') # 也可以尝试 'Qt5Agg' 等其他交互式后端 # 创建布和坐标轴 fig, ax = plt.subplots() ax.set_xlim(0, 2*np.pi) # X轴范围 ax.set_ylim(-1.5, 1.5) # Y轴范围 line, = ax.plot([], [], lw=2) # 创建空线条对象 # 初始化函数:清空/重置图形 def init(): line.set_data([], []) return line, # 动更新函数(核心逻辑) def animate(i): x = np.linspace(0, 2*np.pi, 200) y = np.sin(x + 0.1 * i) # 通过i值改变相位实现波动效果 line.set_data(x, y) return line, # 创建动对象 ani = FuncAnimation( fig, # 布对象 animate, # 更新函数 init_func=init,# 初始化函数 frames=200, # 总帧数(i从0到199) interval=20, # 帧间隔时间(毫秒) blit=True # 使用blitting优化渲染 ) plt.title("动态正弦波演示") plt.xlabel("X轴") plt.ylabel("Y轴") plt.grid(True) plt.show() ``` 关键要素解析: 1. **FuncAnimation** 核心类: - `fig`: 绑定到创建的布对象 - `animate`: 每帧调用的更新函数 - `frames`: 控制动总时长(帧数) - `interval`: 帧率控制(20ms=50FPS) 2. **常见应用场景**: - 实时数据监控(传感器数据) - 物理过程模拟(弹簧振动、波动方程) - 算法可视化(排序过程、路径搜索) 3. **性能优化技巧**: ```python # 在创建figure时设置关闭自动缩放(提升性能) fig, ax = plt.subplots() ax.set_autoscale_on(False) # 使用blitting技术(只重绘变化部分) ani = FuncAnimation(..., blit=True) ``` 4. **保存为GIF/视频**: ```python # 保存为GIF需要安装imagemagick ani.save('animation.gif', writer='imagemagick', fps=30) # 保存为MP4视频(需要ffmpeg) ani.save('animation.mp4', writer='ffmpeg', fps=30) ``` 常见问题解决方案: 1. **图形不显示**: - 确保使用交互式后端(如`TkAgg`, `Qt5Agg`) - 在Jupyter中使用`%matplotlib notebook`魔法命令 2. **动卡顿**: - 减少数据点数量 - 关闭抗锯齿:`line, = ax.plot([], [], lw=2, antialiased=False)` - 降低帧率(增大interval值) 3. **实时数据更新示例**: ```python def animate(i): # 获取最新100个数据点 new_data = get_live_data()[-100:] # 假设的实时获取数据函数 line.set_data(range(len(new_data)), new_data) ax.relim() # 自动调整坐标范围 ax.autoscale_view() return line, ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值