Python 中的动画问题
Animation issue in Python
我希望绿色矩形在矩阵 b
中从一个值移动到另一个值时不会消失。例如,矩形约为 0.24671953。然后矩形停留在这个值上。然后另一个矩形出现在下一个值 0.25959473 上。然后0.41092171上又出现了一个矩形,前面两个矩形没有消失
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
import numpy as np
from celluloid import Camera
a = np.array([[0.24671953, 0.25959473, 0.85494718],
[0.60553861, 0.76276659, 0.41092171],
[0.37356358, 0.69378785, 0.46988614]])
b = np.array([[0.24671953,0.25959473],
[0.41092171,0.46988614],
[0.37356358,0.60553861]])
annot=True
fig, ax1 = plt.subplots(1)
camera = Camera(fig)
sns.set_style('white')
ax1 = sns.heatmap(a, linewidth=0.5,ax=ax1,annot=annot)
for bb in b.flatten():
ax1.add_patch(plt.Rectangle((np.where(a == bb)[1][0],
np.where(a == bb)[0][0]), 1, 1, fc='none', ec='green', lw=5, clip_on=False))
camera.snap()
animation = camera.animate(interval=800)
animation.save('animation2.gif')
plt.show()
看起来 celluloid
清除了每个 snap
处的现有地块。您可以在每一步从头开始重新创建绘图。矩形可以存储在列表中。
为避免在每一步都设置新的颜色条位置,您可以使用 sns.heatmap
的 cbar_ax=
参数始终使用相同的颜色条 ax
:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from celluloid import Camera
a = np.array([[0.24671953, 0.25959473, 0.85494718],
[0.60553861, 0.76276659, 0.41092171],
[0.37356358, 0.69378785, 0.46988614]])
b = np.array([[0.24671953, 0.25959473],
[0.41092171, 0.46988614],
[0.37356358, 0.60553861]])
fig, (ax1, cbar_ax) = plt.subplots(ncols=2, gridspec_kw={'width_ratios': [20, 1]})
camera = Camera(fig)
sns.set_style('white')
rectangles = []
for bb in b.flatten():
sns.heatmap(a, linewidth=0.5, ax=ax1, annot=True, cbar_ax=cbar_ax)
rectangles.append(plt.Rectangle((np.where(a == bb)[1][0], np.where(a == bb)[0][0]), 1, 1,
fc='none', ec='green', lw=5, clip_on=False))
for rect in rectangles:
ax1.add_patch(rect)
camera.snap()
animation = camera.animate(interval=800)
animation.save('animation2.gif')
plt.show()
另一种方法是直接使用 matplotlib 的动画框架。然后,代码可能如下所示:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import seaborn as sns
import numpy as np
a = np.array([[0.24671953, 0.25959473, 0.85494718],
[0.60553861, 0.76276659, 0.41092171],
[0.37356358, 0.69378785, 0.46988614]])
b = np.array([[0.24671953, 0.25959473],
[0.41092171, 0.46988614],
[0.37356358, 0.60553861]])
fig, ax1 = plt.subplots()
sns.set_style('white')
sns.heatmap(a, linewidth=0.5, ax=ax1, annot=True)
def animate(i):
bb = b.flatten()[i]
patch = ax1.add_patch(plt.Rectangle((np.where(a == bb)[1][0], np.where(a == bb)[0][0]), 1, 1,
fc='none', ec='green', lw=5, clip_on=False))
return patch,
animation = FuncAnimation(fig, animate, frames=range(0, b.size), interval=800, repeat=False)
animation.save('animation2.gif')
plt.show()
我希望绿色矩形在矩阵 b
中从一个值移动到另一个值时不会消失。例如,矩形约为 0.24671953。然后矩形停留在这个值上。然后另一个矩形出现在下一个值 0.25959473 上。然后0.41092171上又出现了一个矩形,前面两个矩形没有消失
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns
import numpy as np
from celluloid import Camera
a = np.array([[0.24671953, 0.25959473, 0.85494718],
[0.60553861, 0.76276659, 0.41092171],
[0.37356358, 0.69378785, 0.46988614]])
b = np.array([[0.24671953,0.25959473],
[0.41092171,0.46988614],
[0.37356358,0.60553861]])
annot=True
fig, ax1 = plt.subplots(1)
camera = Camera(fig)
sns.set_style('white')
ax1 = sns.heatmap(a, linewidth=0.5,ax=ax1,annot=annot)
for bb in b.flatten():
ax1.add_patch(plt.Rectangle((np.where(a == bb)[1][0],
np.where(a == bb)[0][0]), 1, 1, fc='none', ec='green', lw=5, clip_on=False))
camera.snap()
animation = camera.animate(interval=800)
animation.save('animation2.gif')
plt.show()
看起来 celluloid
清除了每个 snap
处的现有地块。您可以在每一步从头开始重新创建绘图。矩形可以存储在列表中。
为避免在每一步都设置新的颜色条位置,您可以使用 sns.heatmap
的 cbar_ax=
参数始终使用相同的颜色条 ax
:
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from celluloid import Camera
a = np.array([[0.24671953, 0.25959473, 0.85494718],
[0.60553861, 0.76276659, 0.41092171],
[0.37356358, 0.69378785, 0.46988614]])
b = np.array([[0.24671953, 0.25959473],
[0.41092171, 0.46988614],
[0.37356358, 0.60553861]])
fig, (ax1, cbar_ax) = plt.subplots(ncols=2, gridspec_kw={'width_ratios': [20, 1]})
camera = Camera(fig)
sns.set_style('white')
rectangles = []
for bb in b.flatten():
sns.heatmap(a, linewidth=0.5, ax=ax1, annot=True, cbar_ax=cbar_ax)
rectangles.append(plt.Rectangle((np.where(a == bb)[1][0], np.where(a == bb)[0][0]), 1, 1,
fc='none', ec='green', lw=5, clip_on=False))
for rect in rectangles:
ax1.add_patch(rect)
camera.snap()
animation = camera.animate(interval=800)
animation.save('animation2.gif')
plt.show()
另一种方法是直接使用 matplotlib 的动画框架。然后,代码可能如下所示:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import seaborn as sns
import numpy as np
a = np.array([[0.24671953, 0.25959473, 0.85494718],
[0.60553861, 0.76276659, 0.41092171],
[0.37356358, 0.69378785, 0.46988614]])
b = np.array([[0.24671953, 0.25959473],
[0.41092171, 0.46988614],
[0.37356358, 0.60553861]])
fig, ax1 = plt.subplots()
sns.set_style('white')
sns.heatmap(a, linewidth=0.5, ax=ax1, annot=True)
def animate(i):
bb = b.flatten()[i]
patch = ax1.add_patch(plt.Rectangle((np.where(a == bb)[1][0], np.where(a == bb)[0][0]), 1, 1,
fc='none', ec='green', lw=5, clip_on=False))
return patch,
animation = FuncAnimation(fig, animate, frames=range(0, b.size), interval=800, repeat=False)
animation.save('animation2.gif')
plt.show()