【python】matplotlib(animation)

news2025/2/11 9:09:34

在这里插入图片描述

文章目录

  • 1、matplotlib.animation
    • 1.1、FuncAnimation
    • 1.2、修改 matplotlib 背景
  • 2、matplotlib + imageio
    • 2.1、折线图
    • 2.2、条形图
    • 2.3、散点图
  • 3、参考

1、matplotlib.animation

1.1、FuncAnimation

matplotlib.animation.FuncAnimation 是 Matplotlib 库中用于创建动画的一个类。它允许你通过循环调用一个函数来更新图表,从而生成动画效果。这个函数通常被称为“更新函数”,它决定了每一帧图表的样子。FuncAnimation 类提供了一种灵活而强大的方式来创建和展示动画,使得数据可视化更加生动和直观。

(1)基本用法

使用 FuncAnimation 创建动画的基本步骤如下:

  • 准备数据:首先,你需要准备好用于动画的数据。这可能包括一系列的X和Y坐标点、颜色、大小等,具体取决于你要制作的动画类型。
  • 创建图形和轴:使用 Matplotlib 创建图形(Figure)和轴(Axes)对象,这些对象将作为动画的画布。
  • 定义更新函数:编写一个函数,这个函数接受当前的帧号(或其他参数)作为输入,并返回一个更新后的图形元素状态。例如,如果你正在制作一个点的移动动画,这个函数可能会更新点的位置。
  • 创建 FuncAnimation 对象:使用 FuncAnimation 类创建一个动画对象。你需要指定图形对象、轴对象、更新函数、帧数(或时间间隔)、以及其他可选参数(如重复次数、初始延迟等)。
  • 显示或保存动画:最后,你可以使用 Matplotlib 的显示功能(如 plt.show())来查看动画,或者将其保存为文件(如GIF、MP4等)。

(2)示例代码

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
 
# 准备数据
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
 
# 创建图形和轴
fig, ax = plt.subplots()
line, = ax.plot([], [], 'r-')  # 初始化一个空线条对象
ax.set_xlim(0, 2 * np.pi)      # 设置X轴范围
ax.set_ylim(-1.5, 1.5)         # 设置Y轴范围
 
# 定义更新函数
def update(frame):
    line.set_data(x[:frame], y[:frame])  # 更新线条数据
    return line,
 
# 创建 FuncAnimation 对象
ani = FuncAnimation(fig, update, frames=len(x), interval=50, blit=True)
 
# 显示动画
plt.show()

在这里插入图片描述

在这个例子中,update 函数根据当前的帧号(frame)更新线条的数据,使得线条逐渐变长,模拟了一个点沿正弦曲线移动的动画效果。

再看一个例子

#coding=utf-8
import sys

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

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

def animate(i):
    line.set_ydata(np.sin(x + i/10.0))
    return line,

def init():
    line.set_ydata(np.ma.array(x, mask=True))
    return line,

ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
                              interval=25, blit=True)
ani.save("animation.gif", writer="imagemagick", fps=30)
plt.show()

在这里插入图片描述

(3)matplotlib.animation.FuncAnimation

class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, *, cache_frame_data=True, **kwargs)
def __init__(self,
             fig: Figure,
             func: (...) -> Iterable[Artist],
             frames: Iterable | int | () -> Generator | None = ...,
             init_func: () -> Iterable[Artist] | None = ...,
             fargs: tuple[Any, ...] | None = ...,
             save_count: int | None = ...,
             *,
             cache_frame_data: bool = ...,
             **kwargs: Any) -> None
`TimedAnimation` subclass that makes an animation by repeatedly calling a function *func*.  .. note::  You must store the created Animation in a variable that lives as long as the animation should run. Otherwise, the Animation object will be garbage-collected and the animation stops.  Parameters ---------- fig : `~matplotlib.figure.Figure` The figure object used to get needed events, such as draw or resize.  func : callable The function to call at each frame. The first argument will be the next value in *frames*. Any additional positional arguments can be supplied using `functools.partial` or via the *fargs* parameter.  The required signature is::  def func(frame, *fargs) -> iterable_of_artists  It is often more convenient to provide the arguments using `functools.partial`. In this way it is also possible to pass keyword arguments. To pass a function with both positional and keyword arguments, set all arguments as keyword arguments, just leaving the *frame* argument unset::  def func(frame, art, *, y=None): ...  ani = FuncAnimation(fig, partial(func, art=ln, y='foo'))  If ``blit == True``, *func* must return an iterable of all artists that were modified or created. This information is used by the blitting algorithm to determine which parts of the figure have to be updated. The return value is unused if ``blit == False`` and may be omitted in that case.  frames : iterable, int, generator function, or None, optional Source of data to pass *func* and each frame of the animation  - If an iterable, then simply use the values provided. If the iterable has a length, it will override the *save_count* kwarg.  - If an integer, then equivalent to passing ``range(frames)``  - If a generator function, then must have the signature::  def gen_function() -> obj  - If *None*, then equivalent to passing ``itertools.count``.  In all of these cases, the values in *frames* is simply passed through to the user-supplied *func* and thus can be of any type.  init_func : callable, optional A function used to draw a clear frame. If not given, the results of drawing from the first item in the frames sequence will be used. This function will be called once before the first frame.  The required signature is::  def init_func() -> iterable_of_artists  If ``blit == True``, *init_func* must return an iterable of artists to be re-drawn. This information is used by the blitting algorithm to determine which parts of the figure have to be updated. The return value is unused if ``blit == False`` and may be omitted in that case.  fargs : tuple or None, optional Additional arguments to pass to each call to *func*. Note: the use of `functools.partial` is preferred over *fargs*. See *func* for details.  save_count : int, optional Fallback for the number of values from *frames* to cache. This is only used if the number of frames cannot be inferred from *frames*, i.e. when it's an iterator without length or a generator.  interval : int, default: 200 Delay between frames in milliseconds.  repeat_delay : int, default: 0 The delay in milliseconds between consecutive animation runs, if *repeat* is True.  repeat : bool, default: True Whether the animation repeats when the sequence of frames is completed.  blit : bool, default: False Whether blitting is used to optimize drawing. Note: when using blitting, any animated artists will be drawn according to their zorder; however, they will be drawn on top of any previous artists, regardless of their zorder.  cache_frame_data : bool, default: True Whether frame data is cached. Disabling cache might be helpful when frames contain large objects.
Params:
fig – The figure object used to get needed events, such as draw or resize.
func – The function to call at each frame. The first argument will be the next value in *frames*. Any additional positional arguments can be supplied using `functools.partial` or via the *fargs* parameter. The required signature is:: def func(frame, *fargs) -> iterable_of_artists It is often more convenient to provide the arguments using `functools.partial`. In this way it is also possible to pass keyword arguments. To pass a function with both positional and keyword arguments, set all arguments as keyword arguments, just leaving the *frame* argument unset:: def func(frame, art, *, y=None): ... ani = FuncAnimation(fig, partial(func, art=ln, y='foo')) If ``blit == True``, *func* must return an iterable of all artists that were modified or created. This information is used by the blitting algorithm to determine which parts of the figure have to be updated. The return value is unused if ``blit == False`` and may be omitted in that case.
frames – Source of data to pass *func* and each frame of the animation - If an iterable, then simply use the values provided. If the iterable has a length, it will override the *save_count* kwarg. - If an integer, then equivalent to passing ``range(frames)`` - If a generator function, then must have the signature:: def gen_function() -> obj - If *None*, then equivalent to passing ``itertools.count``. In all of these cases, the values in *frames* is simply passed through to the user-supplied *func* and thus can be of any type.
init_func – A function used to draw a clear frame. If not given, the results of drawing from the first item in the frames sequence will be used. This function will be called once before the first frame. The required signature is:: def init_func() -> iterable_of_artists If ``blit == True``, *init_func* must return an iterable of artists to be re-drawn. This information is used by the blitting algorithm to determine which parts of the figure have to be updated. The return value is unused if ``blit == False`` and may be omitted in that case.
fargs – Additional arguments to pass to each call to *func*. Note: the use of `functools.partial` is preferred over *fargs*. See *func* for details.
save_count – Fallback for the number of values from *frames* to cache. This is only used if the number of frames cannot be inferred from *frames*, i.e. when it's an iterator without length or a generator.
cache_frame_data – Whether frame data is cached. Disabling cache might be helpful when frames contain large objects.
  • fig:图形对象(Figure),用于获取绘制、调整大小等事件。这是动画的画布。
  • func:可调用对象(函数),每帧调用的函数。该函数的第一个参数将是 frames 中的下一个值。任何其他的位置参数可以通过 fargs 参数提供。如果 blit 为 True,则该函数必须返回一个被修改或创建的所有图形元素(artists)的可迭代对象。
  • frames:可迭代对象、整数、生成器函数或 None,可选。用于传递给 func 和动画的每一帧的数据源。如果是可迭代对象,则直接使用提供的值。如果是一个整数,则相当于传递 range(frames)。如果是一个生成器函数,则必须具有特定的签名。如果为 None,则相当于传递 itertools.count。
  • init_func:可调用对象(函数),可选。用于绘制清空画面的函数。如果未提供,则将使用 frames 序列中的第一个项目的绘图结果。此函数将在第一帧之前被调用一次。如果 blit 为 True,则 init_func 必须返回一个将被重新绘制的图形元素(artists)的可迭代对象。
  • fargs:元组或 None,可选。传递给每次调用 func 的附加参数。
  • save_count:整数,可选。要缓存的 frames 中的值的数量。
  • interval:数字,可选。帧之间的延迟时间(以毫秒为单位)。默认为 200。
  • blit:布尔值,可选。控制是否使用 blitting 来优化绘制。当使用 blitting 时,只有变化的图形元素会被重新绘制,从而提高性能。
  • cache_frame_data:布尔值,可选。控制是否缓存帧数据。默认为 True。

方法说明

  • save:将动画保存为电影文件。
  • to_html5_video:将动画转换为 HTML5 视频。
  • to_jshtml:生成动画的 HTML 表示形式。

(4)注意事项

  • 性能:对于复杂的动画,可能需要优化性能,比如通过减少每次更新的数据量(使用 blit=True 参数)或调整帧的更新间隔。
  • 兼容性:保存动画时,不同的文件格式(如GIF、MP4)可能需要不同的编解码器支持。确保你的环境中安装了必要的编解码器。
  • 交互性:动画在Jupyter Notebook等交互式环境中可能表现不同,需要根据具体环境调整显示方式。

1.2、修改 matplotlib 背景

在上述示例代码的情况下,我们引入一些修改颜色的配置,

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

# 准备数据
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

# 创建图形和轴
fig, ax = plt.subplots()
line, = ax.plot([], [], 'r-')  # 初始化一个空线条对象
ax.set_xlim(0, 2 * np.pi)  # 设置X轴范围
ax.set_ylim(-1.5, 1.5)  # 设置Y轴范围

# 修改轴背景颜色
ax.set_facecolor("orange")  
# OR
# ax.set(facecolor = "orange")

# 修改绘图背景颜色
fig.patch.set_facecolor('yellow')   
fig.patch.set_alpha(1.0)

# 移除图表的上边框和右边框
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# 设置虚线网格线
ax.set_axisbelow(True)
ax.yaxis.grid(color='gray', linestyle='dashed', alpha=0.7)

# 定义更新函数
def update(frame):
    line.set_data(x[:frame], y[:frame])  # 更新线条数据
    return line,

# 创建 FuncAnimation 对象
ani = FuncAnimation(fig, update, frames=len(x), interval=50, blit=True)

# ani.save("animation.gif", writer="imagemagick", fps=30)

# 显示动画
plt.show()

修改前

在这里插入图片描述

修改后
在这里插入图片描述
换个背景图试试

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

# 准备数据
x = np.linspace(0, 20 * np.pi, 100)
y = 9* np.sin(x)

# 创建图形和轴
fig, ax = plt.subplots()
line, = ax.plot([], [], 'r-')  # 初始化一个空线条对象

img = plt.imread("123.jpg")
ax.imshow(img, extent=[0, 65, -10, 10])  # 横纵坐标范围

# 定义更新函数
def update(frame):
    line.set_data(x[:frame], y[:frame])  # 更新线条数据
    return line,


# 创建 FuncAnimation 对象
ani = FuncAnimation(fig, update, frames=len(x), interval=50, blit=True)

ani.save("animation.gif", writer="imagemagick", fps=30)

# 显示动画
plt.show()

原始图片

在这里插入图片描述

添加之后的效果
在这里插入图片描述

2、matplotlib + imageio

2.1、折线图

先画个简单的折线图

import os
import numpy as np
import matplotlib.pyplot as plt
import imageio

np.random.seed(1234)

# 生成40个取值在30-40的数
y = np.random.randint(30, 40, size=(40))
print(y)
"""
[33 36 35 34 38 39 31 37 39 36 38 30 35 30 39 36 32 30 35 32 36 33 37 30
 39 30 33 32 33 31 33 31 33 37 31 37 34 30 35 31]
"""
# 绘制折线
plt.plot(y)
# 设置y轴最小值和最大值
plt.ylim(20, 50)

# 显示
plt.show()

在这里插入图片描述

保存最后几个点的数据,然后绘制成 gif

import os
import numpy as np
import matplotlib.pyplot as plt
import imageio

np.random.seed(1234)

# 生成40个取值在30-40的数
y = np.random.randint(30, 40, size=(40))
print(y)
"""
[33 36 35 34 38 39 31 37 39 36 38 30 35 30 39 36 32 30 35 32 36 33 37 30
 39 30 33 32 33 31 33 31 33 37 31 37 34 30 35 31]
"""
# 绘制折线
plt.plot(y)
# 设置y轴最小值和最大值
plt.ylim(20, 50)

# 显示
plt.show()

# 第一张图
plt.plot(y[:-3])
plt.ylim(20, 50)
plt.savefig('1.png')
plt.show()

# 第二张图
plt.plot(y[:-2])
plt.ylim(20, 50)
plt.savefig('2.png')
plt.show()

# 第三张图
plt.plot(y[:-1])
plt.ylim(20, 50)
plt.savefig('3.png')
plt.show()

# 第四张图
plt.plot(y)
plt.ylim(20, 50)
plt.savefig('4.png')
plt.show()

# 生成Gif
with imageio.get_writer('mygif.gif', mode='I') as writer:
    for filename in ['1.png', '2.png', '3.png', '4.png']:
        image = imageio.imread(filename)
        writer.append_data(image)

横坐标 0 至 36
在这里插入图片描述

横坐标 0 至 37

在这里插入图片描述

横坐标 0 至 38

在这里插入图片描述

横坐标 0 至 39

在这里插入图片描述

合并成为 gif(仅播放一次)

请添加图片描述

下面把所有点都保存下来,绘制动态图(仅播放一次)

import os
import numpy as np
import matplotlib.pyplot as plt
import imageio

np.random.seed(1234)

# 生成40个取值在30-40的数
y = np.random.randint(30, 40, size=(40))
print(y)
"""
[33 36 35 34 38 39 31 37 39 36 38 30 35 30 39 36 32 30 35 32 36 33 37 30
 39 30 33 32 33 31 33 31 33 37 31 37 34 30 35 31]
"""
# 绘制折线
plt.plot(y)
# 设置y轴最小值和最大值
plt.ylim(20, 50)

# 显示
plt.show()

filenames = []
num = 0
for i in y:
    num += 1
    # 绘制40张折线图
    plt.plot(y[:num])
    plt.ylim(20, 50)

    # 保存图片文件
    filename = f'{num}.png'
    filenames.append(filename)
    plt.savefig(filename)
    plt.close()

# 生成gif
with imageio.get_writer('mygif.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)

# 删除40张折线图
for filename in set(filenames):
    os.remove(filename)

在这里插入图片描述

2.2、条形图

import os
import numpy as np
import matplotlib.pyplot as plt
import imageio

np.random.seed(1234)

x = [1, 2, 3, 4, 5]
coordinates_lists = [[0, 0, 0, 0, 0],
                     [10, 30, 60, 30, 10],
                     [70, 40, 20, 40, 70],
                     [10, 20, 30, 40, 50],
                     [50, 40, 30, 20, 10],
                     [75, 0, 75, 0, 75],
                     [0, 0, 0, 0, 0]]
filenames = []
for index, y in enumerate(coordinates_lists):
    # 条形图
    plt.bar(x, y)
    plt.ylim(0, 80)

    # 保存图片文件
    filename = f'{index}.png'
    filenames.append(filename)

    # 重复最后一张图形15帧(数值都为0),15张图片
    if (index == len(coordinates_lists) - 1):
        for i in range(15):
            filenames.append(filename)

    # 保存
    plt.savefig(filename)
    plt.close()

# 生成gif
with imageio.get_writer('mygif.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)

# 删除20张柱状图
for filename in set(filenames):
    os.remove(filename)

生成的图片

在这里插入图片描述

生成的 gif(播放一次)

在这里插入图片描述

看起来太快了,优化代码使其平滑

import os
import numpy as np
import matplotlib.pyplot as plt
import imageio

np.random.seed(1234)

n_frames = 10  # 怕内存不够的话可以设置小一些
x = [1, 2, 3, 4, 5]
coordinates_lists = [[0, 0, 0, 0, 0],
                     [10, 30, 60, 30, 10],
                     [70, 40, 20, 40, 70],
                     [10, 20, 30, 40, 50],
                     [50, 40, 30, 20, 10],
                     [75, 0, 75, 0, 75],
                     [0, 0, 0, 0, 0]]
print('生成图表\n')
filenames = []
for index in np.arange(0, len(coordinates_lists) - 1):
    # 获取当前图像及下一图像的y轴坐标值
    y = coordinates_lists[index]
    y1 = coordinates_lists[index + 1]

    # 计算当前图像与下一图像y轴坐标差值
    y_path = np.array(y1) - np.array(y)
    for i in np.arange(0, n_frames + 1):
        # 分配每帧的y轴移动距离
        # 逐帧增加y轴的坐标值
        y_temp = (y + (y_path / n_frames) * i)
        # 绘制条形图
        plt.bar(x, y_temp)
        plt.ylim(0, 80)
        # 保存每一帧的图像
        filename = f'frame_{index}_{i}.png'
        filenames.append(filename)
        # 最后一帧重复,画面停留一会
        if (i == n_frames):
            for i in range(5):
                filenames.append(filename)
        # 保存图片
        plt.savefig(filename)
        plt.close()
print('保存图表\n')


# 生成GIF
print('生成GIF\n')
with imageio.get_writer('mybars.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)
print('保存GIF\n')


print('删除图片\n')
# 删除图片
for filename in set(filenames):
    os.remove(filename)
print('完成')

原理解释统计柱状图当前帧和下一帧的差值,然后插帧平滑过去,这里插帧数量配置为了 n_frames = 10

最终生成的 gif 如下(仅播放一次),可以观察到平滑了很多
在这里插入图片描述

接下来美化下界面

import os
import numpy as np
import matplotlib.pyplot as plt
import imageio

np.random.seed(1234)

n_frames = 5
bg_color = '#95A4AD'
bar_color = '#283F4E'
gif_name = 'bars'
x = [1, 2, 3, 4, 5]
coordinates_lists = [[0, 0, 0, 0, 0],
                     [10, 30, 60, 30, 10],
                     [70, 40, 20, 40, 70],
                     [10, 20, 30, 40, 50],
                     [50, 40, 30, 20, 10],
                     [75, 0, 75, 0, 75],
                     [0, 0, 0, 0, 0]]
print('生成图表\n')
filenames = []
for index in np.arange(0, len(coordinates_lists) - 1):
    y = coordinates_lists[index]
    y1 = coordinates_lists[index + 1]
    y_path = np.array(y1) - np.array(y)
    for i in np.arange(0, n_frames + 1):
        y_temp = (y + (y_path / n_frames) * i)
        # 绘制条形图
        fig, ax = plt.subplots(figsize=(8, 4))
        ax.set_facecolor(bg_color)
        plt.bar(x, y_temp, width=0.4, color=bar_color)
        plt.ylim(0, 80)

        # 移除图表的上边框和右边框
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)

        # 设置虚线网格线
        ax.set_axisbelow(True)
        ax.yaxis.grid(color='gray', linestyle='dashed', alpha=0.7)

        # 保存每一帧的图像
        filename = f'images/frame_{index}_{i}.png'
        filenames.append(filename)

        # 最后一帧重复,画面停留一会
        if (i == n_frames):
            for i in range(5):
                filenames.append(filename)
        # 保存图片
        plt.savefig(filename, dpi=96, facecolor=bg_color)
        plt.close()
print('保存图表\n')

# 生成GIF
print('生成GIF\n')
with imageio.get_writer(f'{gif_name}.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)
print('保存GIF\n')

print('删除图片\n')
# 删除图片
for filename in set(filenames):
    os.remove(filename)
print('完成')

看看生成的 gif 效果(仅播放一次)

在这里插入图片描述
给图表添加了背景色、条形图上色、去除边框、增加网格线等。

2.3、散点图

import os
import numpy as np
import matplotlib.pyplot as plt
import imageio

np.random.seed(1234)

coordinates_lists = [[[0], [0]],
                     [[100, 200, 300], [100, 200, 300]],
                     [[400, 500, 600], [400, 500, 600]],
                     [[400, 500, 600, 400, 500, 600], [400, 500, 600, 600, 500, 400]],
                     [[500], [500]],
                     [[0], [0]]]
gif_name = 'movie'
n_frames = 5
bg_color = '#95A4AD'
marker_color = '#283F4E'
marker_size = 25

print('生成图表\n')
filenames = []
for index in np.arange(0, len(coordinates_lists) - 1):
    # 获取当前图像及下一图像的x与y轴坐标值
    x = coordinates_lists[index][0]  # 当前帧
    y = coordinates_lists[index][1]
    x1 = coordinates_lists[index + 1][0]  # 下一帧
    y1 = coordinates_lists[index + 1][1]
    # 查看两点差值
    while len(x) < len(x1):
        diff = len(x1) - len(x)
        x = x + x[:diff]
        y = y + y[:diff]
    while len(x1) < len(x):
        diff = len(x) - len(x1)
        x1 = x1 + x1[:diff]
        y1 = y1 + y1[:diff]
    # 计算路径
    x_path = np.array(x1) - np.array(x)
    y_path = np.array(y1) - np.array(y)
    for i in np.arange(0, n_frames + 1):
        # 计算当前位置
        x_temp = (x + (x_path / n_frames) * i)
        y_temp = (y + (y_path / n_frames) * i)
        # 绘制图表
        fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(aspect="equal"))
        ax.set_facecolor(bg_color)

        plt.scatter(x_temp, y_temp, c=marker_color, s=marker_size)
        plt.xlim(0, 1000)
        plt.ylim(0, 1000)
        # 移除边框线
        ax.spines['right'].set_visible(False)
        ax.spines['top'].set_visible(False)
        # 网格线
        ax.set_axisbelow(True)
        ax.yaxis.grid(color='gray', linestyle='dashed', alpha=0.7)
        ax.xaxis.grid(color='gray', linestyle='dashed', alpha=0.7)
        # 保存图片
        filename = f'images/frame_{index}_{i}.png'
        filenames.append(filename)
        if (i == n_frames):
            for i in range(5):
                filenames.append(filename)
        # 保存
        plt.savefig(filename, dpi=96, facecolor=bg_color)
        plt.close()
print('保存图表\n')

# 生成GIF
print('生成GIF\n')
with imageio.get_writer(f'{gif_name}.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)
print('保存GIF\n')


print('删除图片\n')
# 删除图片
for filename in set(filenames):
    os.remove(filename)
print('完成')

思路,计算前后帧坐标点数量的差 diff ,然后 while 循环来复制以实现数量平衡 x = x + x[:diff],最后插帧平滑移动 x_temp = (x + (x_path / n_frames) * i)

在这里插入图片描述

3、参考

  • 太强了,用 Matplotlib+Imageio 制作动画!
  • 如何在 Matplotlib 中更改绘图背景

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2295627.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ubuntu24.04安装布置ros

最近换电脑布置机器人环境&#xff0c;下了24.04&#xff0c;但是网上的都不太合适&#xff0c;于是自己试着布置好了&#xff0c;留作有需要的人一起看看。 文章目录 目录 前言 一、确认 ROS 发行版名称 二、检查你的 Ubuntu 版本 三、安装正确的 ROS 发行版 四、对于Ubuntu24…

接入 deepseek 实现AI智能问诊

1. 准备工作 注册 DeepSeek 账号 前往 DeepSeek 官网 注册账号并获取 API Key。 创建 UniApp 项目 使用 HBuilderX 创建一个新的 UniApp 项目&#xff08;选择 Vue3 或 Vue2 模板&#xff09;。 安装依赖 如果需要在 UniApp 中使用 HTTP 请求&#xff0c;推荐使用 uni.requ…

网络爬虫js逆向之异步栈跟栈案例

【注意&#xff01;&#xff01;&#xff01;】 前言&#xff1a; 1. 本章主要讲解js逆向之异步栈跟栈的知识&#xff08;通过单步执行调试&#xff09; 2. 使用关键字搜定位加密入口 3. 本专栏通过多篇文章【文字案例】的形式系统化进行描述 4. 本文章全文进行了脱敏处理 5. 详…

机器学习 - 需要了解的条件概率、高斯分布、似然函数

似然函数是连接数据与参数的桥梁&#xff0c;通过“数据反推参数”的逆向思维&#xff0c;成为统计推断的核心工具。理解它的关键在于区分“参数固定时数据的概率”与“数据固定时参数的合理性”&#xff0c;这种视角转换是掌握现代统计学和机器学习的基础。 一、在学习似然函…

【Spring】什么是Spring?

什么是Spring&#xff1f; Spring是一个开源的轻量级框架&#xff0c;是为了简化企业级开发而设计的。我们通常讲的Spring一般指的是Spring Framework。Spring的核心是控制反转(IoC-Inversion of Control)和面向切面编程(AOP-Aspect-Oriented Programming)。这些功能使得开发者…

[笔记] 汇编杂记(持续更新)

文章目录 前言举例解释函数的序言函数的调用栈数据的传递 总结 前言 举例解释 // Type your code here, or load an example. int square(int num) {return num * num; }int sub(int num1, int num2) {return num1 - num2; }int add(int num1, int num2) {return num1 num2;…

开放式TCP/IP通信

一、1200和1200之间的开放式TCP/IP通讯 第一步&#xff1a;组态1214CPU&#xff0c;勾选时钟存储器 第二步&#xff1a;防护与安全里面连接机制勾选允许PUT/GET访问 第三步&#xff1a;添加PLC 第四步&#xff1a;点击网络试图&#xff0c;选中网口&#xff0c;把两个PLC连接起…

(原创,可用)SSH实现内外网安全穿透(安全不怕防火墙)

目前有A、B终端和一台服务器&#xff0c;A、B机器不能直接访问&#xff0c;服务器不能直接访问A、B终端但是A、B终端可以访问服务器&#xff0c;这个场景很像我们有一台电脑在单位内网&#xff0c;外机器想访问内网系统&#xff0c;可能大家目前想到的就是frp之类穿透工具&…

第二节 docker基础之---镜像构建及挂载

查看当前镜像&#xff1a; [rootdocker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [rootdocker ~]#很明显docker是咱们新搭建的所以目前还没有镜像 1&#xff0c;搜索镜像&#xff1a; [rootdocker ~]# docker search centos 搜索镜像并过滤是官…

LLM学习笔记1——本地部署Meta-Llama-3.2-1B大模型

系列文章目录 参考博客 参考博客 文章目录 系列文章目录前言与调用一、部署要求二、实现步骤0.深度学习环境错误1&#xff0c;验证pytorch版本时提示以下问题&#xff1a;错误2&#xff0c;验证pytorch版本时提示以下问题&#xff1a;错误3&#xff0c;有时候还会提示你有一些…

AI安全最佳实践:AI应用开发安全评估矩阵(上)

生成式AI开发安全范围矩阵简介 生成式AI目前可以说是当下最热门的技术&#xff0c;吸引各大全球企业的关注&#xff0c;并在全球各行各业中带来浪潮般的编个。随时AI能力的飞跃&#xff0c;大语言模型LLM参数达到千亿级别&#xff0c;它和Transformer神经网络共同驱动了我们工…

deepseek+kimi自动生成ppt

打开deepseek官网&#xff0c;输入详细的需求&#xff0c;让他生成个ppt 接着deepseek开始思考生成了 接着复制生成了的内容 打开kimi粘贴刚才deepseek生成的内容 可以一键生成啦&#xff0c;下载编辑使用吧

《薄世宁医学通识50讲》以医学通识为主题,涵盖了医学的多个方面,包括医学哲学、疾病认知、治疗过程、医患关系、公共卫生等

《薄世宁医学通识50讲》是一门由薄世宁医生主讲的医学通识课程&#xff0c;该课程旨在通过深入浅出的方式&#xff0c;向广大听众普及医学知识&#xff0c;提升公众对医学的认知和理解。 晓北斗推荐-薄世宁医学通识 以下是对该课程的详细介绍&#xff1a; 一、课程概述 《薄世…

突破与重塑:逃离Java舒适区,借Go语言复刻Redis的自我突破和成长

文章目录 写在文章开头为什么想尝试用go复刻redis复刻redis的心路历程程序员对于舒适区的一点看法关于mini-redis的一些展望结语 写在文章开头 在程序员的技术生涯长河中&#xff0c;我们常常会在熟悉的领域中建立起自己的“舒适区”。于我而言&#xff0c;Java 就是这片承载…

优惠券平台(一):基于责任链模式创建优惠券模板

前景概要 系统的主要实现是优惠券的相关业务&#xff0c;所以对于用户管理的实现我们简单用拦截器在触发接口前创建一个单一用户。 // 用户属于非核心功能&#xff0c;这里先通过模拟的形式代替。后续如果需要后管展示&#xff0c;会重构该代码 UserInfoDTO userInfoDTO new…

TensorFlow域对抗训练DANN神经网络分析MNIST与Blobs数据集梯度反转层提升目标域适应能力可视化...

全文链接&#xff1a;https://tecdat.cn/?p39656 本文围绕基于TensorFlow实现的神经网络对抗训练域适应方法展开研究。详细介绍了梯度反转层的原理与实现&#xff0c;通过MNIST和Blobs等数据集进行实验&#xff0c;对比了不同训练方式&#xff08;仅源域训练、域对抗训练等&am…

09vue3实战-----引入element-plus组件库中的图标

09vue3实战-----引入element-plus组件库中的图标 1.安装2.引入3.优化 element-plus中的icon图标组件的使用和其他平台组件(如el-button按钮)是不一样的。 1.安装 npm install element-plus/icons-vue2.引入 在这我们只讲述最方便的一种引入方法------完整引入。这需要从elem…

消费电子产品中的噪声对TPS54202的影响

本文章是笔者整理的备忘笔记。希望在帮助自己温习避免遗忘的同时&#xff0c;也能帮助其他需要参考的朋友。如有谬误&#xff0c;欢迎大家进行指正。 一、概述 在白色家电领域&#xff0c;降压转换器的应用非常广泛&#xff0c;为了实现不同的功能就需要不同的电源轨。TPS542…

[Meet DeepSeek] 如何顺畅使用DeepSeek?告别【服务器繁忙,请稍后再试。】

文章目录 [Meet DeepSeek] 如何顺畅使用DeepSeek&#xff1f;告别【服务器繁忙&#xff0c;请稍后再试。】引言使用渠道一&#xff1a;硅基流动 Chatbox AI【推荐】硅基流动 Chatbox AI的优势 使用渠道二&#xff1a;秘塔AI搜索秘塔AI搜索的优势 其它方案1. DeepSeek官网2. 纳…

Websocket从原理到实战

引言 WebSocket 是一种在单个 TCP 连接上进行全双工通信的网络协议&#xff0c;它使得客户端和服务器之间能够进行实时、双向的通信&#xff0c;既然是通信协议一定要从发展历史到协议内容到应用场景最后到实战全方位了解 发展历史 WebSocket 最初是为了解决 HTTP 协议在实时…