Kedy
Kedy

Reputation: 33

Slider in python

I am trying to make a slider for the second graph. I succeeded in making the slider, but I am having trouble with the function that is supposed to update the values of the y data. Can someone please help me see my mistake? Thank you

Code is:

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

Upvotes: 3

Views: 771

Answers (1)

Zephyr
Zephyr

Reputation: 12496

I edited your update() function like this:

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)

First of all, I clear the previous plot with ax1.cla(), then I plot the new curve with ax1.plot(omega1, P1).
Optionally, you can fix the x-axis limits with ax1.set_xlim(4.5e14, 6.5e14), in order to keep fixed the axis and see the curve changing. Moreover, I suggest to call the function update(df.val) before showing the figure, in order to fix the axis as soon as the figure is shown, even before the user changes the slider value.

Complete Code

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

Upvotes: 2

Related Questions