Python 动画阴影 matplotlib

标签 python animation matplotlib shading

所以我正在尝试创建一些随时间移动的高斯图的动画。我的基本动画也按我的预期工作。但后来我想遮蔽高斯曲线下的区域。 我用 ax.fill 函数来遮蔽它。但相反,它只是遮蔽了创建曲线的绘图矢量,它变得一团糟。任何建议或帮助将不胜感激

import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import matplotlib.animation as animation
import pylab as p

Gamma=0.0005
q=1.6e-19
m=0.067*9e-31
B=10
Ec=(1.0567e-34)*B/m
#e=2.78

#E0=0+(1.0567e-34)*x*i/m

fig, ax = plt.subplots()

n = 3 #number of lines
x = np.arange(0, 3.6e-3, 1.7e-5)        # x-array, third number is interval here, x is energy
lines = [ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2))[0]for _ in range(n)]


def animate(i):
    for d, line in enumerate(lines):
        p=(d+1)/2.
        line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2))
        ax.fill(x,(np.e**(-(x-((1.0567e-34)*p*i/m))**2/Gamma**2)), "b")# update the data
    return lines


#Init only required for blitting to give a clean slate.
def init():
    for line in lines:
        line.set_ydata(np.ma.array(x, mask=True))
    return lines

ani = animation.FuncAnimation(fig, animate, np.arange(0, 2.5, .01), init_func=init,
    interval=10, blit=True)
Writer = animation.writers['ffmpeg']
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)

ani.save('QHanimati.mp4', writer=writer)



plt.show()

所以当前的代码运行一个动画,给出这样的东西。 Curve

我希望它使曲线上方的区域保持白色。

谢谢。

最佳答案

您需要注意,保存的动画是 blit=False 时将显示的动画。所以发生的是填充全部添加到 Canvas 上。

一个解决方案是在添加下一个之前删除前一个(因为像 set_data 这样的东西不直接存在用于填充)。

同样,我们将创建一个填充列表,并实际使用 fill_between,而不是 fill,我们可以使用它来删除填充并向其中添加新填充。

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


Gamma=0.0005
q=1.6e-19
m=0.067*9e-31
B=10
Ec=(1.0567e-34)*B/m

fig, ax = plt.subplots()

n = 3 #number of lines
x = np.arange(0, 3.6e-3, 1.7e-5)        # x-array, third number is interval here, x is energy
lines = [ax.plot(x, np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2), zorder=i+3)[0] for i in range(n)]
fills = [ax.fill_between(x,0,(np.e**(-(x-((1.0567e-34)*1*1/m))**2/Gamma**2)), facecolor=lines[i].get_color(), zorder=i+3) for i in range(n)]


def animate(i):
    for d, line in enumerate(lines):
        p=(d+1)/2.
        line.set_ydata(np.e**((-(x-((1.0567e-34)*p*i/m))**2)/Gamma**2))
        fills[d].remove()
        fills[d] = ax.fill_between(x,0,(np.e**(-(x-((1.0567e-34)*p*i/m))**2/Gamma**2)), facecolor=lines[d].get_color(), zorder=d+3)# update the data

    return lines + fills


#Init only required for blitting to give a clean slate.
def init():
    for line in lines:
        line.set_ydata(np.ma.array(x, mask=True))
    return lines

ani = animation.FuncAnimation(fig, animate, np.arange(0, 2.5, .01), init_func=init,
    interval=10, blit=True)
#Writer = animation.writers['ffmpeg']
#writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)
#
#ani.save('QHanimati.mp4', writer=writer)

plt.show()

enter image description here

关于Python 动画阴影 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707395/

相关文章:

python - 我的 Python 代码在尝试登录时无法运行?

ios - 打开相册动画

python - 将 pandas DataFrame 图插入 matplotlib 子图中

objective-c - 移动 UIView

python - 有没有办法在 VSCode 的新窗口中打开图形?

python - Matplotlib:子图中不同的x轴标签

python - 如何检测文本阴影

python - 神经网络没有经过训练,交叉熵保持不变

python - 根据最后一次出现的分隔符将字符串拆分为 2

jquery - 循环播放动画然后停止