此代码为matplotlib。
=======
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig2 = plt.figure()
x = np.arange(-9, 10)
y = np.arange(-9, 10).reshape(-1, 1)
base = np.hypot(x, y)
ims = []
for add in np.arange(30):
ims.append((plt.pcolor(x, y, base + add, norm=plt.Normalize(0, 50)),))
im_ani = animation.ArtistAnimation(fig2, ims, interval=50, repeat_delay=3000,
blit=True)
#im_ani.save('im.mp4', metadata={'artist':'Guido'})
im_ani.save('basic_example.gif', writer='imagemagick')
plt.show()
我想从plt.pcolor更改为plt.matshow,因为我必须可视化矩阵变化。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
a = np.zeros(3)
b = np.identity(3)
a = np.matrix(a)
b = np.matrix(b)
ims = []
for i in np.arange(5):
w = a + i*b
w = w.tolist()
ims.append(plt.matshow(a + i*b))
ani = animation.ArtistAnimation(fig, ims)
plt.show()
错误信息是:
TypeError
Traceback (most recent call last)
/Users/~/simple_anim_matrix.py in <module>()
152 ims.append(plt.matshow(a + i*b))
153
--> 154 ani = animation.ArtistAnimation(fig, ims)
155
156 #ani.save('demo4.mp4')
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/animation.pyc in __init__(self, fig, artists, *args, **kwargs)
932 # over by the machinery.
933 self._framedata = artists
--> 934 TimedAnimation.__init__(self, fig, *args, **kwargs)
935
936 def _init_draw(self):
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/animation.pyc in __init__(self, fig, interval, repeat_delay, repeat, event_source, *args, **kwargs)
876
877 Animation.__init__(self, fig, event_source=event_source,
--> 878 *args, **kwargs)
879
880 def _step(self, *args):
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/animation.pyc in __init__(self, fig, event_source, blit)
557
558 # Clear the initial frame
--> 559 self._init_draw()
560
561 # Instead of starting the event source now, we connect to the figure's
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/animation.pyc in _init_draw(self)
938 axes = []
939 for f in self.new_frame_seq():
--> 940 for artist in f:
941 artist.set_visible(False)
942 # Assemble a list of unique axes that need flushing
TypeError: 'AxesImage' object is not iterable