在matplotlib中同步动画绘制图+图像子图

qmb5sa22  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(166)

我有一幅包含曲线图和相应图像的图。我希望这两幅图能够同步移动-a这样图像的白色区域就可以跟随曲线中三条曲线匹配的位置。(好奇的话,这只是多波长干涉图的一个简单模拟。)
图中的三条曲线被逐点复制到图像帧的R、G和B通道中。因此,我希望这两条曲线自然同步。然而,在动画中,可以看到曲线的移动速度比图像中的颜色快。
在尝试了一些调整(图像的高宽比,改变图像的“范围”等)之后,我到目前为止都未能定位问题。

from numpy import *
import matplotlib.pyplot as plt
from matplotlib import animation

npts = 501
wavelength_blue = 0.45
wavelength_green = 0.55
wavelength_red = 0.65
z = 1.5 * linspace(-0.75, 0.75, npts)      ## distance in microns
nframes = 100
maxshift = 0.55     ## in microns of distance
img_height = 100
img = 255 * ones((img_height,npts,3), 'uint8')

(fig,axes) = plt.subplots(2, num='propagation_phase_shifted')
p1 = axes[0].plot([], [], 'b-')
p2 = axes[0].plot([], [], 'g-')
p3 = axes[0].plot([], [], 'r-')
axes[0].set_xlim((-0.8,0.8))
axes[0].set_ylim((-0.1,1.1))
p4 = axes[1].imshow(img, extent=[-0.8,0.8,-0.1,1.1], aspect=1/3)

axes[0].xaxis.set_label_position('top')
axes[0].xaxis.set_ticks_position('top')
axes[0].set_xlabel('z-distance (um)')
axes[0].set_ylabel('wave amplitude')
axes[0].tick_params(axis='x', direction='out')
plt.subplots_adjust(hspace=0.05)

def animate(n):
    shift = (n / (nframes - 1.0)) * maxshift
    phi_red = 2.0 * pi * (z-shift) / wavelength_red
    phi_green = 2.0 * pi * (z-shift) / wavelength_green
    phi_blue = 2.0 * pi * (z-shift) / wavelength_blue

    y_red = 0.5 * (1.0 + cos(phi_red))
    y_green = 0.5 * (1.0 + cos(phi_green))
    y_blue = 0.5 * (1.0 + cos(phi_blue))

    for x in range(img_height):
        img[x,:,0] = uint8(255 * y_red)
        img[x,:,1] = uint8(255 * y_green)
        img[x,:,2] = uint8(255 * y_blue)

    p1[0].set_data(z, y_red)
    p2[0].set_data(z, y_green)
    p3[0].set_data(z, y_blue)
    p4.set_data(img)
    return(p1[0],p2[0],p3[0],p4)

anim = animation.FuncAnimation(fig, animate, frames=nframes, interval=20, blit=True)
FFwriter = animation.FFMpegWriter(fps=30)
anim.save('result.mp4', writer=FFwriter)

plt.show()

nqwrtyyt

nqwrtyyt1#

我认为当你把距离转换成微米时,问题就出现了。同样的数据在上面的数字-1.125..1.125(1.5*0.75=1.125)和下面的数字-0.8..0.8之间拉伸。然后上面的数字看起来移动得更快。
固定间隔应该可以,而且如果我没记错的话,你在上图中调换了红色和蓝色:

from numpy import *
import matplotlib.pyplot as plt
from matplotlib import animation

npts = 501
wavelength_blue = 0.45
wavelength_green = 0.55
wavelength_red = 0.65
z = 1.5 * linspace(-0.75, 0.75, npts)      ## distance in microns
nframes = 100
maxshift = 0.55     ## in microns of distance
img_height = 100
img = 255 * ones((img_height,npts,3), 'uint8')

(fig,axes) = plt.subplots(2, num='propagation_phase_shifted')
p1 = axes[0].plot([], [], 'r-')     # <--------- replacing b with r
p2 = axes[0].plot([], [], 'g-')
p3 = axes[0].plot([], [], 'b-')     # <--------- replacing r with b
axes[0].set_xlim((-1.125,1.125))    # <--------- fixing interval
axes[0].set_ylim((-0.1,1.1))
p4 = axes[1].imshow(img, extent=[-1.125,1.125,-0.1,1.1], aspect=1/3)    # <--------- fixing interval

axes[0].xaxis.set_label_position('top')
axes[0].xaxis.set_ticks_position('top')
axes[0].set_xlabel('z-distance (um)')
axes[0].set_ylabel('wave amplitude')
axes[0].tick_params(axis='x', direction='out')
plt.subplots_adjust(hspace=0.05)

def animate(n):
    shift = (n / (nframes - 1.0)) * maxshift
    phi_red = 2.0 * pi * (z-shift) / wavelength_red
    phi_green = 2.0 * pi * (z-shift) / wavelength_green
    phi_blue = 2.0 * pi * (z-shift) / wavelength_blue

    y_red = 0.5 * (1.0 + cos(phi_red))
    y_green = 0.5 * (1.0 + cos(phi_green))
    y_blue = 0.5 * (1.0 + cos(phi_blue))

    for x in range(img_height):
        img[x,:,0] = uint8(255 * y_red)
        img[x,:,1] = uint8(255 * y_green)
        img[x,:,2] = uint8(255 * y_blue)

    p1[0].set_data(z, y_red)
    p2[0].set_data(z, y_green)
    p3[0].set_data(z, y_blue)
    p4.set_data(img)
    return(p1[0],p2[0],p3[0],p4)

anim = animation.FuncAnimation(fig, animate, frames=nframes, interval=20, blit=True)
FFwriter = animation.FFMpegWriter(fps=30)
anim.save('result.mp4', writer=FFwriter)

plt.show()

相关问题