gpt4 book ai didi

python - 如何在 Funcanimation 中获得 fill_between 形状?

转载 作者:太空宇宙 更新时间:2023-11-04 02:48:58 25 4
gpt4 key购买 nike

我想制作一个移动图,在绘制曲线时曲线下方的区域会变色。

我用谷歌搜索了一下,发现我应该以某种方式创建一个补丁。但是,我不明白他们给出的任何例子,所以让我在这里用我的具体例子问一下:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import pylab as p

data = np.loadtext('datafile.dat', delimiter=',')
A = data[:,1]
B = data[:,2]

fig = plt.figure(figsize=(25,5), dpi=80)
ax = plt.axes(xlim=(0, 3.428), ylim=(-1,1))
line, = ax.plot([], [], lw=5)

def init():
line.set_data([], [])
return line,

def animate(i):
x = A[0:(i-1)*400]
y = B[0:(i-1)*400]
line.set_data(x,y)
# Here is the problem. I would now like to add the following line
# p.fill_between(x, 0, y, facecolor = 'C0', alpha = 0.2)
return line,

anim = animation.FuncAnimation(fig,animate, init_func=init, frames = 857, interval=20, blit=True)

我希望有人能为我的问题提供解决方案,或者至少为我指明正确的方向。

所以我的问题是:如何添加评论部分而不会出现任何错误?

最佳答案

假设您想要 blit = True,您还需要返回 fill_between 生成的补丁。

p = plt.fill_between(x, y, 0)
return line, p,

完整的工作示例:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

X = np.linspace(0,3.428, num=250)
Y = np.sin(X*3)

fig = plt.figure(figsize=(13,5), dpi=80)
ax = plt.axes(xlim=(0, 3.428), ylim=(-1,1))
line, = ax.plot([], [], lw=5)

def init():
line.set_data([], [])
return line,

def animate(i):
x = X[0:(i-1)]
y = Y[0:(i-1)]
line.set_data(x,y)
p = plt.fill_between(x, y, 0, facecolor = 'C0', alpha = 0.2)
return line, p,

anim = animation.FuncAnimation(fig,animate, init_func=init,
frames = 250, interval=20, blit=True)

plt.show()

关于python - 如何在 Funcanimation 中获得 fill_between 形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44413223/

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