Во-первых, общий совет: этот фрагмент кода действительно трудно читать. Подумайте о том, чтобы разбить его на более мелкие части, возможно, вы сможете повторно использовать некоторые части.
Итак, для вашего вопроса: 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(Fom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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
см. также этот ответ .