gpt4 book ai didi

python - python 中的 slider

转载 作者:行者123 更新时间:2023-12-05 04:47:01 28 4
gpt4 key购买 nike

我正在尝试为第二张图制作一个 slider 。我成功制作了 slider ,但我在使用应该更新 y 数据值的函数时遇到了问题。有人可以帮我看看我的错误吗?谢谢

代码是:

import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from scipy.constants import *

%matplotlib tk


#Defining constants and variables of interests
hbar_sq = hbar**2
omega_0 = 5.63*10**14 #Transition of 532 nm in the visible, expressed in hertz (delta E/hbar)
omega = 5.65*10**14 #Incoming laser of 530 nm in the visible, expressed in hertz
diff = omega_0 - omega #The difference in frequency between the incoming field and the state-to-state frequency
diff_p=abs(diff)
V = np.sqrt((diff**2 * hbar_sq)) #Matrix element value
V_sq=V**2
t=np.linspace(0,(8*pi/diff_p),100)
P=(V_sq/(hbar_sq*diff**2))*np.sin(diff*t/2)*np.sin(diff*t/2)

#Plot parameters
fig=plt.figure()
ax=fig.subplots()
f=ax.plot(t,P,'b')
plt.ylabel('P (t)')
plt.xlabel('time')

#Second part, plotting P(omega)
omega1=np.linspace(4.99*10**14,6.66*10**14,100)
diff1=omega_0-omega1
P1=(V_sq/(hbar_sq*diff1**2))*np.sin(diff1*t/2)*np.sin(diff1*t/2)

#Plot parameters
fig1=plt.figure()
plt.subplots_adjust(bottom=0.25) #Generating some space under the graph to add the slider button
ax1=fig1.subplots()
f1=ax1.plot(omega1,P1)

#Adding slider functionality to plot
# xposition, yposition, width and height
ax1.slide = plt.axes([0.15,0.1,0.65,0.05])
#Properties of the slider
df = Slider(ax1.slide,'driving frequency',valmin=4.99*10**14, valmax=6.66*10**14, valinit=6.66*10**14, valstep=.5*10**14)

#Making a function to update the plot
def update(val):
current_v = df.val
omega1 = np.linspace(4.99*10**14,current_v,100)
P1=(V_sq/(hbar_sq*diff1**2))*np.sin(diff1*t/2)*np.sin(diff1*t/2)
f1.set_ydata(P1)
fig1.canvas.draw()
df.on_changed(update)
plt.show()```

最佳答案

我像这样编辑了你的 update() 函数:

def update(val):
current_v = df.val
omega1 = np.linspace(4.99*10**14,current_v,100)
P1=(V_sq/(hbar_sq*diff1**2))*np.sin(diff1*t/2)*np.sin(diff1*t/2)
ax1.cla()
ax1.plot(omega1, P1)
ax1.set_xlim(4.5e14, 6.5e14)

首先,我使用 ax1.cla() 清除之前的曲线,然后使用 ax1.plot(omega1, P1) 绘制新曲线。< br/>或者,您可以使用 ax1.set_xlim(4.5e14, 6.5e14) 修复 x 轴限制,以保持轴固定并查看曲线变化。此外,我建议在显示图形之前调用函数 update(df.val),以便在显示图形后立即修复轴,甚至在用户更改 slider 值之前。

完整代码

import numpy as np
import math
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from scipy.constants import *

%matplotlib tk


#Defining constants and variables of interests
hbar_sq = hbar**2
omega_0 = 5.63*10**14 #Transition of 532 nm in the visible, expressed in hertz (delta E/hbar)
omega = 5.65*10**14 #Incoming laser of 530 nm in the visible, expressed in hertz
diff = omega_0 - omega #The difference in frequency between the incoming field and the state-to-state frequency
diff_p=abs(diff)
V = np.sqrt((diff**2 * hbar_sq)) #Matrix element value
V_sq=V**2
t=np.linspace(0,(8*pi/diff_p),100)
P=(V_sq/(hbar_sq*diff**2))*np.sin(diff*t/2)*np.sin(diff*t/2)

#Plot parameters
fig=plt.figure()
ax=fig.subplots()
f=ax.plot(t,P,'b')
plt.ylabel('P (t)')
plt.xlabel('time')

#Second part, plotting P(omega)
omega1=np.linspace(4.99*10**14,6.66*10**14,100)
diff1=omega_0-omega1
P1=(V_sq/(hbar_sq*diff1**2))*np.sin(diff1*t/2)*np.sin(diff1*t/2)

#Plot parameters
fig1=plt.figure()
plt.subplots_adjust(bottom=0.25) #Generating some space under the graph to add the slider button
ax1=fig1.subplots()
f1=ax1.plot(omega1,P1)

#Adding slider functionality to plot
# xposition, yposition, width and height
ax1.slide = plt.axes([0.15,0.1,0.65,0.05])
#Properties of the slider
df = Slider(ax1.slide,'driving frequency',valmin=4.99*10**14, valmax=6.66*10**14, valinit=6.66*10**14, valstep=.5*10**13)

#Making a function to update the plot
def update(val):
current_v = df.val
omega1 = np.linspace(4.99*10**14,current_v,100)
P1=(V_sq/(hbar_sq*diff1**2))*np.sin(diff1*t/2)*np.sin(diff1*t/2)
ax1.cla()
ax1.plot(omega1, P1)
ax1.set_xlim(4.5e14, 6.5e14)
df.on_changed(update)
update(df.val)
plt.show()

关于python - python 中的 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68640841/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com