保存到视频时,在matplotlib中暂停动画

时间:2019-06-07 07:12:18

标签: python matplotlib animation

首先,此代码完全按照我的意愿运行。问题是当将动画另存为MP4时,视频将完全忽略暂停。有什么办法可以写暂停,以便在另存为视频时显示出来?是编解码器问题吗?

import time
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker
import matplotlib.animation as animation
from tkinter import *

# Setup a plot such that only the bottom spine is shown
def setup(axx):
    axx.spines['right'].set_visible(False)
    axx.spines['left'].set_visible(False)
    axx.spines['top'].set_visible(False)
    ax.spines['bottom'].set_color('#ffffff')
    axx.xaxis.set_ticks_position('bottom')
    axx.yaxis.set_major_locator(ticker.NullLocator())
    axx.tick_params(which='major', width=3, length=20, colors='#ffffff', labelsize = 12)
    axx.tick_params(which='minor', width=1.5, length=10, colors='#ffffff')
    axx.xaxis.set_major_locator(ticker.MultipleLocator(0.05))
    axx.xaxis.set_minor_locator(ticker.MultipleLocator(0.01))
    ax.xaxis.label.set_color('#ffffff')
    axx.set_ylim(0, 1)
    axx.patch.set_alpha(0.0)
    axx.set_xlim(1.2, 1.3)

def animate1(i):
    step1 = (1.3-1.283)/99  
    if i == 0:
        anim1.event_source.stop()
        time.sleep(3)
        anim1.event_source.start()
    xpos1 = i * step1 + 1.283 
    point.set_xdata([xpos1])
    if i == 99:
        ax.text(0.975, 0.13, 1.3, fontsize=12, color='#ffff80', transform=ax.transAxes)
    return ax,

mpl.rcParams['axes.linewidth'] = 3
mpl.rcParams["savefig.facecolor"] = "#2e5090"
fig = plt.figure(figsize=(8,2))
fig.patch.set_facecolor('#2e5090')  #So the preview background color matches the saved animation
ax = fig.add_subplot(1, 1, 1)
x = 1.283
setup(ax)
pos1 = ax.get_position() # get the original position 
pos2 = [pos1.x0, pos1.y0 + 0.4,  pos1.width, pos1.height] 
ax.set_position(pos2) # set a new position
ax.text(0.79, 0.13, format(x, '.3f'), fontsize=12, color='#ffffff', transform=ax.transAxes)
point, = ax.plot(x, 0.055, color='#ffff80', marker='v', markersize='8')
firstpoint = ax.plot(x, 0.055, color='#ffffff', marker='v', markersize='8')
anim1 = animation.FuncAnimation(fig, animate1, frames=100, interval=1, blit=False, repeat = False)  
anim1.save('movingpoint.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

0 个答案:

没有答案