【问题标题】:Animate matshow function in matplotlibmatplotlib 中的动画 matshow 函数
【发布时间】:2016-10-14 09:01:12
【问题描述】:

我有一个时间相关的矩阵,我想将演变绘制为动画。

我的代码如下:

import numpy as np
import matplotlib.pyplot as plt
from  matplotlib.animation import FuncAnimation


n_frames = 3 #Numero de ficheros que hemos generado
data = np.empty(n_frames, dtype=object) #Almacena los datos

#Leer todos los datos
for k in range(n_frames):
    data[k] = np.loadtxt("frame"+str(k))


fig = plt.figure()
plot =plt.matshow(data[0])

def init():
    plot.set_data(data[0])
    return plot

def update(j):
    plot.set_data(data[j])
    return [plot]


anim = FuncAnimation(fig, update, init_func = init, frames=n_frames, interval = 30, blit=True)

plt.show()

但是,当我运行它时,我总是收到以下错误:draw_artist can only be used after an initial draw which caches the render。我不知道这个错误来自哪里,也不知道如何解决它。 我已经阅读了this answerthis article,但仍然不知道为什么我的代码不起作用。

感谢您的帮助,谢谢!

【问题讨论】:

    标签: python animation matplotlib


    【解决方案1】:

    您已经非常接近可行的解决方案了。要么改变

    plot = plt.matshow(data[0])
    

    plot = plt.matshow(data[0], fignum=0)
    

    或使用

    plot = plt.imshow(data[0])
    

    改为。


    在这里使用plt.matshow(data[0]) 的问题是,如果fignum 参数留空(即默认等于None),则creates a new figure。 因为fig = plt.figure() 被调用并且fig 被传递给FuncAnimation,你最终得到两个数字,一个是plt.matshow 的结果,另一个是FuncAnimation 绘制的空白。 FuncAnimation 正在绘制的图形没有找到初始绘制,因此它会引发

    AttributeError: draw_artist can only be used after an initial draw which caches the render
    

    【讨论】:

      猜你喜欢
      • 2011-04-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2021-10-27
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      相关资源
      最近更新 更多