import numpy as np
import matplotlib.pyplot as plt
L1 = list(range(9))
L2 = list(range(10,19))
L1, L2
D = dict( L1=L1, L2=L2)
D
D['L1']
plt.rcParams['figure.figsize'] = (10.0, 6.0) # set figures display bigger
x = np.linspace(- 5*np.pi,5*np.pi,200)
plt.plot(x,np.sin(x)/x);
plt.plot(x,np.sin(x)/x,x,np.sin(2*x)/x);
plt.plot(np.c_[x,x],np.c_[np.sin(x)/x,np.sin(2*x)/x]);
%config InlineBackend.figure_format = 'retina'
# red, dot-dash, triangles and blue, dot-dash, bullet
plt.plot(x,np.sin(x)/x, 'r-^',x,np.sin(2*x)/x, 'b-o');
x = np.linspace(-1,1,50)
y = np.sqrt(1-x**2)
plt.scatter(x,y);
theta = np.linspace(0,6*np.pi,50) # 50 steps from 0 to 6 PI
size = 30*np.ones(50) # array with 50 values set to 30
z = np.random.rand(50) # array with 50 random values in [0,1]
x = theta*np.cos(theta)
y = theta*np.sin(theta)
plt.scatter(x,y,size,z)
plt.colorbar();
n = 2000
threshold = 0.25
rng = np.random.RandomState(1234)
X1 = rng.rand(n)
X2 = rng.rand(n)
U = rng.rand(n)
Y = np.zeros(n, dtype=np.uint8)
Y[(X1 <= 0.25) & (U <= threshold)] = 1
Y[(X1 > 0.25) & (X2 >= 0.75) & (U <= threshold)] = 1
Y[(X1 > 0.25) & (X2 < 0.75) & (U > threshold)] = 1
plt.axes().set_aspect("equal")
plt.scatter(X1, X2, c=Y, s=5)
fig = plt.figure() # create a figure
ax = fig.add_subplot(1, 1, 1) # add a single plot
ax.scatter(x,y,size,z,cmap='jet');
ax.set_aspect('equal', 'datalim')
plt.figure()
plt.plot(x)
plt.figure()
plt.plot(z,'ro');
plt.subplot(1,2,1) # 1 row 1, 2 columns, active plot number 1
plt.plot(x,'b-*')
plt.subplot(1,2,2) # 1 row 1, 2 columns, active plot number 2
plt.plot(z,'ro');
theta =np.linspace(0,4*np.pi,200)
plt.plot(np.sin(theta), label='sin')
plt.plot(np.cos(theta), label='cos')
plt.legend();
plt.plot(np.sin(theta))
plt.plot(np.cos(theta)**2)
plt.legend(['sin','$\cos^2$']);
plt.plot(theta,np.sin(theta))
plt.xlabel('radians from 0 to $4\pi$')
plt.ylabel('amplitude');
t = np.arange(0.01, 20.0, 0.01)
plt.subplot(121)
plt.semilogy(t, np.exp(-t/5.0))
plt.title('semilogy')
plt.grid(True)
plt.subplot(122,fc='y')
plt.semilogx(t, np.sin(2*np.pi*t))
plt.title('semilogx')
plt.grid(True)
theta = np.linspace(0,2*np.pi,100)
plt.plot(np.cos(theta),np.sin(theta))
plt.grid();
plt.savefig('circle.png');
%ls *.png
from numpy.random import randn
plt.hist(randn(1000));
plt.hist(randn(1000), 30, ec='w');
x = y = np.arange(-2.0*np.pi, 2.0*np.pi+0.01, 0.01)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)*np.cos(Y)
plt.contourf(X, Y, Z,cmap=plt.cm.hot);
img = plt.imread("https://hackage.haskell.org/package/JuicyPixels-extra-0.1.0/src/data-examples/lenna.png")
plt.imshow(img)
fig = plt.figure()
axis = fig.add_subplot(111, aspect='equal',
xlim=(-2, 2), ylim=(-2, 2))
state = -0.5 + np.random.random((50, 4))
state[:, :2] *= 3.9
bounds = [-1, 1, -1, 1]
particles = axis.plot(state[:,0], state[:,1], 'bo', ms=6)
rect = plt.Rectangle(bounds[::2],
bounds[1] - bounds[0],
bounds[3] - bounds[2],
ec='r', lw=2, fc='none')
axis.grid()
axis.add_patch(rect)
axis.text(-0.5,1.1,"BOX")
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
ax.set_xlim(( 0, 2))
ax.set_ylim((-2, 2))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return (line,)
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return (line,)
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20,
blit=True)
HTML(anim.to_jshtml())
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
xdata, ydata = [], []
line1, = plt.plot([], [], 'r-', animated=True)
def init():
ax1.set_xlim((0,1))
ax1.set_ylim((-1,1))
return line1,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(8*np.pi*frame))
line1.set_data(xdata, ydata)
return line1,
anim = FuncAnimation(fig, update, frames=np.linspace(0, 1.0, 100),
init_func=init, blit=True)
HTML(anim.to_jshtml())
#example from Filipe Fernandes
#http://nbviewer.jupyter.org/gist/ocefpaf/9730c697819e91b99f1d694983e39a8f
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
g = 9.81
denw = 1025.0 # Seawater density [kg/m**3].
sig = 7.3e-2 # Surface tension [N/m].
a = 1.0 # Wave amplitude [m].
L, h = 100.0, 50.0 # Wave height and water column depth.
k = 2 * np.pi / L
omega = np.sqrt((g * k + (sig / denw) * (k**3)) * np.tanh(k * h))
T = 2 * np.pi / omega
c = np.sqrt((g / k + (sig / denw) * k) * np.tanh(k * h))
# We'll solve the wave velocities in the `x` and `z` directions.
x, z = np.meshgrid(np.arange(0, 160, 10), np.arange(0, -80, -10),)
u, w = np.zeros_like(x), np.zeros_like(z)
def compute_vel(phase):
u = a * omega * (np.cosh(k * (z+h)) / np.sinh(k*h)) * np.cos(k * x - phase)
w = a * omega * (np.sinh(k * (z+h)) / np.sinh(k*h)) * np.sin(k * x - phase)
mask = -z > h
u[mask] = 0.0
w[mask] = 0.0
return u, w
def basic_animation(frames=91, interval=30, dt=0.3):
fig = plt.figure(figsize=(8, 6))
ax = plt.axes(xlim=(0, 150), ylim=(-70, 10))
# Animated.
quiver = ax.quiver(x, z, u, w, units='inches', scale=2)
ax.quiverkey(quiver, 120, -60, 1,
label=r'1 m s$^{-1}$',
coordinates='data')
line, = ax.plot([], [], 'b')
# Non-animated.
ax.plot([0, 150], [0, 0], 'k:')
ax.set_ylabel('Depth [m]')
ax.set_xlabel('Distance [m]')
text = (r'$\lambda$ = %s m; h = %s m; kh = %2.3f; h/L = %s' %
(L, h, k * h, h/L))
ax.text(10, -65, text)
time_step = ax.text(10, -58, '')
line.set_data([], [])
def init():
return line, quiver, time_step
def animate(i):
time = i * dt
phase = omega * time
eta = a * np.cos(x[0] * k - phase)
u, w = compute_vel(phase)
quiver.set_UVC(u, w)
line.set_data(x[0], 5 * eta)
time_step.set_text('Time = {:.2f} s'.format(time))
return line, quiver, time_step
return animation.FuncAnimation(fig, animate, init_func=init,
frames=frames, interval=interval)
from IPython.display import HTML
HTML(basic_animation(dt=0.3).to_jshtml())