0

我已经想出了如何使用 matplotlib保存视频动画缩放图片。我现在想合并这两件事并了解如何在动画中引入缩放:阅读一些文档,我注意到对于图片来说并不简单,我希望对于视频来说是相同或更糟。

在下面我写了一个简单的工作代码,你可以在第一个链接上找到

import numpy as np
import matplotlib.pyplot as plt    
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
from matplotlib import cm

#Define x,y vectors and meshgrid with function u on it

x = np.arange(0,10,.1)
y = np.arange(0,10,.1)
X,Y = np.meshgrid(x,y)

#Create a figure and an axis object for the surface

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')

#Animation without axes and with colormap

def animate(n):
    ax.cla()

    u = np.sin(X+Y+(n/10))
    plt.axis('off')
    plt.grid('off')
    ax.plot_surface(X,Y,u,cmap=cm.inferno)

    return fig,     

anim = animation.FuncAnimation(fig,animate,frames=63)
anim.save('A.mp4',fps=20)

这里的输出

在此处输入图像描述

如您所见,放大后还不错,但这还不够,我想要更多!

在我使用的实际代码中,视频动画非常非常小,我不知道为什么,因为它与此非常相似。我也希望这样我可以提高视频质量,那是很差的。谢谢你的帮助。

4

1 回答 1

0

我终于找到了一个原始但有效的解决方案。代码几乎没有变化

import numpy as np
import matplotlib.pyplot as plt    
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
from matplotlib import cm

#Define x,y vectors and meshgrid with function u on it

x = np.arange(0,10,.1)
y = np.arange(0,10,.1)
X,Y = np.meshgrid(x,y)

#Create a figure and an axis object for the surface

fig = plt.figure(frameon=False)
ax = fig.add_subplot(111,projection='3d')

#Definition? and zooming

fig.set_size_inches(10,10)                
fig.subplots_adjust(left=0,right=1,bottom=0,top=1,wspace=None,hspace=None)

#Animation without axes and with colormap

def animate(n):
    ax.cla()

    u = np.sin(X+Y+(n/10))
    plt.axis('off')
    plt.grid('off')
    ax.plot_surface(X,Y,u,cmap=cm.inferno)
    print(n)

    return fig,     

anim = animation.FuncAnimation(fig,animate,frames=63)
anim.save('A.gif',fps=20)

在此处输入图像描述

如您所见,缩放效果很好。质量差是由于我在第二个时刻进行的压缩,因为实际输出的 gif 带有这些参数非常重,实际上质量非常好。

于 2022-02-02T10:46:32.980 回答