08 matplotlib

matplotlib.pyplot is a collection of command that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.




01  Example:  Cs Sputtering Yield


This example is related to Physics problems. Here we are considering the Sputtering Yield of Cs atom from the GaAs cathode surface. Sputter Yield is the number of atoms removed from the surface due to incident ion bombardment. We have obtained the data from SRIM simulation. However, we need to digitize the data to save plotting time in 3d surface damage simulation.
 
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import mat


X = np.arange(0, 350+0.5 , 0.5 )
Y = np.arange(0, 350+0.5 , 0.5 )


for i in range(701):
    if 0.2<X[i]<=1.5:
        Y[i]=13*(X[i]-0.2)
    elif 1.5<X[i]<=350:
        Y[i]=1+18*math.exp(-0.05*X[i])
    else:
        Y[i]=0
plt.xlabel('Ion Energy[KeV]')
plt.ylabel('Cs Sputtering Yield(atom/ion) / 1000')
plt.title('Cs sputtering Yield with respect to Proton Energy')
 
plt.plot(X, Y,'-.')
plt.show()

 
and we see the following output



 



02  Example:  3D Surface Plot

 Here we are generating data, and plotting 3d surface. For surface plot in matplotlib its better to make a grid mesh. However one of the drawback of matplotlib is that, its not efficient enough for plotting 3d surface, especially if lots of data is invoked. 

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np


fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data. 
X = np.arange(-1, 2, 1)
Y = np.arange(-1, 2, 1)


X, Y = np.meshgrid(X, Y)


R_fetch = -(X**2 + Y**2)
 
Z = R_fetch

# Plot the surface. 
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)
 
plt.xlabel('x[m]')
plt.ylabel('y[m]')


# Customize the z axis.
ax.zaxis.set_major_locator(LinearLocator(5))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

# Add a color bar which maps values to colors. 
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()


and we see the following output,