Matplotlib:实时动画工作正常,但在保存时显示空白图

时间:2017-07-16 23:57:34

标签: python animation matplotlib ffmpeg cellular-automata

我做了一点森林火灾动画。我的代码就在问题的最后。

在我提出问题之前,这里有一些信息:

  • 没有树:forest[i,j] = 0
  • 一棵树:forest[i,j] = 1
  • 一棵树着火:forest[i,j] = 2

基本上发生的事情是constructforest通过 m 创建一个名为forest的大小为 n 的二维数组,并调用树的占用概率的 p 即可。在setonfire点燃forest之后,forest可能会spreadfire点燃forestfire

当我使用Python promptIPython prompt运行from random import random import numpy as np import matplotlib.pylab as plt import matplotlib.colors as mcolors import matplotlib.animation as animation def hazard(p): r=random() assert p>=0 and p<=1 return r <= p def constructforest(n,m,p): forest = np.zeros((n,n)) for i in xrange(n): for j in xrange(m): if hazard(p): forest[i,j] = 1 return forest def setfire(forest,i,j): forest[i,j] = 2 return forest def spreadfire(forest): n,m=forest.shape c = np.copy(forest) for i in xrange(n): for j in xrange(m): if c[i,j] == 1: Y, X = xrange(max(0,i-1),min(n,i+2)), xrange(max(0,j-1),min(m,j+2)) for y in Y: for x in X: if c[y,x] == 2: forest[i,j] = 2 return forest def canburn(forest): n,m=forest.shape c = np.copy(forest) for i in xrange(n): for j in xrange(m): if c[i,j] == 1: Y, X = xrange(max(0,i-1),min(n,i+2)), xrange(max(0,j-1),min(m,j+2)) for y in Y: for x in X: if c[y,x] == 2: return True return False def forestfire(forest): fig, ax = plt.subplots() movie = [] # Colormap red, green, blue = [(1,0,0,1)], [(0,1,0,1)], [(0,0,1,1)] colors = np.vstack((blue, green, red)) mycmap = mcolors.LinearSegmentedColormap.from_list('my_colormap', colors) # Initialization k = 0 forest = spreadfire(forest) im = plt.imshow(forest, animated=True, cmap = mycmap, interpolation="none", origin='lower') movie.append([im]) # Fire propagation while canburn(forest): k += 1 print k forest = spreadfire(forest) im = plt.imshow(forest, animated=True, cmap = mycmap, interpolation="none", origin='lower') movie.append([im]) return animation.ArtistAnimation(fig, movie, blit=True, repeat_delay=100) ani = forestfire(setfire(constructforest(101,101,0.4),50,50)) ani.save("forestfire_test.mp4", writer = 'ffmpeg', fps=5, dpi=500) 时,我得到了一个不错的动画但是当我去检查我保存的视频文件时,我只看到一个空白的情节。

我做了一些研究,我发现了很多关于这个问题的问题,但我读到的建议都没有帮助:

有人可以告诉我发生了什么吗?

forestfire.py

forestfire_test.mp4

修改

根据@Y.Luo的请求@ImportanceOfBeingErnest在评论中我将matplotlib降级为2.0.0并更改了动画的帧速率,但import { fetchMarkers } from 'path_to_makers_action'; ....Code of react component.... export default connent(null, { fetchMarkers }); 仍显示空白图。

以下是我的设置: enter image description here enter image description here

0 个答案:

没有答案