滑块不工作并更新图中的值

Slider not working and updating values in graph

当我更改滑块时,以下代码不会更新图表。滑块由我想更改的两个变量 Ao 和 Au 组成。最初 Ao 设置为 600,Au 设置为 800。随着滑块的移动,我预计图形会发生变化,但这是行不通的。我在做什么错?如果可能的话,请告诉我制作四个滑块的方法,当我改变它们时它们可以一起工作,它们是 Ao、Au、Bo 和 Bu。

import numpy as np
from matplotlib.widgets import Slider, Button, RadioButtons

import numpy as np
import matplotlib.pyplot as plt


Jo=6.5
Ju=6.5
I=2.5
deltaJ=Jo-Ju
Ao=600
Bo=0
Au=800
Bu=0
v5=1400e6
deltaJ=Jo-Ju
inten=[]
wave=[]
rrrinten=[]
Fomax=np.int64(Jo+I)
Fomin=np.int64(Jo-I)
Fumax=np.int64(Ju+I)
Fumin=np.int64(Ju-I)
if deltaJ == 0:
    for i in range(Fomin,Fomax+1):
        Fo=i
        for k in range(Fumin,Fumax+1):
            Fu=k
            if np.absolute(Fo-Fu)<2:
#                print(Fo, Fu)
                deltaF=Fo- Fu
                if deltaF ==0:
                    a=I
                    b=Fo
                    c=Jo
                    s=a+b+c
                    X=(a*(a+1))-(b*(b+1))-(c*(c+1))
                    p=4*(X*X)/(2*b)*(2*b+1)*(2*b+2)*(2*c)*(2*c+1)*(2*c+2)
                    t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                    inten.append(p*t)
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1) 
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v)
#                        print(Fo,Fu,inten)
                elif deltaF ==1:
                    a=I
                    b=Fo
                    c=Jo
                    s=a+b+c
                    p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                    t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                    inten.append(p*t)
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v)
#                        print(Fo,Fu,inten)
                elif deltaF ==-1:
                    a=I
                    b=Fo
                    c=Jo
                    s=a+b+c
                    p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                    t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                    inten.append(p*t)
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v) 
#                        print(Fo,Fu,inten)
                else:
                    print("Invalid delta F value") 
                    print("value of delta J=",deltaJ)

elif deltaJ == 1:
    for i in range(Fomin,Fomax+1):
        Fo=i
        for k in range(Fumin,Fumax+1):
            Fu=k
            if np.absolute(Fo-Fu)<2:
#                print(Fo, Fu)
                deltaF=Fo- Fu
                if deltaF ==0:
                    a=I
                    b=Fo
                    c=Jo
                    s=a+b+c
                    p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                    t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                    inten.append(p*t)
                    vc=0 
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)    
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v)
#                        print(Fo,Fu,inten)
                elif deltaF ==1:
                    c=Jo
                    b=Fo
                    a=I
                    s=a+b+c
                    p=s*(s+1)*(s-2*a-1)*(s-2*a)/(2*b-1)*(2*b)*(2*b+1)*(2*c-1)*(2*c)*(2*c+1)
                    t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                    inten.append(p*t)
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1) 
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v)
#                        print(Fo,Fu,inten)
                elif deltaF ==-1:
                    c=Jo
                    b=Fo
                    a=I
                    s=a+b+c
                    p=(s-2*b)*(s-2*b-1)*(s-2*c+1)*(s-2*c+2)/(2*b+1)*(2*b+2)*(2*b+3)*(2*c-1)*(2*c)*(2*c+1)
                    t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                    inten.append(p*t)
                    vc=0 
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)      
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v)
#                        print(Fo,Fu,inten)
                else:
                    print("Invalid delta F value") 
                    print("value of delta J=",deltaJ) 

elif deltaJ == -1:
    for i in range(Fomin,Fomax+1):
        Fo=i
        for k in range(Fumin,Fumax+1):
            Fu=k
            if np.absolute(Fo-Fu)<2:
#                print(Fo, Fu)
                deltaF=Fo- Fu
                if deltaF ==0:
                    a=I
                    b=Fo
                    c=Jo+1
                    s=a+b+c
                    p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                    t=((2*Fo+1)*(2*Fu+1))/(2*I+1)
                    inten.append(p*t)
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v)
#                        print(Fo,Fu,inten)
                elif deltaF ==1:
                    c=Jo+1
                    b=Fo-1
                    a=I
                    s=a+b+c
                    p=(s-2*b)*(s-2*b-1)*(s-2*c+1)*(s-2*c+2)/(2*b+1)*(2*b+2)*(2*b+3)*(2*c-1)*(2*c)*(2*c+1)    
                    t=((2*Fo+1)*(2*Fu+1))/(2*I+1)
                    inten.append(p*t)
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1) 
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v)
#                        print(Fo,Fu,inten)
                elif deltaF ==-1:
                    c=Jo+1
                    b=Fo+1
                    a=I
                    s=a+b+c
                    p=s*(s+1)*(s-2*a-1)*(s-2*a)/(2*b-1)*(2*b)*(2*b+1)*(2*c-1)*(2*c)*(2*c+1)
                    t=((2*Fo+1)*(2*Fu+1))/(2*I+1)
                    inten.append(p*t)
                    Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                    alphao=Co/2
                    betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                    Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                    alphau=Cu/2
                    vc=0
                    betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                    v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                    wave.append(v) 
#                        print(Fo,Fu,inten)
                else:
                    print("Invalid delta F value")      
                    print("value of delta J=",deltaJ)             
else:
    print("Invalid delta J value") 


s1=np.size(wave)
print("no of waves=",s1)
if s1==15:
    for i in range(0,15): 
        maxinten=max(inten)
        rrrinten.append(100*inten[i]/maxinten)
elif s1==16:
    for i in range(0,16):
        maxinten=max(inten) 
        rrrinten.append(100*inten[i]/maxinten)
    else:
            print("Invalid Value")

#    v5 = 1400e6 #Doppler width MHz
wave1=np.array(wave)
rrrinten1=np.array(rrrinten)
xvalues = np.arange(wave1.min()-100,wave1.max()+100)

z = np.exp(((-3e12)*np.log(2)*((xvalues-wave1.reshape((-1,1)))/(v5))**2))*((5*np.log(2)/(np.pi))**0.5)
s = z*rrrinten1.reshape((-1,1))
#    plt.plot(xrange,s.sum(axis=0),'--r')

ax = plt.subplot(111)
plt.subplots_adjust(bottom=0.25)
axcolor = 'lightgoldenrodyellow'
#plt.figure()
plt.stem(wave,rrrinten)
plt.plot(xvalues,s.sum(axis=0),'--r')

axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.2, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp  = plt.axes([0.2, 0.15, 0.65, 0.03], facecolor=axcolor)

sfreq = Slider(axfreq, 'Au',0,1000,valinit=Ao)
samp = Slider(axamp, 'Ao' ,0,1000,valinit=Au)
sfreq.on_changed(Ao)
samp.on_changed(Au)
#plt.draw()

plt.show()

先提一个普遍的建议:这段代码真的很难读。考虑把它拆成更小的部分,也许你可以重复使用一些部分。

所以对于你的问题:slider.on_change(fun) 需要一个函数,当滑块的值发生变化时应该调用该函数。现在你只是传递了一个 int/float.

为了执行此更新,您需要一个函数,它为您提供要为给定的 (Ao, Au, Bo, Bu) 组合绘制的值。为了快速修复,我只是将所有内容放在一个函数中 fun,但你真的应该考虑以更有意义的方式构建它:

def fun(Ao, Au, Bo, Bu):
    inten=[]
    wave=[]
    rrrinten=[]

    if deltaJ == 0:
        ...
    elif deltaJ == 1:
        ...
    elif deltaJ == -1:
        ...
    else:
        ...

    s1 = np.size(wave)

    print("no of waves=", s1)
    ...
    s = z*rrrinten1.reshape((-1, 1))

    s = s.sum(axis=0)
    return (wave, rrrinten), (xvalues, s)

有了这个函数我们可以先初始化绘图:

Ao_init = 600
Bo_init = 0
Au_init = 800
Bu_init = 0

(x1, y1), (x2, y2) = fun(Ao=Ao_init, Au=Au_init, Bo=Bo_init, Bu=Bu_init)

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.30)

h_stem = plt.stem(x1, y1, use_line_collection=True)
h_plot = plt.plot(x2, y2, '--r')[0]

和滑块,然后编写更新函数:

axcolor = 'lightgoldenrodyellow'
ax_bu = plt.axes([0.2, 0.25, 0.65, 0.03], facecolor=axcolor)
ax_bo = plt.axes([0.2, 0.2, 0.65, 0.03], facecolor=axcolor)
ax_ao = plt.axes([0.2, 0.15, 0.65, 0.03], facecolor=axcolor)
ax_au = plt.axes([0.2, 0.1, 0.65, 0.03], facecolor=axcolor)

s_bu = Slider(ax_bu, 'Bu', -100, 100, valinit=Bo_init, valstep=1, valfmt='%i')
s_bo = Slider(ax_bo, 'Bo', -100, 100, valinit=Bu_init, valstep=1, valfmt='%i')
s_ao = Slider(ax_ao, 'Ao', 0, 1000, valinit=Au_init, valstep=1, valfmt='%i')
s_au = Slider(ax_au, 'Au', 0, 1000, valinit=Ao_init, valstep=1, valfmt='%i')

def on_changed(event):
    # Get new values
    (x1, y1), (x2, y2) = fun(Ao=s_au.val, Au=s_ao.val, Bo=s_bo.val, Bu=s_bu.val)
    ax.set_xlim((min(min(x1), min(x2)),
                 max(max(x1), max(x2))))
    # update plot
    h_plot.set_ydata(y2)
    h_plot.set_xdata(x2)

    # update stem
    h_stem[0].set_ydata(y1)
    h_stem[0].set_xdata(x1)

    h_stem[1].set_paths([np.array([[x, 0], [x, y]]) for (x, y) in zip(x1, y1)])

    h_stem[2].set_xdata([np.min(x1), np.max(x1)])

    fig.canvas.draw_idle()

s_au.on_changed(on_changed)
s_ao.on_changed(on_changed)
s_bo.on_changed(on_changed)
s_bu.on_changed(on_changed)

完整代码供参考:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

Jo = 6.5
Ju = 6.5
I = 2.5
deltaJ = Jo-Ju
v5 =1400e6

Fomax = np.int64(Jo+I)
Fomin = np.int64(Jo-I)
Fumax = np.int64(Ju+I)
Fumin = np.int64(Ju-I)

def fun(Ao, Au, Bo, Bu):
    inten=[]
    wave=[]
    rrrinten=[]

    if deltaJ == 0:
        for i in range(Fomin, Fomax+1):
            Fo = i
            for k in range(Fumin,Fumax+1):
                Fu = k
                if np.absolute(Fo-Fu)<2:
                    #                print(Fo, Fu)
                    deltaF=Fo- Fu
                    if deltaF ==0:
                        a=I
                        b=Fo
                        c=Jo
                        s=a+b+c
                        X=(a*(a+1))-(b*(b+1))-(c*(c+1))
                        p=4*(X*X)/(2*b)*(2*b+1)*(2*b+2)*(2*c)*(2*c+1)*(2*c+2)
                        t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                        inten.append(p*t)
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    elif deltaF ==1:
                        a=I
                        b=Fo
                        c=Jo
                        s=a+b+c
                        p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                        t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                        inten.append(p*t)
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    elif deltaF ==-1:
                        a=I
                        b=Fo
                        c=Jo
                        s=a+b+c
                        p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                        t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                        inten.append(p*t)
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    else:
                        print("Invalid delta F value")
                        print("value of delta J=", deltaJ)

    elif deltaJ == 1:
        for i in range(Fomin,Fomax+1):
            Fo=i
            for k in range(Fumin,Fumax+1):
                Fu=k
                if np.absolute(Fo-Fu)<2:
                    #                print(Fo, Fu)
                    deltaF=Fo- Fu
                    if deltaF ==0:
                        a=I
                        b=Fo
                        c=Jo
                        s=a+b+c
                        p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                        t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                        inten.append(p*t)
                        vc=0
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    elif deltaF ==1:
                        c=Jo
                        b=Fo
                        a=I
                        s=a+b+c
                        p=s*(s+1)*(s-2*a-1)*(s-2*a)/(2*b-1)*(2*b)*(2*b+1)*(2*c-1)*(2*c)*(2*c+1)
                        t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                        inten.append(p*t)
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    elif deltaF ==-1:
                        c=Jo
                        b=Fo
                        a=I
                        s=a+b+c
                        p=(s-2*b)*(s-2*b-1)*(s-2*c+1)*(s-2*c+2)/(2*b+1)*(2*b+2)*(2*b+3)*(2*c-1)*(2*c)*(2*c+1)
                        t=(2*Fo+1)*(2*Fu+1)/(2*I+1)
                        inten.append(p*t)
                        vc=0
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    else:
                        print("Invalid delta F value")
                        print("value of delta J=",deltaJ)

    elif deltaJ == -1:
        for i in range(Fomin,Fomax+1):
            Fo=i
            for k in range(Fumin,Fumax+1):
                Fu=k
                if np.absolute(Fo-Fu)<2:
                    #                print(Fo, Fu)
                    deltaF=Fo- Fu
                    if deltaF ==0:
                        a=I
                        b=Fo
                        c=Jo+1
                        s=a+b+c
                        p=2*(s+1)*(s-2*a)*(s-2*b)*(s-2*c+1)/(2*b)*(2*b+1)*(2*b+2)*(2*c-1)*(2*c)*(2*c+1)
                        t=((2*Fo+1)*(2*Fu+1))/(2*I+1)
                        inten.append(p*t)
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    elif deltaF ==1:
                        c=Jo+1
                        b=Fo-1
                        a=I
                        s=a+b+c
                        p=(s-2*b)*(s-2*b-1)*(s-2*c+1)*(s-2*c+2)/(2*b+1)*(2*b+2)*(2*b+3)*(2*c-1)*(2*c)*(2*c+1)
                        t=((2*Fo+1)*(2*Fu+1))/(2*I+1)
                        inten.append(p*t)
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    elif deltaF ==-1:
                        c=Jo+1
                        b=Fo+1
                        a=I
                        s=a+b+c
                        p=s*(s+1)*(s-2*a-1)*(s-2*a)/(2*b-1)*(2*b)*(2*b+1)*(2*c-1)*(2*c)*(2*c+1)
                        t=((2*Fo+1)*(2*Fu+1))/(2*I+1)
                        inten.append(p*t)
                        Co=(Fo*(Fo+1)-I*(I+1)-Jo*(Jo+1))
                        alphao=Co/2
                        betao=(0.75)*Co*(Co+1)-I*(I+1)*Jo*(Jo+1)/(2*I*Jo)*(2*I-1)*(2*Jo-1)
                        Cu=(Fu*(Fu+1)-I*(I+1)-Ju*(Ju+1))
                        alphau=Cu/2
                        vc=0
                        betau=(0.75)*Cu*(Cu+1)-I*(I+1)*Ju*(Ju+1)/(2*I*Ju)*(2*I-1)*(2*Ju-1)
                        v=vc+alphao*Ao+betao*Bo-alphau*Au+betau*Bu
                        wave.append(v)
                    #                        print(Fo,Fu,inten)
                    else:
                        print("Invalid delta F value")
                        print("value of delta J=",deltaJ)

    else:
        print("Invalid delta J value")

    s1 = np.size(wave)

    print("no of waves=", s1)
    if s1 == 15:
        for i in range(0,15):
            maxinten = max(inten)
            rrrinten.append(100*inten[i]/maxinten)
    elif s1 == 16:
        for i in range(0, 16):
            maxinten = max(inten)
            rrrinten.append(100*inten[i]/maxinten)
    else:
        print("Invalid Value")

    wave1 = np.array(wave)
    rrrinten1 = np.array(rrrinten)
    xvalues = np.arange(wave1.min() - 100, wave1.max() +100)

    z = np.exp(((-3e12)*np.log(2)*((xvalues-wave1.reshape((-1,1)))/(v5))**2))*((5*np.log(2)/(np.pi))**0.5)
    s = z*rrrinten1.reshape((-1, 1))

    s = s.sum(axis=0)
    return (wave, rrrinten), (xvalues, s)


Ao_init = 600
Bo_init = 0
Au_init = 800
Bu_init = 0

(x1, y1), (x2, y2) = fun(Ao=Ao_init, Au=Au_init, Bo=Bo_init, Bu=Bu_init)

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.30)

h_stem = plt.stem(x1, y1, use_line_collection=True)
h_plot = plt.plot(x2, y2, '--r')[0]

axcolor = 'lightgoldenrodyellow'
ax_bu = plt.axes([0.2, 0.25, 0.65, 0.03], facecolor=axcolor)
ax_bo = plt.axes([0.2, 0.2, 0.65, 0.03], facecolor=axcolor)
ax_ao = plt.axes([0.2, 0.15, 0.65, 0.03], facecolor=axcolor)
ax_au = plt.axes([0.2, 0.1, 0.65, 0.03], facecolor=axcolor)

s_bu = Slider(ax_bu, 'Bu', -100, 100, valinit=Bo_init, valstep=1, valfmt='%i')
s_bo = Slider(ax_bo, 'Bo', -100, 100, valinit=Bu_init, valstep=1, valfmt='%i')
s_ao = Slider(ax_ao, 'Ao', 0, 1000, valinit=Au_init, valstep=1, valfmt='%i')
s_au = Slider(ax_au, 'Au', 0, 1000, valinit=Ao_init, valstep=1, valfmt='%i')

def on_changed(event):
    # Get new values
    (x1, y1), (x2, y2) = fun(Ao=s_au.val, Au=s_ao.val, Bo=s_bo.val, Bu=s_bu.val)
    ax.set_xlim((min(min(x1), min(x2)),
                 max(max(x1), max(x2))))
    # update plot
    h_plot.set_ydata(y2)
    h_plot.set_xdata(x2)

    # update stem
    h_stem[0].set_ydata(y1)
    h_stem[0].set_xdata(x1)

    h_stem[1].set_paths([np.array([[x, 0], [x, y]]) for (x, y) in zip(x1, y1)])

    h_stem[2].set_xdata([np.min(x1), np.max(x1)])
    fig.canvas.draw_idle()

s_au.on_changed(on_changed)
s_ao.on_changed(on_changed)
s_bo.on_changed(on_changed)
s_bu.on_changed(on_changed)

plt.show()

有关 stem plot 的更新,另请参阅