EzTemplates

Ez Templates

This tutorial shows how to easily create templates for CDAT's VCS.

The CDAT software was developed by LLNL. This tutorial was written by Charles Doutriaux. This work was performed under the auspices of the U.S. Department of Energy by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.

Table of Contents

Introduction

More often than not, one would like to have mutliple plots on a single page (or vcs canvas, in our case).

This can be achieved via VCS's templates objects. Tweaking the templates can be tidious, so this is where the EzTemplate comes in handy. It helps create a vcs template object for most common plot configurations.

Setting up the notebook

In [1]:
from __future__ import print_function, division
from vcsaddons import EzTemplate
from IPython.display import Image
import vcs
import cdms2

vcs.download_sample_data_files()
canvas = vcs.init(bg=True)
f = cdms2.open(vcs.sample_data+"/clt.nc")
clt = f("clt")
gm = vcs.createisofill()
levels = range(0,110,25)
gm.levels = levels
gm.fillareacolors = vcs.getcolors(levels)

box = vcs.createline()
box.x = [0.001, .999, .999, .001, .001]
box.y = [0.001, .001, .999, .999, .001]

def plot_all(M):
    canvas.clear()
    for i in range(M.rows * M.columns):
        template = M.get(column = i % M.columns, row = i // M.columns)  # This is a vcs template object you could further edit
        display = canvas.plot(clt[i*2], template, gm)
    return canvas.plot(box)
('MD5:', '/Users/doutriaux1/anaconda2/envs/nightly/share/vcs/sample_files.txt')
Downloading: 'th_yr.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/doutriaux1/anaconda2/envs/nightly/share/cdat/sample_data/th_yr.nc
Downloading: 'th_yr.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/doutriaux1/anaconda2/envs/nightly/share/cdat/sample_data/th_yr.nc
Downloading: 'th_yr.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/doutriaux1/anaconda2/envs/nightly/share/cdat/sample_data/th_yr.nc

Creating Templates for 2x3 plots

Let's say we want to have 6 plots, split on 3 rows of 2. Notice the canvas automatically switches to portrait orientation.

In [2]:
Multiple = EzTemplate.Multi(rows=3, columns=2, x=canvas)
for i in range(6):
    template = Multiple.get()  # This is a vcs template object you could further edit
    display = canvas.plot(clt[i*2], template, gm)
display  # Causes the notebook to render the final picture
/Users/doutriaux1/anaconda2/envs/nightly/lib/python2.7/site-packages/vtk/util/numpy_support.py:135: FutureWarning: Conversion of the second argument of issubdtype from `complex` to `np.complexfloating` is deprecated. In future, it will be treated as `np.complex128 == np.dtype(complex).type`.
  assert not numpy.issubdtype(z.dtype, complex), \
Out[2]:

Accessing a specific template

Rather than accessing templates in order, you can retrieve a specific object directly.

In [3]:
canvas.clear()
template = Multiple.get(row=0, column=0)  # Retrieve the object in the first position located at row 0, column 0.
canvas.plot(clt[0], template, gm)
template = Multiple.get(row=1, column=1)  # Retrieve the object located at row 1, column 1.
canvas.plot(clt[6], template, gm)
template = Multiple.get(row=2, column=0)  # Retrieve the object located at row 2, column 0.
canvas.plot(clt[12], template, gm)
Out[3]:

Local legend

You probably noticed that only one legend is plotted. Sometimes you might want a separate legend for a specific subplot. This can be achieved when you get the template.

In [4]:
canvas.clear()
canvas.clear()
template = Multiple.get(row=0, column=0)  # Retrieve the object in the first position located at row 0, column 0.
canvas.plot(clt[0], template, gm)
template = Multiple.get(row=1, column=1, legend="local")  # Create a legend for the subplot located at row 1, column 1.
canvas.plot(clt[6], template, gm)
template = Multiple.get(row=2, column=0)  # Retrieve the object located at row 2, column 0.
canvas.plot(clt[12], template, gm)
Out[4]:

Controlling the subplots aspects

Many general aspects of the plot can be controlled via the Multi object. A list of controllable aspects and their current values can be accessed via the list() function.

In [5]:
Multiple.list()
----------Template (P) member (attribute) listings ----------
rows = 3
columns = 2
template = default
member = margins
	top = 0.05
	bottom = 0.075
	right = 0.05
	left = 0.033
member = spacing
	horizontal = 0.05
	vertical = 0.035
member = legend
	direction = horizontal
	stretch = 0.8
	thickness = 0.2
	fat = 0.05

Base template

By default each individual template is generated from the default vcs template, but you can pass it the base template you would like to use instead.

In [6]:
base_template = vcs.createtemplate()
base_template.blank(["title","crtime","crdate",
                     "source","dataname", "units",
                     "tname", "tvalue", "zvalue",
                     "min", "max",
                     "xname", "xlabel1"])
canvas.clear()
canvas.plot(clt,base_template, gm)
Out[6]: