1

the following code is not updating the graph as i change the slider. The slider consists of two variables Ao and Au which i wanted to change. Initially Ao is set to be 600 and Au is set to be 800. as the slider moves i expected the graph to change which is not working. what mistake am I doing? and if possible kindly tell me the way to make four sliders that works together as i change them which is Ao,Au, Bo and 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()
James Z
  • 12,209
  • 10
  • 24
  • 44
Zewo
  • 153
  • 1
  • 12

1 Answers1

2

First a general advise: this piece of code is really hard to read. Think about breaking it into smaller piece, maybe you can reuse some parts.

So for your question: slider.on_change(fun) expects a function which should be called when the value of the slider changes. right now you are just passing an int/float.

In order to perform this update you need a function which gives you the values to plot for a given combination of (Ao, Au, Bo, Bu). For a quick fix I just put everything together in a function fun, but you should really think about structuring this in a more meaningful way:

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)

With this function we can first initialize the plot:

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]

and the sliders and than write the update function:

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)

Here the full code for reference:

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()

For the updating of a stem plot see also this answer.

scleronomic
  • 4,392
  • 1
  • 13
  • 43
  • its working. but still the graph is not updating. :( – Zewo Mar 06 '20 at 20:40
  • yes i have activated the interactive plotting using %matplotlib qt5 – Zewo Mar 06 '20 at 20:53
  • Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 216, in process func(*args, **kwargs) File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\widgets.py", line 442, in _update self.set_val(val) File "C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\widgets.py", line 467, in set_val func(val) File "C:/Users/MUHAMMAD ALI QURESHI/Desktop/hyper gui.py", line 295, in on_changed fig.canvas.draw_idle()() NameError: name 'fig' is not defined – Zewo Mar 06 '20 at 20:57
  • another help ... kindly set x and y limits in the code . i am trying to put x and y limits but its not working – Zewo Mar 06 '20 at 21:38
  • 1
    Hi, I am trying to understand this code, but I get an error "File "", line 176 betao ax.set_xlim((min(min(x1), min(x2)), ^ SyntaxError: invalid syntax" This is within the if deltaJ ==-1 conditional, and inside elif deltaF == 1 part. Is it possible for you to check? Thanks! – xiaxio Mar 07 '20 at 15:59
  • @xiaxio Hi, yeah I made a mistake while copy&pasting, it should work now. Thank you for pointing this out. Does it work now for you? – scleronomic Mar 07 '20 at 18:17
  • func(val) File "", line 383, in on_changed h_stem[1].set_paths([np.array([[x, 0], [x, y]]) for (x, y) in zip(x1, y1)]) AttributeError: 'list' object has no attribute 'set_paths' what does this means? @scleronomic – Zewo Mar 30 '20 at 15:35
  • `'list' object has no attribute 'set_paths'` means that `h_stem[1]` is a `list` while it should be a `LineCollection`. How did you produce this error? – scleronomic Mar 30 '20 at 21:50
  • I DONT KNOW I AM JUST RUNNING YOUR PROGRAM .. SEE FULL THING REFERENCE OF YOURS. – Zewo Mar 31 '20 at 16:19
  • But if i remember correctly the program did run for you some time ago. Is this true or did my code never work for you? When I run my code it works fine. Did you make some changes to my code or have you really just copied the code? – scleronomic Apr 01 '20 at 12:37
  • YES U R ABSOULETY RIGHT it worked . but i have some python issues , i reinstall it and after that this error is making my life mesearable. – Zewo Apr 01 '20 at 15:17
  • i upadated the matploib from https://anaconda.org/conda-forge/matplotlib still having this problem – Zewo Apr 01 '20 at 15:18
  • no changes at all., copy pasting only. still encountered the same problem . – Zewo Apr 01 '20 at 15:21
  • Can you run the animation of the stem plot in [this answer](https://stackoverflow.com/a/60570742/7570817). Maybe try to run it without the last line which saves the animation to a gif – scleronomic Apr 01 '20 at 16:26
  • I am running this under `python 3.8`, `matplotlib 3.1.1`. The matplotlib backends I tried are `MacOSX` and `TKAgg`. – scleronomic Apr 01 '20 at 16:33
  • now its working ..did reinstall python again and bang!! thanks @scleronomic – Zewo Apr 02 '20 at 12:06
  • how do u do it.. plz guide refer some books or text .. amazing u are bro – Zewo Apr 02 '20 at 12:07
  • and also tell me is it possible to make .exe file for this program ? – Zewo Apr 02 '20 at 15:49
  • 1
    Glad I could help you. Making a standalone file out of a python script is a different question and is already answered [here](https://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency). – scleronomic Apr 02 '20 at 17:34