PyEarthScience: Python examples for Earth Scientists

XY-plots

Using PyNGL

Line plot with

- marker
- different colors
- legend
- title
- x-axis label
- y-axis label

In [1]:
import numpy as np
import Ngl, Nio

Generate x- and y-values.


In [2]:
x2 = np.arange(100)

data   = np.arange(1,40,5)
linear = np.arange(100)
square = [v * v for v in np.arange(0,10,0.1)]

#-- retrieve maximum size of plotting data
maxdim = max(len(data),len(linear),len(square))

#-- create 2D arrays to hold 1D arrays above
y = -999.*np.ones((3,maxdim),'f')                   #-- assign y array containing missing values
y[0,0:(len(data))]   = data
y[1,0:(len(linear))] = linear
y[2,0:(len(square))] = square

Draw data, set title and axis labels.


In [3]:
#-- open a workstation
wkres                      =  Ngl.Resources()       #-- generate an res object for workstation
wks                        =  Ngl.open_wks("png","plot_xy_simple_PyNGL",wkres)

#-- set resources
res                        =  Ngl.Resources()       #-- generate an res object for plot
res.tiMainString           = "Title string"         #-- set x-axis label
res.tiXAxisString          = "x-axis label"         #-- set x-axis label
res.tiYAxisString          = "y-axis label"         #-- set y-axis label

res.vpWidthF               =  0.9                   #-- viewport width
res.vpHeightF              =  0.6                   #-- viewport height

res.caXMissingV            =  -999.                 #-- indicate missing value
res.caYMissingV            =  -999.                 #-- indicate missing value

#-- marker and line settings
res.xyLineColors           =  ["blue","green","red"] #-- set line colors
res.xyLineThicknessF       =  3.0                   #-- define line thickness
res.xyDashPatterns         =  [0,0,2]               #-- ( none, solid, cross )
res.xyMarkLineModes        =  ["Markers","Lines","Markers"] #-- marker mode for each line
res.xyMarkers              =  [16,0,2]              #-- marker type of each line
res.xyMarkerSizeF          =  0.01                  #-- default is 0.01
res.xyMarkerColors         =  ["blue","green","red"] #-- set marker colors

#-- legend settings
res.xyExplicitLegendLabels = [" data"," linear"," square"]  #-- set explicit legend labels
res.pmLegendDisplayMode    = "Always"               #-- turn on the drawing
res.pmLegendOrthogonalPosF = -1.13                  #-- move the legend upwards
res.pmLegendParallelPosF   =  0.15                  #-- move the legend to the right
res.pmLegendWidthF         =  0.2                   #-- change width
res.pmLegendHeightF        =  0.10                  #-- change height
res.lgBoxMinorExtentF      =  0.16                  #-- legend lines shorter

#-- draw the plot
plot = Ngl.xy(wks,x2,y,res)

#-- the end
Ngl.delete_wks(wks)                            #-- this need to be done to close the graphics output file
Ngl.end()

Show the plot in this notebook.


In [4]:
from IPython.display import Image
Image(filename='plot_xy_simple_PyNGL.png')


Out[4]: