Plotting

Import Libraries

In [2]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

Plotting using Matplotlib

Pyplot interface

In [3]:
x = np.linspace(0,6,80)
y = np.exp(-x)*np.sin(4*x)

Basic plot

In [4]:
plt.plot(x,y);   # use ";" on last plot command 
                  # (if that is the last cell command)
                  # to suppress output: [<matplotlib.lines.Line2D at 0x1113b8e80>] 

Add formatting

In [5]:
plt.plot(x,y+1, 'ko-'  )   # linestyle: k is black, o is symbol, - is solid line
                           # Other colors: b,r,g,c,m; other lines: --, -., : other symbols: .s^v*dpx+<>h

plt.xlabel('The x-axis label')   
plt.ylabel('The y-axis label', fontsize=16)
plt.title('Some title');

Draw multiple curves on the same plot

In [6]:
plt.plot(x,     y+0.5,        '--', color='black', linewidth=2)
plt.plot(x[1:], 1/x[1:]**0.2, 'x-', color='blue',  markersize=6)

plt.yscale('log')                                              
plt.xlim([0,6])
plt.ylim([0.1,10])
plt.xlabel(r'With Symbols: $\alpha_{s}\beta^{s}$', fontsize=20) 
plt.ylabel('some label', fontsize=20)                           
plt.legend(['curve 1', 'curve2'], fontsize=16, frameon=False, loc='upper left');  

Subplots: like a table of plots

In [10]:
plt.rc("font", size=10)        # Set all font sizes

plt.subplot(2,2,1)             # rows, cols, active;  where active is the plot that is currently active
plt.plot(x,y)                  

plt.subplot(2,2,2)             # add labels, legends to each subplot separately
plt.plot(x,y*x)
plt.xlabel('my x')             # format each plot as previously shown

plt.subplot(2,2,3)
plt.plot(x,np.sqrt(x))

plt.subplot(2,2,4)
plt.plot(x,1/(x+1))

plt.tight_layout()             # keep labels from overlapping

Contour plot of 3-D data: z(x,y)

In [8]:
#--------- Set the X,Y,Z data
Lx = 6
Ly = 6
nx = 100 
ny = 100
x = np.linspace(-Lx/2,Lx/2,nx)
y = np.linspace(-Ly/2,Ly/2,ny)
X,Y = np.meshgrid(x,y)
Z = np.sin(2*X)*np.sin(3*Y)*X/Y*np.exp(np.abs(Y))

#---------- make the plot
plt.subplot(1,2,1)
plt.contour(X,Y,Z,20)          # 20 is the number of contour levels chosen
plt.subplot(1,2,2)
plt.contourf(X,Y,Z,20)         # contourf is a filled contour plot
plt.colorbar();                # add a colorbar for the Z scale   

Object oriented interface

In [45]:
import numpy as np
import matplotlib                     # for saving to pdf
matplotlib.use('PDF')                # for saving to pdf
import matplotlib.pyplot as plt
%matplotlib inline
In [54]:
x = np.linspace(0,10,25)
y_exp = x**0.6 + (np.random.rand(25) -0.5)        # ploting data series below
y_1   = x**0.6
y_2   = np.sin(x)*2
y_3   = np.exp(-0.2*x)*np.cos(2*x)*2
In [55]:
fig, ax = plt.subplots(1,1, figsize=(7,5))     # create a figure and axes
plt.rcParams.update({'font.size': 18})         # change all font sizes (specialize below)

ax.tick_params(direction='in', top=True, right=True)

ax.plot(x, y_1,   '-',  color='gray',  linewidth=4)
ax.plot(x, y_2,   '--', color='blue',  linewidth=1)
ax.plot(x, y_3,   ':',  color='red',   linewidth=1)
ax.plot(x, y_exp, 'o',  color='black', markersize=6);

ax.set_xlabel(r"$v_{rms} + \theta$", fontsize=20)
ax.set_ylabel("position",            fontsize=20)
ax.set_xlim([0,10])
ax.set_ylim([-2,6])
ax.legend(["T (K)", 
           r"$\Delta_{max}=2$", 
           r"$\rho$ (kg/m$^3$)", 
           "Exp"], loc='upper left', frameon=False, fontsize=16);

plt.savefig("plot.pdf")                         # for saving file plot.pdf
In [ ]: