Python 如何制作海洋生物动画';是热图还是相关矩阵?
我对python比较陌生(来自Matlab)。作为一个项目,我正在尝试创建一个随时间变化的相关矩阵的动画绘图。为了使情节好看,我正在尝试seaborn。我很难完成动画(mac上的Matplotlib后端有问题),但是现在一个非常基本的动画可以使用来自web的以下代码工作:Python 如何制作海洋生物动画';是热图还是相关矩阵?,python,animation,matplotlib,heatmap,seaborn,Python,Animation,Matplotlib,Heatmap,Seaborn,我对python比较陌生(来自Matlab)。作为一个项目,我正在尝试创建一个随时间变化的相关矩阵的动画绘图。为了使情节好看,我正在尝试seaborn。我很难完成动画(mac上的Matplotlib后端有问题),但是现在一个非常基本的动画可以使用来自web的以下代码工作: import numpy as np from matplotlib import pyplot as plt from matplotlib import animation nx = 50 ny = 50 fig =
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
nx = 50
ny = 50
fig = plt.figure()
data = np.random.rand(nx, ny)
im = plt.imshow(data)
def init():
im.set_data(np.zeros((nx, ny)))
def animate(i):
#xi = i // ny
#yi = i % ny
data = np.random.rand(nx, ny)
im.set_data(data)
return im
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50, repeat = False)
现在,我正试图将其应用于seaborn
,但没有成功。似乎seaborn在子地块上工作,要使这些子地块动画化要困难得多。我曾经得到的最好的东西是一种递归绘图,其中,seaborn。热图
相互叠加。此外,im.set\u data
方法不可用
非常感谢您的建议。我将
plt.imshow
(通过设置的铸造数据
无效)替换为seaborn.heatmap
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import animation
fig = plt.figure()
data = np.random.rand(10, 10)
sns.heatmap(data, vmax=.8, square=True)
def init():
sns.heatmap(np.zeros((10, 10)), vmax=.8, square=True, cbar=False)
def animate(i):
data = np.random.rand(10, 10)
sns.heatmap(data, vmax=.8, square=True, cbar=False)
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=20, repeat = False)
这就产生了我一直在挣扎的递归图 下面是一个完整的示例(使用Matplotlib 3.0.3进行测试)
根据r schmaelzle的答案,我制作了带有注释的动画海本热图
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import animation
class Heatmap:
def __init__(self):
self.fig, self.ax = plt.subplots()
self.anim = None
def animate(self):
def init():
sns.heatmap(np.zeros((10, 10)), vmax=.8, ax=self.ax)
def animate(i):
self.ax.texts = []
sns.heatmap(np.random.rand(10, 10), annot=True, vmax=.8, cbar=False, ax=self.ax)
self.anim = animation.FuncAnimation(self.fig, animate, init_func=init, frames=20, repeat=False)
if __name__ == '__main__':
hm = Heatmap()
hm.animate()
更新注释的技巧是将ax.text=[]设为空
我希望它能帮助别人!:) 除了上面的答案之外,我还想从数据帧列表中执行此操作,并将其另存为gif。所以,使用你的代码和Serenity的答案
谢谢 我想我刚刚找到了一个解决方案:将plt.clf()放在animate函数的第一行现在就可以了!谢谢,拉尔夫。我在这里添加了你的内容,作为对你的问题的编辑,这就是问题的实际归属,并将你的答案编辑成实际答案。这不起作用,因为每次迭代你的代码都会在图形上生成另一个比例尺。
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import animation
class Heatmap:
def __init__(self):
self.fig, self.ax = plt.subplots()
self.anim = None
def animate(self):
def init():
sns.heatmap(np.zeros((10, 10)), vmax=.8, ax=self.ax)
def animate(i):
self.ax.texts = []
sns.heatmap(np.random.rand(10, 10), annot=True, vmax=.8, cbar=False, ax=self.ax)
self.anim = animation.FuncAnimation(self.fig, animate, init_func=init, frames=20, repeat=False)
if __name__ == '__main__':
hm = Heatmap()
hm.animate()
fig = plt.figure()
def init():
sns.heatmap(np.zeros((10, 10)), vmax=.8, square=True, cbar=False)
def animate(i):
data = data_list[i]
sns.heatmap(data, vmax=.8, square=True, cbar=False)
data_list = []
for j in range(20):
data = np.random.rand(10, 10)
data_list.append(data)
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=20, repeat = False)
savefile = r"test3.gif"
pillowwriter = animation.PillowWriter(fps=20)
anim.save(savefile, writer=pillowwriter)
plt.show()