Week 6 notebook

In [13]:
import matplotlib.pyplot as plt
import numpy as np

List unpacking

In [2]:
mylist = [1, 2, 3]
a = mylist[0]
b = mylist[1]
c = mylist[2]
In [3]:
print(a)
1
In [4]:
print(b)
2
In [5]:
mylist = [1, 2, 3]
x, y, z = mylist
In [6]:
print(x)
1
In [7]:
print(y)
2
In [8]:
mylist2 = [5]
In [9]:
x = mylist
In [10]:
print(x)
[1, 2, 3]
In [14]:
x,  = mylist2
print(x)
5

Modifying plots

In [14]:
%matplotlib notebook
In [17]:
plt.plot([1, 2, 3], [1, 2, 3])
plt.show()
In [18]:
%matplotlib inline
In [19]:
plt.plot([1, 2, 3], [1, 2, 3])
plt.show()
In [6]:
%matplotlib notebook
In [12]:
fig = plt.figure()

plt.axis('equal')

t = np.linspace(- np.pi, np.pi, 200)
x = np.sin(t)
y = np.cos(t)
p, = plt.plot(x, y)
plt.show()
In [13]:
for r in np.linspace(1, 0.1, 100):
    p.set_xdata(r*x)
    p.set_ydata(r*y)
    fig.canvas.draw()
In [24]:
from matplotlib.widgets import Slider

a_min = 0 
a_max = 10
a_init = 1


# setup of subplots

fig = plt.figure(figsize=(8,3))

sin_ax = plt.axes([0.1, 0.2, 0.8, 0.65])
slider_ax = plt.axes([0.1, 0.05, 0.8, 0.05])

#plot the initial view of the function with the initial value of the parameter a

plt.axes(sin_ax)
x = np.linspace(0, np.pi*2, 500)
y = np.sin(a_init*x)
sin_plot, = plt.plot(x, y, 'r')
plt.xlim(0, 2*np.pi)
plt.ylim(-1.1, 1.1)


a_slider = Slider(
                  slider_ax,  #the axes object containing the slider
                  'a', # name of the slider parameter
                  a_min,  # minimal value of the parameter
                  a_max,  # max value of the parameter
                  valinit = a_init # the initial value of the parameter
                  )


#function used to update the plot depending on the value of the slider
def update(a):
    sin_plot.set_ydata(np.sin(a*x))
    fig.canvas.draw_idle()
    
# execute the above function if the value of the slider changes
a_slider.on_changed(update)
Out[24]:
0

A variant:

In [15]:
from matplotlib.widgets import Slider

a_min = 0 
a_max = 10
a_init = 1


# setup of subplots

fig = plt.figure(figsize=(8,3))

sin_ax = plt.axes([0.1, 0.2, 0.8, 0.65])
slider_ax = plt.axes([0.1, 0.05, 0.8, 0.05])

#plot the initial view of the function with the initial value of the parameter a

plt.axes(sin_ax)
x = np.linspace(0, np.pi*2, 500)
y = np.sin(a_init*x)
sin_plot, = plt.plot(x, y, 'r')
plt.xlim(0, 2*np.pi)
plt.ylim(-1.1, 1.1)


a_slider = Slider(
                  slider_ax,  #the axes object containing the slider
                  'a', # name of the slider parameter
                  a_min,  # minimal value of the parameter
                  a_max,  # max value of the parameter
                  valinit = a_init # the initial value of the parameter
                  )


#function used to update the plot depending on the value of the slider
def update(a):
    sin_plot.set_ydata(np.sin(a_slider.val*x))
    fig.canvas.draw_idle()
    
# execute the above function if the value of the slider changes
a_slider.on_changed(update)
Out[15]:
0
In [16]:
def mayfly(y, b, n):
    ylist = [y]
    for i in range(n):
        y = b*(1-y)*y
        ylist.append(y)
    return ylist
In [17]:
import matplotlib.pyplot as plt
In [24]:
%matplotlib inline

plt.figure(figsize=(10,6))
b = 3.2
y = [0.1, 0.5, 0.9]
n=30

plt.subplots_adjust(hspace=0.4)

for i in range(3):
    plt.subplot(3, 1, i+1)
    ylist = mayfly(b=b, y=y[i], n=n)
    plt.plot(ylist, 'ro-')
    plt.title('b={}, y={}, n={}'.format(b, y[i], n))
    plt.ylim(0, 1)

plt.show()