【python】matplotlib(animation)

在这里插入图片描述

文章目录

  • 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 中更改绘图背景

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

IntelliJ IDEA使用经验(十三):使用Git克隆github的开源项目

文章目录 问题背景办法1、设置git代理&#xff1b;2、再次克隆项目&#xff1b;3、再次按常规方式进行git克隆即可。 问题背景 由于github在国外&#xff0c;很多时候我们在使用idea克隆开源项目的时候&#xff0c;没办法检出&#xff0c;提示 连接重置。 办法 1、设置git代…

人工智能学习(七)之神经网络

目录 一、引言 二、经典神经网络回顾 &#xff08;一&#xff09;结构与计算过程 &#xff08;二&#xff09;局限性 三、循环神经网络&#xff08;RNN&#xff09;原理 &#xff08;一&#xff09;基本结构 &#xff08;二&#xff09;计算过程 &#xff08;三&#xf…

IDEA编写SpringBoot项目时使用Lombok报错“找不到符号”的原因和解决

目录 概述|背景 报错解析 解决方法 IDEA配置解决 Pom配置插件解决 概述|背景 报错发生背景&#xff1a;在SpringBoot项目中引入Lombok依赖并使用后出现"找不到符号"的问题。 本文讨论在上述背景下发生的报错原因和解决办法&#xff0c;如果仅为了解决BUG不论原…

使用golang wails写了一个桌面端小工具:WoWEB, 管理本地多前端项目

WoWEB 本地快速启动 http 服务。 辅助管理本地前端项目。 使用界面配置代理转发。 支持平台 windows 10macOS 功能描述 管理本地前端项目启动本地 HTTP 服务&#xff0c;可本地或者局域网访问快速打开项目文件夹配置 HTTP 代理转发规则&#xff0c;方便开发调试 以下情况…

Unity Dots理论学习-5.与ECS相关的概念

DOTS的面向数据编程方式比你在MonoBehaviour项目中常见的面向对象编程方式更适合硬件开发。可以尝试理解一些与数据导向设计&#xff08;DOD&#xff09;相关的关键概念&#xff0c;以及这些概念如何影响你的代码&#xff0c;对你在MonoBehaviour项目中的C#编程通常是较少涉及的…

【hive】记一次hiveserver内存溢出排查,线程池未正确关闭导致

一、使用 MemoryAnalyzer软件打开hprof文件 很大有30G&#xff0c;win内存24GB&#xff0c;不用担心可以打开&#xff0c;ma软件能够生成索引文件&#xff0c;逐块分析内存&#xff0c;如下图。 大约需要4小时。 overview中开不到具体信息。 二、使用Leak Suspects功能继续…

(篇三)基于PyDracula搭建一个深度学习的软件之解析yolo算法融合

文章目录 1YoloPredictor类——检测器1.1继承BasePredictor解析1.2继承QObject解析 2MainWindow类——主窗口 在前面两篇中&#xff0c;篇一介绍了启动界面的制作&#xff0c;篇二介绍了如何修改PyDracula的界面&#xff0c;那么这一篇我们学习一下yolo要融合进入软件中&#x…

26~31.ppt

目录 26.北京主要的景点 题目 解析 27.创新产品展示及说明会 题目​ 解析 28.《小企业会计准则》 题目​ 解析 29.学习型社会的学习理念 题目​ 解析 30.小王-产品展示信息 题目​ 解析 31.小王-办公理念-信息工作者的每一天 题目​ 解析 26.北京主要的景点…

Vue.js 状态管理库Pinia

Pinia Pinia &#xff1a;Vue.js 状态管理库Pinia持久化插件-persist Pinia &#xff1a;Vue.js 状态管理库 Pinia 是 Vue 的专属状态管理库&#xff0c;它允许你跨组件或页面共享状态。 要使用Pinia &#xff0c;先要安装npm install pinia在main.js中导入Pinia 并使用 示例…

day10-字符串

目录 字符串1、API 和 API 帮助文档2、String概述3、String构造方法代码实现 和 内存分析3.1 创建String对象的两种方式3.2 Java的内存模型 4、字符串的比较4.1 号的作用4.2 equals方法的作用 练习5、用户登录6、遍历字符串和统计字符个数7、字符串拼接和翻转8、较难练习-金额转…

从二叉树遍历深入理解BFS和DFS

1. 介绍 1.1 基础 BFS&#xff08;Breadth-First Search&#xff0c;广度优先搜索&#xff09;和 DFS&#xff08;Depth-First Search&#xff0c;深度优先搜索&#xff09;是两种常见的图和树的遍历算法。 BFS&#xff1a;从根节点&#xff08;或起始节点&#xff09;开始&am…

【大数据安全分析】大数据安全分析技术框架与关键技术

在数字化时代&#xff0c;网络安全面临着前所未有的挑战。传统的网络安全防护模式呈现出烟囱式的特点&#xff0c;各个安全防护措施和数据相互孤立&#xff0c;形成了防护孤岛和数据孤岛&#xff0c;难以有效应对日益复杂多变的安全威胁。而大数据分析技术的出现&#xff0c;为…

亚博microros小车-原生ubuntu支持系列 27、手掌控制小车运动

背景知识 本节跟上一个测试类似&#xff1a;亚博microros小车-原生ubuntu支持系列&#xff1a;26手势控制小车基础运动-CSDN博客 都是基于MediaPipe hands做手掌、手指识别的。 为了方便理解&#xff0c;在贴一下手指关键点分布。手掌位置就是靠第9点来识别的。 2、程序说明…

MySQL第五次作业

根据图片内容完成作业 1.建表 &#xff08;1&#xff09;建立两个表:goods(商品表)、orders(订单表) mysql> create table goods( -> gid char(8) primary key, -> name varchar(10), -> price decimal(8,2), -> num int); mysql> create t…

Linux:软硬链接和动静态库

hello&#xff0c;各位小伙伴&#xff0c;本篇文章跟大家一起学习《Linux&#xff1a;软硬链接和动静态库》&#xff0c;感谢大家对我上一篇的支持&#xff0c;如有什么问题&#xff0c;还请多多指教 &#xff01; 如果本篇文章对你有帮助&#xff0c;还请各位点点赞&#xff0…

CSS 组合选择符详解与实战示例

在 Web 开发过程中&#xff0c;CSS 用于定义页面元素的样式&#xff0c;而选择器则帮助我们精确定位需要添加样式的元素。今天我们主要来讲解 CSS 中的组合选择符&#xff0c;它们能够根据 DOM 结构中元素之间的关系来选中目标元素&#xff0c;从而写出结构清晰、易于维护的 CS…

【Linux系统】—— 简易进度条的实现

【Linux系统】—— 简易进度条的实现 1 回车和换行2 缓冲区3 进度条的准备代码4 第一版进度条5 第二版进度条 1 回车和换行 先问大家一个问题&#xff1a;回车换行是什么&#xff0c;或者说回车和换行是同一个概念吗&#xff1f;   可能大家对回车换行有一定的误解&#xff0…

Winform开发框架(蝇量级) MiniFramework V2.1

C/S框架网与2022年发布的一款蝇量级开发框架&#xff0c;适用于开发Windows桌面软件、数据管理应用系统、软件工具等轻量级软件&#xff0c;如&#xff1a;PLC上位机软件、数据采集与分析软件、或企业管理软件&#xff0c;进销存等。适合个人开发者快速搭建软件项目。 适用开发…

win10 llamafactory模型微调相关②

微调 使用微调神器LLaMA-Factory轻松改变大语言模型的自我认知_llamafactory 自我认知-CSDN博客 【大模型微调】使用Llama Factory实现中文llama3微调_哔哩哔哩_bilibili 样本数据集 &#xff08;数据集管理脚本处需更改&#xff0c;见报错解决参考1&#xff09; 自我认知微…

AI大模型随机初始化权重并打印网络结构方法(以Deepseekv3为例,单机可跑)

背景 当前大模型的权重加载和调用&#xff0c;主要是通过在HuggingFace官网下载并使用transformer的库来加以实现&#xff1b;其中大模型的权重文件较大&#xff08;部分>100GB&#xff09;&#xff0c;若只是快速研究网络结构和数据流变化&#xff0c;则无需下载权重。本文…