import matplotlib.pyplot as plt
import numpy as np
mylist = [1, 2, 3]
a = mylist[0]
b = mylist[1]
c = mylist[2]
print(a)
print(b)
mylist = [1, 2, 3]
x, y, z = mylist
print(x)
print(y)
mylist2 = [5]
x = mylist
print(x)
x, = mylist2
print(x)
%matplotlib notebook
plt.plot([1, 2, 3], [1, 2, 3])
plt.show()
%matplotlib inline
plt.plot([1, 2, 3], [1, 2, 3])
plt.show()
%matplotlib notebook
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()
for r in np.linspace(1, 0.1, 100):
p.set_xdata(r*x)
p.set_ydata(r*y)
fig.canvas.draw()
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)
A variant:
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)
def mayfly(y, b, n):
ylist = [y]
for i in range(n):
y = b*(1-y)*y
ylist.append(y)
return ylist
import matplotlib.pyplot as plt
%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()