1

I'd like to update a matrix text in dynamic by using animation function of matplotlib. But I found that if the data array is too large , the animation will become very very slow. Is there any way to improve it ?

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()

Actually, I wants to create a heatmmap plot with data values like below link. But use annotations is the only way i could figure out. Heamap with values

CH Yang
  • 11
  • 3
  • Those are a lot of annotations.. you probably are not going to see much with 2500 points annotated in a single plot (unless the plot is huge and the data points are sparse). Anyway, I think matplotlib doesn't have a mechanism for that.. you can try [this](http://stackoverflow.com/a/14434334/764322), but I believe the result will be the same. What is slowing down your animation is iterating over all your 2500 points in python. – Imanol Luengo Jul 04 '15 at 10:52

1 Answers1

0

Find a workaround by import seaborn module. But how to avoid the graph keep flashing

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()
CH Yang
  • 11
  • 3