在我提出问题之前,这里有一些信息:
forest[i,j] = 0
forest[i,j] = 1
forest[i,j] = 2
基本上发生的事情是constructforest
通过 m 创建一个名为forest
的大小为 n 的二维数组,并调用树的占用概率的 p 即可。在setonfire
点燃forest
之后,forest
可能会spreadfire
点燃forestfire
。
当我使用Python prompt
或IPython 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_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 });
仍显示空白图。