Matplotlib Tutorial


In [2]:
import numpy as np         #include numpy package 
import matplotlib.pyplot as plt   #include matplotlib package 
#to allow drawing the plots inside jupyter notebook
%matplotlib inline

Line Plot


In [4]:
x = np.linspace(0,5*np.pi,500) # data of x axis
ySine = np.sin(x) # data of y axis (sine wave)
 
plt.plot(x,ySine)            # plot the sine wave 
plt.xlabel('x axis label')   # add label to the x-axis 
plt.ylabel('y axis label')   # add label to the y-axis 
plt.xlim(0,16)               # limit the x-axes to region 0:16
plt.ylim(-1,1)               # limit the y-axes to region -1:1
plt.title('Sine Wave')       # title of the figure 
plt.legend(['Sine'])         # add legend to the figure
plt.grid(True)               # show the grid layout 
plt.savefig("./figures/test-Sine-Wave.png")   # save figure


Scatter Plot


In [5]:
x = np.linspace(0,5*np.pi,50) # data of x axis
ySine = np.sin(x)             # data of y axis (sine wave)

plt.scatter(x,ySine)
plt.xlabel('x axis label') # label of x axis
plt.ylabel('y axis label') # label of y axis
plt.xlim(0,16)
plt.ylim(-1,1)
plt.title('Sine Wave')     # title of the plot
plt.legend(['Sine'])       # legend of the plot
plt.grid(True)


Histogram Plot


In [9]:
mu, sigma = 1, 0.5 # mean and std 
x = np.random.normal(mu,sigma,50000) # generate data with the aformentioned mean and std
y = plt.hist(x,bins=50,normed=True,color='r') # plot the generated data as histogram


Box Plot


In [15]:
# fake up some data
spread = np.random.rand(50) * 100
center = np.ones(25) * 50
flier_high = np.random.rand(10) * 100 + 100
flier_low = np.random.rand(10) * -100
data = np.concatenate((spread, center, flier_high, flier_low), 0)

# basic plot
plt.subplot(1,5,1)
plt.boxplot(data)

plt.subplot(1,5,2)
plt.boxplot(data,notch=False)

plt.subplot(1,5,3)
plt.boxplot(data,notch=True)

plt.subplot(1,5,4)
plt.boxplot(data, 0, 'gD')

plt.subplot(1,5,5)
plt.boxplot(data, 0, ' ')


Out[15]:
{'boxes': [<matplotlib.lines.Line2D at 0x7fe86f114d50>],
 'caps': [<matplotlib.lines.Line2D at 0x7fe86f122c90>,
  <matplotlib.lines.Line2D at 0x7fe86f12f310>],
 'fliers': [<matplotlib.lines.Line2D at 0x7fe86f12ff90>],
 'means': [],
 'medians': [<matplotlib.lines.Line2D at 0x7fe86f12f950>],
 'whiskers': [<matplotlib.lines.Line2D at 0x7fe86f114f50>,
  <matplotlib.lines.Line2D at 0x7fe86f122650>]}

Multiple Figures On The Same Figure


In [34]:
x = np.linspace(0,5*np.pi,500) # data of x axis
ySine = np.sin(x) # data of y axis for sine wave
yCosine = np.cos(x) # data of y axis for cosine wave

plt.plot(x,ySine,'r') # plot sine wave with red color
plt.plot(x,yCosine,'b')  # plot sine wave with blue color
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.xlim(0,16)
plt.ylim(-1,1)
plt.title('Sine and Cosine Waves')
plt.legend(['Sine','Cosine'])
plt.grid(True)


Figure Subplots


In [36]:
x = np.linspace(0,5*np.pi,500) # data of x axis
ySine = np.sin(x) # data of y axis for sine wave
yCosine = np.cos(x) # data of y axis for cosine wave

plt.subplot(2,1,1)
plt.plot(x,ySine,'r.') # plot sine wave with red color
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.xlim(0,16)
plt.ylim(-1,1)
plt.title('Sine Wave')
plt.legend(['Sine'])
plt.grid(True)

plt.subplot(2,1,2)
plt.plot(x,yCosine,'b^')  # plot sine wave with blue color
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.xlim(0,16)
plt.ylim(-1,1)
plt.title('Cosine Wave')
plt.legend(['Cosine'])
plt.grid(True)


Options for the color characters are:

'r' = red 'g' = green 'b' = blue 'c' = cyan 'm' = magenta 'y' = yellow 'k' = black 'w' = white

Options for line styles are

'-' = solid '--' = dashed ':' = dotted '-.' = dot-dashed '.' = points 'o' = filled circles '^' = filled triangles