Plotting Climate data in Python using matplotlib

Using the same example as before, we will use sea level pressure (SLP) netcdf data to read and make a plot of annual mean SLP (globally). Most of the script line below is commented to show their purpose:

from netCDF4 import Dataset as NetCDFFile #this package reads nc data
import matplotlib.pyplot as plt #python plotting package
import numpy as np #numpy is for all scientific computing
from mpl_toolkits.basemap import Basemap  
#Basemap package helps to plot lat lon dimension data

print('reading the file ..')

nc = NetCDFFile('/homes/afahad/data/slp_erai_1979_2016.nc') #read the file

lat = nc.variables['lat'][:] #this the data from file
lon = nc.variables['lon'][:]
time = nc.variables['time'][:]
mslp = nc.variables['slp'][:]
nc.close()

print('Making climatology...')

# we will take average over time dimension, which is 0 (1st index index in #python)

a=(np.mean(mslp,0))/100 #climatology of SLP

# make some plot
print('Ploting...')
plt.figure(figsize=(9,5)) #setting the figure size

map = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90,\
            llcrnrlon=-180,urcrnrlon=180,resolution='l')      
#This like sets the lat lon of the plot. Projection cylinder.

map.drawcoastlines(linewidth=.5)  #draws coastline

parallels = np.arange(-90,91,30.) # make latitude lines ever 30 degrees from 30N-50N
meridians = np.arange(-180,180,60.) # make longitude lines every 60 degrees from 95W to 70W

#labelling the lat and lon dimesion

map.drawparallels(parallels,labels=[1,0,0,0],linewidth=0.2,fontsize=8)
map.drawmeridians(meridians,labels=[0,0,0,1],linewidth=0.2,fontsize=8)

lons,lats= np.meshgrid(lon,lat) #2D lat lon to plot contours
x,y = map(lons,lats)

clevsf = np.arange(960,1040,4)
clevs = np.arange(1020,1040,10)

#clevs and clevsf sets the contour interval of contour and filled contour. if you don't set it, it will plot default values.

csf = map.contourf(x,y,a,clevsf,extend='both',cmap='coolwarm') #filled contour
cb = map.colorbar(csf,"right", extend='both',size="3%", pad="1%")
cs = map.contour(x,y,a,clevs,colors='k',linewidths=0.3)

plt.clabel(cs, inline=True, fmt='%1.0f', fontsize=3, colors='k')
plt.title('mean SLP')
plt.show()
plt.savefig('test_slp.eps', format='eps', dpi=1000) #saving figure

print('done! To see the plot, type: display test_slp.eps')

Install matplotlib if you are not using anaconda:

python -m pip install -U pip setuptools
python -m pip install matplotlib

fore more: https://matplotlib.org/faq/installing_faq.html

If you are using anaconda 3 like me, to install packages follow: How to read netcdf file in Python