为 3D 体积绘制 seaborn 热图动画时出错
Error while drawing animation of seaborn heatmap for 3D volume
尝试使用 Seaborn 热图 和 [=19] 可视化两个卷之间的 互相关 img_3D, and mask_3D =]animation 从 Matplotlib 将 3D 互相关结果可视化为 2D 图像的渐进动画,但我遇到了一个错误,你能告诉我如何摆脱这个错误,并可视化热图正确吗?
提前致谢。
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 259, in resize
self.draw()
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_agg.py", line 392, in draw
else nullcontext()):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backend_bases.py", line 2788, in _wait_cursor_for_draw_cm
self.set_cursor(self._lastCursor)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 544, in set_cursor
window.configure(cursor=cursord[cursor])
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1476, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name "."
使用的代码是:
# Import Libraries
#====================================
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import nibabel as nib
from scipy.signal import correlate
import seaborn as sns
sns.set()
#===================================
img = np.load('img.npy')
act = np.load('act.npy')
# Mode : 'full', 'valid', 'same'
result = correlate(img, act,mode='same')
print(img.shape, act.shape, result.shape)
def updatefig(sl):
for sl in range(result.shape[2]):
print(sl,' / ',result.shape[2])
sns.heatmap(result[...,sl],cbar=False)
ax.set_title("frame {}".format(sl))
# Note that using time.sleep does *not* work here!
plt.pause(0.1)
fig, ax = plt.subplots()
ani = FuncAnimation(fig, updatefig, frames=range(result.shape[2]), interval=5, blit=True)
plt.show()
检查此代码:
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.signal import correlate
import seaborn as sns
sns.set()
img = np.load('img.npy')
act = np.load('act.npy')
result = correlate(img, act, mode = 'same')
def updatefig(sl):
ax.cla()
print(sl + 1, ' / ', result.shape[2])
sns.heatmap(result[..., sl], cbar = False)
ax.set_title("frame {}".format(sl + 1))
ax.axis('off')
fig, ax = plt.subplots()
ani = FuncAnimation(fig, updatefig, frames = result.shape[2], interval = 5)
plt.show()
这给了我这个动画(我将下面报告的动画减半以将文件大小减小到 2 MB 以下,上面的代码重现所有 40 帧):
编辑
为了向热图添加固定颜色条,请检查此代码:
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.signal import correlate
import seaborn as sns
sns.set()
img = np.load('img.npy')
act = np.load('act.npy')
result = correlate(img, act, mode = 'same')
def updatefig(sl):
ax.cla()
print(sl + 1, ' / ', result.shape[2])
sns.heatmap(result[..., sl],
ax = ax,
cbar = True,
cbar_ax = cbar_ax,
vmin = result.min(),
vmax = result.max())
ax.set_title("frame {}".format(sl + 1))
ax.axis('off')
grid_kws = {'width_ratios': (0.9, 0.05), 'wspace': 0.2}
fig, (ax, cbar_ax) = plt.subplots(1, 2, gridspec_kw = grid_kws, figsize = (10, 8))
ani = FuncAnimation(fig, updatefig, frames = result.shape[2], interval = 5)
plt.show()
制作这个动画(剪成上一个):
尝试使用 Seaborn 热图 和 [=19] 可视化两个卷之间的 互相关 img_3D, and mask_3D =]animation 从 Matplotlib 将 3D 互相关结果可视化为 2D 图像的渐进动画,但我遇到了一个错误,你能告诉我如何摆脱这个错误,并可视化热图正确吗?
提前致谢。
Traceback (most recent call last):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 259, in resize
self.draw()
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 9, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\backend_agg.py", line 392, in draw
else nullcontext()):
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backend_bases.py", line 2788, in _wait_cursor_for_draw_cm
self.set_cursor(self._lastCursor)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\backends\_backend_tk.py", line 544, in set_cursor
window.configure(cursor=cursord[cursor])
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1485, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1476, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name "."
使用的代码是:
# Import Libraries
#====================================
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import nibabel as nib
from scipy.signal import correlate
import seaborn as sns
sns.set()
#===================================
img = np.load('img.npy')
act = np.load('act.npy')
# Mode : 'full', 'valid', 'same'
result = correlate(img, act,mode='same')
print(img.shape, act.shape, result.shape)
def updatefig(sl):
for sl in range(result.shape[2]):
print(sl,' / ',result.shape[2])
sns.heatmap(result[...,sl],cbar=False)
ax.set_title("frame {}".format(sl))
# Note that using time.sleep does *not* work here!
plt.pause(0.1)
fig, ax = plt.subplots()
ani = FuncAnimation(fig, updatefig, frames=range(result.shape[2]), interval=5, blit=True)
plt.show()
检查此代码:
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.signal import correlate
import seaborn as sns
sns.set()
img = np.load('img.npy')
act = np.load('act.npy')
result = correlate(img, act, mode = 'same')
def updatefig(sl):
ax.cla()
print(sl + 1, ' / ', result.shape[2])
sns.heatmap(result[..., sl], cbar = False)
ax.set_title("frame {}".format(sl + 1))
ax.axis('off')
fig, ax = plt.subplots()
ani = FuncAnimation(fig, updatefig, frames = result.shape[2], interval = 5)
plt.show()
这给了我这个动画(我将下面报告的动画减半以将文件大小减小到 2 MB 以下,上面的代码重现所有 40 帧):
编辑
为了向热图添加固定颜色条,请检查此代码:
import numpy as np
np.random.seed(0)
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.signal import correlate
import seaborn as sns
sns.set()
img = np.load('img.npy')
act = np.load('act.npy')
result = correlate(img, act, mode = 'same')
def updatefig(sl):
ax.cla()
print(sl + 1, ' / ', result.shape[2])
sns.heatmap(result[..., sl],
ax = ax,
cbar = True,
cbar_ax = cbar_ax,
vmin = result.min(),
vmax = result.max())
ax.set_title("frame {}".format(sl + 1))
ax.axis('off')
grid_kws = {'width_ratios': (0.9, 0.05), 'wspace': 0.2}
fig, (ax, cbar_ax) = plt.subplots(1, 2, gridspec_kw = grid_kws, figsize = (10, 8))
ani = FuncAnimation(fig, updatefig, frames = result.shape[2], interval = 5)
plt.show()
制作这个动画(剪成上一个):