Matplotlib 视频创建

2023-11-30

编辑:欧内斯特的重要性提供了答案,但是我仍然邀请大家解释,为什么savefig逻辑不同于animation logic.

我想制作视频matplotlib。我浏览了手册和示例,但我就是不明白。 (关于matplotlib,我总是复制示例,因为经过五年的python和两年的mathplotlib,我仍然理解0.0%的matplotlib语法)

六个小时后,这就是我的想法。好吧,我得到的是空视频。不知道为什么。

import os
import math
import matplotlib
matplotlib.use("Agg")
from matplotlib import pyplot as plt
import matplotlib.animation as animation

# Set up formatting for the movie files
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)

numb=100
temp=[0.0]*numb
cont=[0.0]*numb
for i in range(int(4*numb/10),int(6*numb/10)):
    temp[i]=2
    cont[i]=2

fig = plt.figure()
plts=fig.add_subplot(1,1,1)

plts.set_ylim([0,2.1])
plts.get_xaxis().set_visible(False)
plts.get_yaxis().set_visible(False)
ims = []

for i in range(1,10):
    line1, = plts.plot(range(0,numb),temp, linewidth=1, color='black')
    line2, = plts.plot(range(0,numb),cont, linewidth=1, color='red')
#    savefig is here for testing, works perfectly!
#    fig.savefig('test'+str(i)+'.png', bbox_inches='tight', dpi=300)
    ims.append([line1,line2])
    plts.lines.remove(line1)
    plts.lines.remove(line2)
    for j in range(1,10):
        tempa=0
        for k in range (1,numb-1):
            tempb=temp[k]+0.51*(temp[k-1]-2*temp[k]+temp[k+1])
            temp[k-1]=tempa
            tempa=tempb
        temp[numb-1]=0
    for j in range(1,20):
        conta=0
        for k in range (1,numb-1):
            contb=cont[k]+0.255*(cont[k-1]-2*cont[k]+cont[k+1])
            cont[k-1]=conta
            conta=contb
        cont[numb-1]=0

im_ani = animation.ArtistAnimation(fig, ims, interval=50, repeat_delay=3000,blit=True)
im_ani.save('im.mp4', writer=writer)

有人可以帮我弄这个吗?


如果你想要一个不为空的图,主要的想法是不要从图中删除线条。

也就是删除这两行

plts.lines.remove(line1)
plts.lines.remove(line2)

如果删除这两行,输出将如下所示

enter image description here [Link to orginial size animation]

现在有人可能会问,为什么我不需要在每个迭代步骤中删除艺术家,否则所有线条都会立即填充画布?

答案是 ArtistAnimation 负责处理这个问题。它只会显示提供的列表中与给定时间步相对应的艺术家。因此,在 for 循环结束时,您最终会将所有线条绘制到画布上,但一旦动画开始,它们将全部被删除,并且一次仅显示一组艺术家。

在这种情况下,使用循环来保存各个图像当然不是一个好主意,因为最终图像将同时包含所有绘制的线条,

enter image description here

然后,解决方案是运行两次脚本,一次用于动画,另一次在每个时间步中删除线条。或者,也许更好,使用动画本身来创建图像。

im_ani.save('im.png', writer="imagemagick") 

将创建图像为im-<nr>.png在当前文件夹中。需要安装 imagemagick。


I'm trying here to answer the two questions from the comments:

1.我在删除 line1 和 line2 之前附加了它们。但他们在最终结果中仍然消失了。怎么会?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Matplotlib 视频创建 的相关文章

  • 使用子图绘制 pandas 数据框 (subplots=True):放置图例并使用紧凑的布局

    我真的很喜欢 pandas 来处理和分析大数据集 到目前为止 我主要使用 matplotlib 进行绘图 但现在想使用 pandas 自己的绘图功能 基于 matplotlib 因为它需要更少的代码 并且在大多数情况下对我来说似乎足够了 尤
  • 如何抑制 pyinstaller 生成的可执行文件窗口中的所有警告

    我已经使用 pyinstaller 从 python 文件生成了可执行文件 该程序按其应有的方式工作 但在我想隐藏的窗口中出现了一条警告消息 当 python 文件在 IDE 中运行时 以下行会抑制所有警告消息 warnings filte
  • 在子图中显示多个图像

    如何使用 matplotlib 函数 plt imshow image 显示多个图像 例如我的代码如下 for file in images process file def process filename image mpimg imr
  • python 中使用 scipy 截断正态分布

    我正在尝试使用截断正态分布scipy在Python3 我想做一些简单的事情 绘制以 0 5 为中心 范围从 0 到 1 的截断法线的 pdf 我有以下代码行 from scipy import truncnorm import matplo
  • 在 matplotlib 中分割图例

    是否有可能将一个大图例分成多个 通常是 2 个 较小的图例 from pylab import t arange 0 0 2 0 0 01 s sin 2 pi t plot t s linewidth 1 0 label Graph1 g
  • 使用 Julia Plots 访问后端特定功能

    Plots简单而强大 但有时我想对情节的各个元素有更多的控制 以微调其外观 是否可以直接更新后端的绘图对象 例如 对于默认的pyplot后端 我试过了 using Plots p plot sin p o axes 1 xaxis set
  • 如何在seaborn中将matplotlib颜色图设置为调色板

    如何将 matplotlib 颜色图设置为 seaborn 中的调色板 有一个类似的问题here https stackoverflow com questions 57373881 converting a matplotlib colo
  • 当 plt.plot() 调用在其他单元格中完成时, plt.show() 为空

    Context 我必须为物理作业编写一个 Jupyter 笔记本 并且必须绘制一些图表来显示某些数量的演变等 我必须通过每次在代码和 Markdown 单元之间交替来解释我编写的几乎每一行代码 包括plot 电话 这就是我的问题的来源 简而
  • 在 matplotlib 中将 3D 背景更改为黑色

    我在将 3D 图表的背景更改为黑色时遇到问题 这是我当前的代码 当我将facecolor设置为黑色时 它会将图表内部更改为灰色 这不是我想要的 fig plt figure fig set size inches 10 10 ax plt
  • Matplotlib:检查空图

    我有一个循环加载并绘制一些数据 如下所示 import os import numpy as np import matplotlib pyplot as plt for filename in filenames plt figure i
  • matplotlib 中的固定大小矩形?

    Is there any way to plot a rectangle spanning the entire length of the X axis but with a fixed and static height of say
  • 在Python matplotlib中的plot_surface之上绘制单个3D点

    我有一些代码可以使用 matplotlib 在 Python 中绘制 3D 曲面 import math import numpy as np import matplotlib pyplot as plt from pylab impor
  • IPython自动开启matplotlib交互模式

    我遇到了 IPython 的一些新奇怪行为 我只需重新安装我的 miniconda 所以我现在有了新的 IPython 和 Matplotlib 版本 事实证明 IPython 会自动将 matplotlib 切换到交互模式 这具有令人讨厌
  • matplotlib 中没有边缘的阴影矩形补丁

    当尝试将带有填充图案的矩形面片添加到绘图中时 在指定填充值时似乎无法将关键字参数 edgecolor 设置为 none 换句话说 我试图添加一个没有边缘但有图案填充的阴影矩形 这似乎不起作用 仅当我还允许在矩形面片周围绘制边缘时 该图案才会
  • ImportError:无法加载需要“tk”交互式框架的后端“TkAgg”,因为“headless”当前正在运行

    当我使用google colaboratory时 我遇到了这个问题 我在stackoverflow上搜索过它 但很少有人回答 有人可以帮我解决这个问题吗 谢谢 我尝试过多种方式重新安装matplotlib 并安装tk dev 但都不起作用
  • 如何使绘图的 xtick 标签成为简单的绘图?

    我不想用单词或数字作为 x 轴的刻度标签 而是想绘制一个简单的绘图 由直线和圆圈组成 作为每个 x 刻度的标签 这可能吗 如果是这样 在 matplotlib 中处理它的最佳方法是什么 我会删除刻度标签并将文本替换为patches http
  • 基于 Unix ASCII 的命令行图表/绘图工具

    有没有好的命令行 UNIX 图表 绘图 绘图工具 我正在寻找能够在 ASCII 图表上绘制 xy 点的东西 澄清一下 我正在寻找能够以 ASCII 格式输出图形 如 ascii art 风格 的东西 这样我就可以在交互式 shell 会话中
  • 使用自定义颜色渐变填充两条线之间的区域

    我正在做一项几乎已经完成的作业 但我想对其添加一些小改动 尝试使用基于温度的颜色图而不是简单的颜色来填充两条线之间的区域 绘制线条的方式本质上使它们成为独立的实体 所以我知道我可能需要两个彼此相遇或重叠的颜色图来完成此任务 但我不太确定如何
  • Seaborn:带有边缘直方图的 kdeplot

    我正在使用一个kdeplot https seaborn pydata org generated seaborn kdeplot html绘制两个二元分布的密度 如下所示df c and df n有两个 Pandas DataFrame
  • 将 github 上的包安装到 Spyder 中

    我一直在尝试安装并导入mpl finance来自 github 的包 在我的 Spyder 环境中没有成功 我努力了 pip install e git https github com matplotlib mpl finance git

随机推荐