Python matblotlib通过循环颜色为matshow设置动画
我想通过更改颜色贴图来设置矩阵图的动画。但我无法让它运行 这是我的代码:Python matblotlib通过循环颜色为matshow设置动画,python,matplotlib,Python,Matplotlib,我想通过更改颜色贴图来设置矩阵图的动画。但我无法让它运行 这是我的代码: import matplotlib.pyplot as plt import numpy as np from matplotlib import cm from matplotlib.colors import ListedColormap, LinearSegmentedColormap from matplotlib.animation import FuncAnimation def rotate(l, n)
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
from matplotlib.animation import FuncAnimation
def rotate(l, n):
return l[n:] + l[:n]
def samplemat(dims):
aa = np.zeros(dims)
for i in range(min(dims)):
for j in range(min(dims)):
aa[i, j] = (i+1)*(j+1)
return aa
min_val, max_val = 0, 15
fig = plt.figure()
mat = samplemat((15, 15))
colmapV = plt.cm.Blues(list(range(0, max_val*max_val)))
colmap = ListedColormap(colmapV)
ms = plt.matshow(mat, cmap=colmap)
def animate(i):
global colmapV
colmapV = rotate(colmapV, 1)
colmap = ListedColormap(colmapV)
ms = plt.matshow(mat, cmap=colmap)
return ms,
# create animation using the animate() function
myAnimation = FuncAnimation(fig, animate, frames=100, interval=100, blit=True, repeat=True)
plt.show()
它失败了
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python3\lib\tkinter\__init__.py", line 1884, in __call__
return self.func(*args)
File "C:\Python3\lib\tkinter\__init__.py", line 805, in callit
func(*args)
File "C:\Python3\lib\site-packages\matplotlib\backends\_backend_tk.py", line 99, in _on_timer
TimerBase._on_timer(self)
File "C:\Python3\lib\site-packages\matplotlib\backend_bases.py", line 1194, in _on_timer
ret = func(*args, **kwargs)
File "C:\Python3\lib\site-packages\matplotlib\animation.py", line 1426, in _step
still_going = Animation._step(self, *args)
File "C:\Python3\lib\site-packages\matplotlib\animation.py", line 1157, in _step
self._draw_next_frame(framedata, self._blit)
File "C:\Python3\lib\site-packages\matplotlib\animation.py", line 1177, in _draw_next_frame
self._post_draw(framedata, blit)
File "C:\Python3\lib\site-packages\matplotlib\animation.py", line 1200, in _post_draw
self._blit_draw(self._drawn_artists)
File "C:\Python3\lib\site-packages\matplotlib\animation.py", line 1222, in _blit_draw
a.axes.draw_artist(a)
File "C:\Python3\lib\site-packages\matplotlib\axes\_base.py", line 2760, in draw_artist
raise AttributeError("draw_artist can only be used after an "
AttributeError: draw_artist can only be used after an initial draw which caches the renderer
有人给我一个暗示吗
另一个小问题是,我总是得到两个数字窗口,但如果我删除数字调用,我不知道我应该传递到
FuncAnimation
,这是一个奇怪的错误,而得到两个数字的事实也很奇怪。
你可以通过显式地创建轴来修复它,但是我不认为你改变颜色贴图的方式是正确的。以下是我最后得到的代码:
导入matplotlib.pyplot作为plt
将numpy作为np导入
从matplotlib.colors导入LinearSegmentedColormap
从matplotlib.animation导入FuncAnimation
def采样垫(dims):
aa=np.零(dims)
对于范围内的i(最小值(dims)):
对于范围内的j(最小(dims)):
aa[i,j]=(i+1)*(j+1)
返回aa
最小值,最大值=0,15
图=plt.图()
ax=图添加_子批次(111)
mat=样本mat((15,15))
cmap=plt.get\u cmap('Blues'))
ms=ax.matshow(mat,cmap=cmap)
def动画(i,out):
out=np.滚动(out,i)
new_cmap=LinearSegmentedColormap。来自_列表(“,cmap(out))
ms.set\U cmap(新cmap)
返回ms,
#使用animate()函数创建动画
myAnimation=FuncAnimation(图,动画,帧=max_val*max_val,间隔=100,blit=True,repeat=True,
fargs=(np.arange(0,最大值*最大值),)
plt.show()
(我不得不降低分辨率以适应文件大小的要求)这是一个奇怪的错误,而且得到两个数字的事实也很奇怪。 你可以通过显式地创建轴来修复它,但是我不认为你改变颜色贴图的方式是正确的。以下是我最后得到的代码:
导入matplotlib.pyplot作为plt
将numpy作为np导入
从matplotlib.colors导入LinearSegmentedColormap
从matplotlib.animation导入FuncAnimation
def采样垫(dims):
aa=np.零(dims)
对于范围内的i(最小值(dims)):
对于范围内的j(最小(dims)):
aa[i,j]=(i+1)*(j+1)
返回aa
最小值,最大值=0,15
图=plt.图()
ax=图添加_子批次(111)
mat=样本mat((15,15))
cmap=plt.get\u cmap('Blues'))
ms=ax.matshow(mat,cmap=cmap)
def动画(i,out):
out=np.滚动(out,i)
new_cmap=LinearSegmentedColormap。来自_列表(“,cmap(out))
ms.set\U cmap(新cmap)
返回ms,
#使用animate()函数创建动画
myAnimation=FuncAnimation(图,动画,帧=max_val*max_val,间隔=100,blit=True,repeat=True,
fargs=(np.arange(0,最大值*最大值),)
plt.show()
(我不得不降低分辨率以适应文件大小要求)是的,我只是在转换问题上绊倒了一下。奇怪-它在我的测试列表上起作用,但在arraysyes上似乎起作用不同。是的,我只是在不断变化的问题上绊倒了一下。奇怪-它在我的测试列表上工作,但在数组上似乎工作不一样