python 中的滑块
Slider in python
我正在尝试为第二个图表制作一个滑块。我成功制作了滑块,但我在使用应该更新 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)
绘制了新曲线。
或者,您可以使用 ax1.set_xlim(4.5e14, 6.5e14)
固定 x 轴限制,以保持轴固定并查看曲线变化。此外,我建议在显示图形之前调用函数 update(df.val)
,以便在图形显示时立即固定轴,甚至在用户更改滑块值之前。
完整代码
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()
我正在尝试为第二个图表制作一个滑块。我成功制作了滑块,但我在使用应该更新 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)
绘制了新曲线。
或者,您可以使用 ax1.set_xlim(4.5e14, 6.5e14)
固定 x 轴限制,以保持轴固定并查看曲线变化。此外,我建议在显示图形之前调用函数 update(df.val)
,以便在图形显示时立即固定轴,甚至在用户更改滑块值之前。
完整代码
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()