In [4]:
import matplotlib.pyplot as plt
%matplotlib inline
In [5]:
import numpy as np
In [11]:
x = np.arange(-np.pi,np.pi,0.01) # Create an array of x values from -pi to pi with 0.01 interval
y = np.sin(x) # Apply sin function on all x
plt.plot(x,y)
Out[11]:
In [12]:
plt.plot(y)
Out[12]:
In [15]:
x = np.arange(0,10,1) # x = 1,2,3,4,5...
y = x*x # Squared x
plt.plot(x,y,'bo') # plot x and y using blue circle markers
Out[15]:
In [17]:
plt.plot(x,y,'r+') # plot x and y using red plusses
Out[17]:
In [18]:
x = np.arange(-np.pi,np.pi,0.001)
plt.plot(x,np.sin(x))
plt.title('y = sin(x)') # title
plt.xlabel('x (radians)') # x-axis label
plt.ylabel('y') # y-axis label
Out[18]:
In [21]:
# To plot the axis label in LaTex, we can run
from matplotlib import rc
## For sans-serif font:
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
In [20]:
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
In [24]:
plt.plot(x,np.sin(x))
plt.title(r'T = sin($\theta$)') # title, the `r` in front of the string means raw string
plt.xlabel(r'$\theta$ (radians)') # x-axis label, LaTex synatx should be encoded with $$
plt.ylabel('T') # y-axis label
Out[24]:
In [13]:
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.subplot(2, 1, 1)
plt.plot(x1, y1, '.-')
plt.title('Plot 2 graph at the same time')
plt.ylabel('Amplitude (Damped)')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Amplitude (Undamped)')
Out[13]:
In [30]:
plt.plot(x,np.sin(x))
plt.savefig('plot.pdf')
plt.savefig('plot.png')
In [31]:
# To load image into this Jupyter notebook
from IPython.display import Image
Image("plot.png")
Out[31]:
In [ ]: