提高Matplotlib中的更新速度

2024-09-26 22:08:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用matplotlib的动画函数动态更新矩阵文本。但是我发现如果数据数组太大,动画会变得非常慢。有什么办法改进吗?在

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

fig, ax = plt.subplots(figsize=(10,20))

def updatefig(i):
  plt.cla()
  ax.grid()
  data = np.random.rand(50,50)
  ax.set_xticks(np.arange(data.shape[1]+1))
  ax.set_yticks(np.arange(data.shape[0]+1))
  for y in range(data.shape[0]):
    for x in range(data.shape[1]):
      plt.text(x + 0.5 , y + 0.5, '%.1f' % data[y, x],horizontalalignment='center',verticalalignment='center',color='b',size = 6)
  plt.draw() 

anim = animation.FuncAnimation(fig, updatefig,interval=50)
plt.show()

实际上,我想用下面的链接这样的数据值创建一个heatmmap图。但是使用注释是我唯一能理解的方法。 Heamap with values


Tags: 数据importdatamatplotlibasnpfig动画

热门问题