I created a widget using interact()
in the Jupyter notebook which plots the data (see code below). However, the widget updates very slowly (around 1 fps) when I move the slider.
Evaluation of the data inside the functions neg(T)
and pos(T)
takes around 1e-4 seconds and plotting of the data inside the function conc(T)
- around 1e-1 seconds. Then why does my widget have such a slow response? How could I improve it?
from scipy import constants
from numpy import *
import matplotlib.pyplot as plt
from ipywidgets import interact
import time
%matplotlib inline
m_e = m_h = 1.5 * 10**-49
hbar = constants.hbar
k = constants.k / constants.e
E_g = 1.1242
E_v = 0
E_c = E_v + E_g
E_a = E_v + 0.045
E_d = E_c - 0.045
n_a = 10**14
n_d = 10**12
g_a = 4
g_d = 2
E_f = linspace(E_v, E_c, 100)
def neg(T):
start = time.time()
N_c = 2 * 10**-6 * (m_e*k*T/(2*pi*hbar**2))**1.5
n = N_c * exp((E_f-E_c)/(k*T))
N_a_minus = n_a/(1+g_a*exp((E_a-E_f)/(k*T)))
print(time.time()-start)
return n + N_a_minus
def pos(T):
start = time.time()
N_v = 2 * 10**-6 * (m_h*k*T/(2*pi*hbar**2))**1.5
p = N_v * exp((E_v-E_f)/(k*T))
N_d_plus = n_d/(1+g_d*exp((E_f-E_d)/(k*T)))
print(time.time()-start)
return p + N_d_plus
def conc(T):
start = time.time()
plt.plot(E_f, neg(T), E_f, pos(T))
plt.axis([E_v,E_g,10.0**6,10.0**21])
plt.yscale('log')
print(time.time()-start)
interact(conc, T=(50,800,50));