提高Matplotlib的更新速度

时间:2015-07-04 10:03:12

标签: python matplotlib

我想使用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()

实际上,我想创建一个热图贴图,其数据值如下面的链接。但是使用注释是我能想到的唯一方法。 Heamap with values

1 个答案:

答案 0 :(得分:0)

通过导入seaborn模块找到解决方法。 但是如何避免图表一直闪烁

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

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

def updatefig(i):
  plt.clf()
  data = np.random.rand(10,10)
  sns.heatmap(data, annot=True, linewidths=.5,cbar=False)

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