Matplotlib : Live animation works fine but displays a blank plot when being saved

自作多情 提交于 2019-12-11 14:29:47

问题


I made a little forest fire animation. My code is at the end of the question.

Here is some information before I ask my question :

  • No tree : forest[i,j] = 0
  • A tree : forest[i,j] = 1
  • A tree on fire : forest[i,j] = 2

Basically what happens is that constructforest creates a 2 dimensional array called forest of size n by m with a probability of tree occupancy called p. After that setonfire sets on fire the forest and while the forest can burn spreadfire spread the fire.

When I run forestfire with the Python prompt or the IPython prompt I get a nice animation but when I go check the video file that I saved I only see a blank plot.

I did some research, I found many questions about this issue but none of the advice I read was helpful:

  • matplotlib animation produces a blank
  • Matplotlib animation not working in IPython Notebook (blank plot)
  • Animation from matplotlib not working in spyder
  • Spyder Python Animation not working

Can someone tell me what is going on please ?

forestfire.py

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)

EDIT

As requested by @Y.Luo by @ImportanceOfBeingErnest in the comments I downgraded matplotlib to 2.0.0 and I changed the framerate of the animation but forestfire_test.mp4 still displays a blank plot.

Here are my settings:

来源:https://stackoverflow.com/questions/45134356/matplotlib-live-animation-works-fine-but-displays-a-blank-plot-when-being-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!