netCDF4Matplotlib SSSO 2
netCDF4Matplotlib SSSO 2
for
Data Processing and Climate Analysis
Training Objectives
We want to introduce:
Basic concepts of Python programming
Array manipulations
Handling of files
2D visualization
EOFs
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 2 / 94
Background Information
Special Topics
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 3 / 94
Background Information
https://modelingguru.nasa.gov/docs/DOC-2322
/discover/nobackup/jkouatch/pythonTrainingGSFC.tar.gz
After you untar the above file, you will obtain the directory
pythonTrainingGSFC/ that contains:
Examples/
Slides/
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 4 / 94
Background Information
Settings on discover
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 5 / 94
Background Information
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 6 / 94
Background Information
1 Matplotlib 2 netCDF4
2D Plot 3 H5Py
3D Plot 4 Visualization
Basemap toolkit Session
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 7 / 94
Matplotlib
Matplolib
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 8 / 94
Matplotlib
Video Presentation
http://videolectures.net/mloss08_hunter_mat
User’s Guide
http://mural.uv.es/parmur/matplotlib.pdf
Image Galery
http://matplotlib.sourceforge.net/gallery.html
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 9 / 94
Matplotlib
What is Matplotlib?
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 10 / 94
Matplotlib
You can generate plots, histograms, power spectra, bar charts, error
charts, scatter plots, etc., with just a few lines of code.
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 11 / 94
Matplotlib
pyplot
Provides a Matlab-style state-machine interface to the
underlying object-oriented plotting library in matplotlib.
Preferred method of access for interactive plotting.
pylab
Combines the pyplot functionality (for plotting) with the
Numpy functionality (for mathematics and for working
with arrays) in a single namespace, making that
namespace (or environment) even more Matlab-like.
Formerly preferred method of access for interactive
plotting, but still available.
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 12 / 94
Matplotlib
pyplot: pylab:
pyplot.plot(x, y) plot(x, y)
pyplot.show() show()
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 13 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 14 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 15 / 94
Matplotlib 2D Plot
Basic Graph
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 16 / 94
Matplotlib 2D Plot
plot(x,y)
xlabel(’string’) # label the x-axis
ylabel(’string’) # label the y-axis
title(’string’) # write the title of the plot
grid(true/false) # adds grid boxes
savefig(’fileName.type’) # type can be png, ps, pdf, etc
show() # display the graph on the screen
xlim(xmin,xmax) # set/get the xlimits
ylim(ymin,ymax) # set/get the ylimits
hold(True/False) # to overlay figures on the same grap
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 17 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 18 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 19 / 94
Matplotlib 2D Plot
4 def f ( t ):
5 return np . exp ( - t ) * np . cos (2* np . pi * t )
6
7 t1 = np . arange (0.0 , 5.0 , 0.1)
8 t2 = np . arange (0.0 , 5.0 , 0.02)
9
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 20 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 21 / 94
Matplotlib 2D Plot
figure(num)
# allows to plot multiple figures at the same time
# can be called several times
# num: reference number to keep tract of the figure object
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 22 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 23 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 24 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 25 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 26 / 94
Matplotlib 2D Plot
Sample Histogram
1 import numpy as np
2 import matplotlib . pyplot as plt
3
4 mu , sigma = 100 , 15
5 x = mu + sigma * np . random . randn (10000)
6
7 # the histogram of the data
8 n , bins , patches = plt . hist (x , 50 , normed =1 , \
9 facecolor = ’g ’ , alpha =0.75)
10
11 plt . xlabel ( ’ Smarts ’)
12 plt . ylabel ( ’ Probability ’)
13 plt . title ( ’ Histogram of IQ ’)
14 plt . text (60 , .025 , r ’$ \ mu =100 ,\ \ sigma =15 $ ’)
15 plt . axis ([40 , 160 , 0 , 0.03])
16 plt . grid ( True )
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 27 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 28 / 94
Matplotlib 2D Plot
where r signifies that the string is a raw string and not to treat backslashes
and python escapes.
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 29 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 30 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 31 / 94
Matplotlib 2D Plot
Log Plots
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 32 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 33 / 94
Matplotlib 2D Plot
1 import numpy as np
2 import matplotlib . pyplot as plt
3
4 t = np . arange (0.0 , 1.01 , 0.01)
5 s = np . sin (2*2* np . pi * t )
6
7 plt . fill (t , s * np . exp ( -5* t ) , ’r ’)
8 plt . grid ( True )
9 plt . show ()
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 34 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 35 / 94
Matplotlib 2D Plot
Legend
Call signatute:
legend(*args, **kwargs)
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 36 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 37 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 38 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 39 / 94
Matplotlib 2D Plot
Colorbar
You need to include the call: colorbar()
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 40 / 94
Matplotlib 2D Plot
Contour Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 41 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 42 / 94
Matplotlib 2D Plot
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 43 / 94
Matplotlib 3D Plots
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 44 / 94
Matplotlib 3D Plots
3d Graphs
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 45 / 94
Matplotlib 3D Plots
Example of 2D Function
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 46 / 94
Matplotlib 3D Plots
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 47 / 94
Matplotlib 3D Plots
3D Surface Demo
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 48 / 94
Matplotlib 3D Plots
Manipulating Images
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 49 / 94
Matplotlib Visualizing Geographical Data
Matplotlib toolkit
Collection of application-specific functions that extends Matplotlib
functionalities
Provides an efficient way to draw Matplotlib plots over real world
maps
Useful for scientists such as oceanographers and meteorologists.
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 50 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 51 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 52 / 94
Matplotlib Visualizing Geographical Data
Defining Borders
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 53 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 54 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 55 / 94
Matplotlib Visualizing Geographical Data
US Map
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 56 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 57 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 58 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 59 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 60 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 61 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 62 / 94
Matplotlib Visualizing Geographical Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 63 / 94
netCDF4
netCDF4
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 64 / 94
netCDF4
Introduction
http://netcdf4-python.googlecode.com/svn/trunk/docs/
netCDF4-module.html
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 65 / 94
netCDF4
What is netCDF4?
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 66 / 94
netCDF4
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 67 / 94
netCDF4
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 68 / 94
netCDF4
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 69 / 94
netCDF4
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 70 / 94
netCDF4
1 import numpy
2 latitudes [:] = numpy . arange ( -90 ,91 ,2.0)
3 longitudes [:] = numpy . arange ( -180 ,180 ,2.5)
4 levels [:] = numpy . arange (0 ,72 ,1)
5
6 from numpy . random import uniform
7 temp [0:5 ,: ,: ,:] = uniform (
8 size =(5 , levels . size , latitudes . size ,
9 longitudes . size ))
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 71 / 94
netCDF4
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 72 / 94
H5py
H5py
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 73 / 94
H5py
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 74 / 94
H5py
What is H5py?
A Python-HDF5 interface
Allows interaction with with files, groups and datasets using
traditional Python and NumPy syntax.
No need to know anything about HDF5 library.
The files it manipulates are ”plain-vanilla” HDF5 files.
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 75 / 94
H5py
import h5py
hFid = h5py.File(’myfile.h5’, modeType) ’
hFid.close()
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 76 / 94
H5py
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 77 / 94
H5py
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 78 / 94
H5py
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 79 / 94
H5py
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 80 / 94
H5py
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 81 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 82 / 94
Visualizing Gridded Data
Goals
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 83 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 84 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 85 / 94
Visualizing Gridded Data
Plot of SLP
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 86 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 88 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 89 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 90 / 94
Visualizing Gridded Data
Assume that we want to plot the data in prescibed latitude and longitude
ranges.
1 # !/ usr / bin / env python
2
3 import numpy as np
4
5 def sliceLatLon ( lat , lon , ( minLat , maxLat ) , \
6 ( minLon , maxLon )):
7 indexLat = np . nonzero (( lat [:]>= minLat ) &
8 ( lat [:]<= maxLat ))[0]
9 indexLon = np . nonzero (( lon [:]>= minLon ) &
10 ( lon [:]<= maxLon ))[0]
11 return indexLat , indexLon
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 91 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 92 / 94
Visualizing Gridded Data
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 93 / 94
Visualizing Gridded Data
References I
J. Kouatchou and H. Oloso (SSSO) Maplotlib and netCDF4 March 25, 2013 94 / 94