Matplotlib

In [46]:
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
import numpy as np
import matplotlib.pyplot as plt

Single graph

In [47]:
f = lambda x: x*np.sin(x)*np.sin(50*x)

n = 200
x = np.linspace(0.0,1.0,n)

plt.plot(x, f(x))
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Plot of f(x)')
plt.grid(True)

Multiple graphs

In [48]:
f = lambda x: x*np.sin(x)*np.sin(50*x)
g = lambda x: x*np.cos(x)*np.cos(50*x)

n = 200
x = np.linspace(0.0,1.0,n)

plt.plot(x,f(x),x,g(x))
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('Plot of f(x) and g(x)')
plt.legend(('f(x)','g(x)'))
plt.grid(True)

Control line style

In [49]:
f = lambda x: x*np.sin(x)*np.sin(50*x)
g = lambda x: x*np.cos(x)*np.cos(50*x)

n = 200
x = np.linspace(0.0,1.0,n)

plt.plot(x,f(x),'r-',linewidth=2)
plt.plot(x,g(x),'b--')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of f(x) and g(x)')
plt.legend(('f(x)','g(x)'))
plt.grid(True)

We can also specify parameters like this

In [53]:
plt.plot(x,f(x),c='red',ls='dashed',lw=2);

Show symbols

In [62]:
f = lambda x: x*np.sin(x)*np.sin(50*x)
g = lambda x: np.cos(x)*np.cos(50*x)

n = 200
x = np.linspace(0.0,1.0,n)

plt.plot(x,f(x),'k-o',markevery=10)
plt.plot(x,g(x),'r--s',markevery=10)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of f(x) and g(x)')
plt.legend(('f(x)','g(x)'),loc='upper right')
plt.grid(True)

Matrix of plots

In [50]:
n  = 200
x  = np.linspace(0.0,1.0,n)
y1 = np.sin(50*x)
y2 = np.log(x+1)+np.tanh(x)
y3 = x**2 + x*np.cos(10*x)
y4 = np.cos(10*x)+np.sin(5*x)

plt.figure(figsize=(10,8))
lw = 1.5

plt.subplot(221)
plt.plot(x,y1,'k-',linewidth=lw)
plt.xlabel('$x$'); plt.ylabel('$y_1$')

plt.subplot(222)
plt.plot(x,y2,'r--',linewidth=lw)
plt.xlabel('$x$'); plt.ylabel('$y_2$')

plt.subplot(223)
plt.plot(x,y3,'g-.',linewidth=lw)
plt.xlabel('$x$'); plt.ylabel('$y_3$')

plt.subplot(224)
plt.plot(x,y4,'b:',linewidth=lw)
plt.xlabel('$x$'); plt.ylabel('$y_4$');