gpt4 book ai didi

python - 更新 matplotlib 动画的轮廓

转载 作者:太空狗 更新时间:2023-10-30 01:38:11 24 4
gpt4 key购买 nike

我正在寻找一种方法来更新动画中的轮廓线,而不需要我每次都重新绘制图形。

我发现对这个问题的大多数回答都提倡记忆 ax.contour,但是由于我的轮廓叠加在另一张图片上,所以速度慢得令人无法忍受。

我发现的唯一看起来接近于回答问题的回答是用死链接回答它:Animating a contour plot in matplotlib using FuncAnimation

编辑:this可能是预期的链接。

示例代码:

#!/usr/bin/env python

import matplotlib.pylab as plt
import matplotlib.animation as anim
from matplotlib.colors import LinearSegmentedColormap as lsc
import numpy

#fig = 0; ax = 0; im = 0; co = 0


image_data = numpy.random.random((100,50,50))
contour_data = numpy.random.random((100,50,50))

def init():
global fig, ax, im, co
fig = plt.figure()
ax = plt.axes()
im = ax.imshow(image_data[0,:,:])
co = ax.contour(contour_data[0,:,:])

def func(n):
im.set_data(image_data[n,:,:])
co.set_array(contour_data[n,:,:])

init()
ani = anim.FuncAnimation(fig, func, frames=100)
plt.show()

干杯。

最佳答案

也许你现在已经明白了;不幸的是,您似乎必须重新声明艺术家的整个轮廓/轮廓集在每个时间步移除旧实例。这是从 this link 复制的一些信息:

The set_array() method (I think) only impacts the colormapping information for contourf, and even then doesn't appear to update. What you need to do is make a new contour plot and remove the old one, especially if you need to change the underlying contoured data. This should be as easy as C.remove(), but for some reason, this doesn't exist (I'll go add it in a minute). So instead, you need to do the following:

import matplotlib.pyplot as plt 
import numpy as np

x = np.arange(0, 2 * np.pi, 0.1)
X,Y = np.meshgrid(x,x)
f1 = np.sin(X) + np.sin(Y)
f2 = np.cos(X) + np.cos(Y)

plt.figure()
C = plt.contourf(f1)
plt.show()
for coll in C.collections:
plt.gca().collections.remove(coll)
C = plt.contourf(f2)
plt.draw()

This answer is probably what you're looking for.

关于python - 更新 matplotlib 动画的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23250004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com