如何保存已保存的mp4,使其与plot.show()的输出完全匹配?

时间:2019-05-10 16:09:04

标签: python matplotlib ffmpeg

当我尝试使用 ffmpeg 将动画结果保存到mp4时,我感到一团糟。

plt.show()精确显示了我想要在动画中显示的内容。但是,当我使用 ffmpeg 保存它时,结果与 plt.show()返回的结果非常不同。我已经尝试过各种 fps 等参数,但没有任何帮助。

%matplotlib
import pandas as pd
import matplotlib as mpl ## uncomment this if you are running this on a Mac
#mpl.use('TkAgg')         ## and want to use blit=True
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 
import numpy as np 
import csv

people = ('','Jim', 'Dan')
plt.rcdefaults()
fig, ax = plt.subplots()
y_pos = np.arange(len(people))

ax.set_xlim(0,10)
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() 
ax.set_xlabel('Skill')
titleList=['Basketball','Hockey','Baseball']
df=[[0,5,7],[0,4,9],[0,2,6]]
def animate(i):
   # Example data
    while i<3:
        ax.set_yticks(y_pos)
        ax.set_yticklabels(people)
        ax.set_xlabel(titleList[i])
        performance=df[i]

        title = ax.text(0.5,0.95,str(titleList[i]), bbox={'facecolor':'w', 'alpha':0.5, 'pad':5},transform=ax.transAxes, ha="center")

        rects = ax.barh(y_pos, performance, align='center',
                color='blue', ecolor='None')
        return [rect for rect in rects] + [title]


ani = animation.FuncAnimation(fig,animate, frames=3, blit=True
                            ,interval=2000,repeat=False)

plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe'
Writer = animation.writers['ffmpeg']
ani.save('test.mp4')
plt.show() 

结果是一个非常快的视频,其中所有数据都被覆盖(类似于 blt = False 时的 plt.show()结果)。

0 个答案:

没有答案