Why does my Game of Life simulation slow down to a crawl within seconds? Matplotlib to blame?

后端 未结 2 401
南方客
南方客 2021-01-25 07:57

I\'m learning OOP in python and so for a bit of fun I bashed out a GameOfLife simulator this morning. When it starts up it runs at about 20 cycles per second (due to the p

2条回答
  •  春和景丽
    2021-01-25 08:25

    Before reusing the canvas, you should clear the old figure. You can use matplotlib.pyplot.clf() to clear the current figure (http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.clf):

    def plot(self):
        plt.clf()  # Clear the old figure
        im = plt.imshow(self.matrix)
        plt.pause(0.05)
    

    Hope this helps! :)

提交回复
热议问题