滑块不起作用并更新图表中的值

Posted

技术标签:

【中文标题】滑块不起作用并更新图表中的值【英文标题】:Slider not working and updating values in graph 【发布时间】:2020-06-19 10:49:23 【问题描述】:

当我更改滑块时,以下代码不会更新图表。滑块由我想要更改的两个变量 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()

【问题讨论】:

【参考方案1】:

首先一般建议:这段代码真的很难阅读。想想把它分成小块,也许你可以重复使用一些部分。

所以对于您的问题: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 的更新,另请参阅this answer。

【讨论】:

它的工作。但图表仍然没有更新。 :( 是的,我已经使用 %matplotlib qt5 激活了交互式绘图 Traceback(最近一次调用最后):文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\cbook_init_.py”,第 216 行,在处理 func(*args, **kwargs) 文件“C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\widgets.py”,第 442 行,_update self.set_val(val) 文件“C:\ProgramData\ Anaconda3\lib\site-packages\matplotlib\widgets.py”,第 467 行,在 set_val func(val) 文件中“C:/Users/MUHAMMAD ALI QURESHI/Desktop/hyper gui.py”,第 295 行,在 on_changed 图中。 canvas.draw_idle()() NameError: name 'fig' is not defined 嗨,我正在尝试理解这段代码,但我收到错误“文件””,第 176 行 betao ax.set_xlim((min(min(x1 ), min(x2)), ^ SyntaxError: invalid syntax" 这在 if deltaJ ==-1 条件和 elif deltaF == 1 部分内。您可以检查一下吗?谢谢! 很高兴能为您提供帮助。用 python 脚本制作独立文件是一个不同的问题,已经回答了here。

以上是关于滑块不起作用并更新图表中的值的主要内容,如果未能解决你的问题,请参考以下文章

更新反应材料 ui 滑块的状态/值不起作用

更改曲目 NAudio 后滑块不起作用

引导滑块不起作用

通过ajax加载时jquery滑块不起作用

在百里香弹簧靴中,动态旋转木马滑块不起作用

无限滚动加载更多信息后,滑动滑块不起作用