Skip to content

Scatter Plotting Tutorial

barronh edited this page Apr 20, 2016 · 2 revisions

How to make a scatter plot?

Below are two methods for making scatter plots with optional regression lines. The first is in the python environment and the second is from the command line interface. These tutorials show work with CMAQ, but can easily be modified for CAMx or GEOS-Chem (see [modifications](Tutorial Modifications))

Method 1 - PseudoNetCDF in Python

# input must exist on your machine
inpath = '/path/to/inputfile'

# silly example plotting against self
inpathx = inpath
inpathy = inpath

from matplotlib import pyplot as plt

# import NetCDF Processing loading functions
from PseudoNetCDF.pncparse import pncparse


# Read in all inputs with the following options
# returns all files and an archive of arguments (including defaults)
# implicit option '-f', 'netcdf' opens the file as netcdf (other -f options "bpch", "uamiv")
# option '--from-conv', 'ioapi' uses the ioapi meta-data to derive latitudes, longitudes, and time variables
# option '--slice', 'LAY,0' takes the first layer
infiles, args = pncparse(args = [inpathx, inpathy, '--from-conv', 'ioapi', '--variables', 'O3', '--slice', 'LAY,0'])
xfile, yfile = infiles[:2]
varx = xfile.variables['O3'][:]
vary = yfile.variables['O3'][:]

label = 'O3'
# Add regression line
#from scipy.stats.mstats import linregress
#m, b, rval, pval, stderr = linregress(varx, vary)
# label = 'y  = %g x + %g (r=%g, p=%g)' % (m, b, rval, pval)
# plt.plot([vmin, vmax], [m*vmin + b, m * vmax + b], ls = '-', color = 'k', label = 'trendline')

plt.scatter(varx, vary, c = 'k', s = 20, label = label)

vmin = np.minimum(varx.min(), vary.min())
vmax = np.maximum(varx.max(), vary.max())
plt.xlim(vmin, vmax)
plt.ylim(vmin, vmax)

plt.legend()
plt.savefig('scatter.png')

Method 2 - Script from CLI

pncscatter.py -v O3 /path/to/inputfilex /path/to/inputfiley rootoutput
Clone this wiki locally