Ez Templates

This tutorial shows how to easily create templates for making images with multiple plots using CDAT's VCS tool.

To download this Jupyter Notebook, right click on the link and choose "Download Linked File As..." or "Save Link as...". Remember where you saved the downloaded file, which should have an .ipynb extension. (You'll need to launch the Jupyter notebook or JupyterLab instance from the location where you store the notebook file.)

Introduction

VCS's template objects allow users to have multiple plots on a single page or a single canvas. Tweaking the templates can be tedious, however. This is where the EzTemplate comes handy since it helps create a VCS template object for the most common plot configurations.

Setting Up the Notebook

Back to Top

In [1]:
from __future__ import division, print_function
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 = list(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 the VCS template object you could further edit 
        display = canvas.plot(clt[i*2], template, gm)
    return canvas.plot(box)
Downloading: 'BlueMarble.ppm' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/BlueMarble.ppm
Downloading: 'clt.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/clt.nc
Downloading: 'geo.1deg.ctl' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/geo.1deg.ctl
Downloading: 'geo.1deg.gmp' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/geo.1deg.gmp
Downloading: 'geo.1deg.grb' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/geo.1deg.grb
Downloading: 'meshfill.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/meshfill.nc
Downloading: 'navy_land.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/navy_land.nc
Downloading: 'rlut_CERES_000001-000012_ac.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/rlut_CERES_000001-000012_ac.nc
Downloading: 'sampleCurveGrid4.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/sampleCurveGrid4.nc
Downloading: 'sampleGenGrid3.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/sampleGenGrid3.nc
Downloading: 'sftbyrgn.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/sftbyrgn.nc
Downloading: 'sftlf_10x10.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/sftlf_10x10.nc
Downloading: 'sftlf_visus.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/sftlf_visus.nc
Downloading: 'so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc
Downloading: 'so_Omon_GISS-E2-R_historicalNat_r5i1p1_185001-187512_2timesteps.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/so_Omon_GISS-E2-R_historicalNat_r5i1p1_185001-187512_2timesteps.nc
Downloading: 'so_Omon_HadGEM2-CC_historical_r1i1p1_185912-186911_2timesteps.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/so_Omon_HadGEM2-CC_historical_r1i1p1_185912-186911_2timesteps.nc
Downloading: 'so_Omon_MPI-ESM-LR_1pctCO2_r1i1p1_185001-185912_2timesteps.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/so_Omon_MPI-ESM-LR_1pctCO2_r1i1p1_185001-185912_2timesteps.nc
Downloading: 'swan.four.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/swan.four.nc
Downloading: 'ta_ncep_87-6-88-4.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/ta_ncep_87-6-88-4.nc
Downloading: 'tas_ccsr-95a.xml' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ccsr-95a.xml
Downloading: 'tas_ccsr-95a_1979.01-1979.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ccsr-95a_1979.01-1979.12.nc
Downloading: 'tas_ccsr-95a_1980.01-1980.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ccsr-95a_1980.01-1980.12.nc
Downloading: 'tas_ccsr-95a_1981.01-1981.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ccsr-95a_1981.01-1981.12.nc
Downloading: 'tas_ccsr-95a_1982.01-1982.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ccsr-95a_1982.01-1982.12.nc
Downloading: 'tas_ccsr-95a_1983.01-1983.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ccsr-95a_1983.01-1983.12.nc
Downloading: 'tas_ccsr-95a_1984.01-1984.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ccsr-95a_1984.01-1984.12.nc
Downloading: 'sftlf_ccsr.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/sftlf_ccsr.nc
Downloading: 'tas_dnm-95a.xml' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_dnm-95a.xml
Downloading: 'tas_dnm-95a_1979.01-1979.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_dnm-95a_1979.01-1979.12.nc
Downloading: 'tas_dnm-95a_1980.01-1980.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_dnm-95a_1980.01-1980.12.nc
Downloading: 'tas_dnm-95a_1981.01-1981.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_dnm-95a_1981.01-1981.12.nc
Downloading: 'tas_dnm-95a_1982.01-1982.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_dnm-95a_1982.01-1982.12.nc
Downloading: 'tas_dnm-95a_1983.01-1983.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_dnm-95a_1983.01-1983.12.nc
Downloading: 'tas_dnm-95a_1984.01-1984.12.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_dnm-95a_1984.01-1984.12.nc
Downloading: 'sftlf_dnm.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/sftlf_dnm.nc
Downloading: 'taylor.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/taylor.nc
Downloading: 'test_anim.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/test_anim.nc
Downloading: 'test_col.asc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/test_col.asc
Downloading: 'testgrib2.ctl' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/testgrib2.ctl
Downloading: 'testgrib2.grib2' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/testgrib2.grib2
Downloading: 'testgrib2.idx' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/testgrib2.idx
Downloading: 'testpp.pp' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/testpp.pp
Downloading: 'thermo.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/thermo.nc
Downloading: 'vertical.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/vertical.nc
Downloading: 'tas_ukmo_con.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ukmo_con.nc
Downloading: 'tas_gavg_rnl_ecm.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_gavg_rnl_ecm.nc
Downloading: 'tas_ecm_1979.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_ecm_1979.nc
Downloading: 'tas_cru_1979.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_cru_1979.nc
Downloading: 'psl_6h.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/psl_6h.nc
Downloading: 'ts_da.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/ts_da.nc
Downloading: 'tas_mo.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_mo.nc
Downloading: 'tas_mo_clim.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_mo_clim.nc
Downloading: 'tas_6h.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/tas_6h.nc
Downloading: 'th_yr.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/th_yr.nc
Downloading: 'th_yr.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/th_yr.nc
Downloading: 'th_yr.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/th_yr.nc
Downloading: 'geos5-sample.nc' from 'https://cdat.llnl.gov/cdat/sample_data/' in: /Users/davis278/miniconda3/envs/cdat81-mesa/share/cdat/sample_data/geos5-sample.nc

Creating Templates for 2x3 Plots

Back to Top

Let's say we want to have 6 plots, divided into 3 rows of 2 plots per row. Notice the canvas automatically switches to a portrait orientation.

In [2]:
Multiple = EzTemplate.Multi(rows=3, columns=2, x=canvas)
for i in range(6):
    template = Multiple.get()  # This is the VCS template object you could further edit
    display = canvas.plot(clt[i*2], template, gm)
display  # To render final image in the notebook
Out[2]:

Accessing a Specific Template

Back to Top

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

In [3]:
canvas.clear()
template = Multiple.get(row=0, column=0)  # This is the VCS template object you could further edit
canvas.plot(clt[0], template, gm)
template = Multiple.get(row=1, column=1)  # This is the VCS template object you could further edit
canvas.plot(clt[6], template, gm)
template = Multiple.get(row=2, column=0)  # This is the VCS template object you could further edit
canvas.plot(clt[12], template, gm)
Out[3]:

Subplot Legends

Back to Top

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)  # This is the VCS template object you could further edit
canvas.plot(clt[0], template, gm)
template = Multiple.get(row=1, column=1, legend="local")  # This is the VCS template object you could further edit
canvas.plot(clt[6], template, gm)
template = Multiple.get(row=2, column=0)  # This is the VCS template object you could further edit
canvas.plot(clt[12], template, gm)
Out[4]:

Controlling Subplot Aspects

Back to Top

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

Back to Top

By default, each individual template is generated based on the default VCS template. You can pass your template the base template you would like to be used by default.

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]:
In [7]:
Multiple = EzTemplate.Multi(rows=3, columns=2, template=base_template)
canvas.clear()
for i in range(6):
    template = Multiple.get()  # This is the VCS template object you could further edit
    display = canvas.plot(clt[i*2], template, gm)
display  # To render final image in the notebook
Out[7]:

Overall Plot Margins

Back to Top

EzTemplate lets you control the area around the plot by using the margins attribute of the Multi object.

For the purpose of illustration, we use drastic values for the margins. Since more space is available at the bottom, the legend thickness increases.

In [8]:
Multiple.margins.top = .2  # 20% margin at the top
Multiple.margins.bottom = .3 # 30% margin from the bottom
Multiple.margins.left = .2  # 20% from left
Multiple.margins.right = .3  # 30% from right
plot_all(Multiple)
Out[8]:

Spacing Between Plots

Back to Top

Users can also control how much spacing should be allowed between plots by using the spacing attribute.

Again, we use drastic values for educational purposes.

In [9]:
Multiple.spacing.vertical = .1  # 10 % of total page height
Multiple.spacing.horizontal = .2  # 20 % of total page width
plot_all(Multiple)
Out[9]:

Controlling the Legend

Back to Top

As we mentioned earlier, EzTemplate tries to make the best possible use of the free space to plot the overall legend.

Legend Direction

Back to Top

By default, we use the bottom margin to draw a horizontal legend, but we can opt for a vertical legend in which case the right margin space will be used.

In [10]:
Multiple.legend.direction = "vertical"
plot_all(Multiple)
Out[10]:

Thickness

Back to Top

The legend bar thickness can also be modified.

In [11]:
Multiple.legend.thickness = .15 # 15% of available space
plot_all(Multiple)
Out[11]:

Stretching the Legend

Back to Top

The stretch attribute controls the length of the legend bar.

In [12]:
Multiple.legend.stretch = .55 # 55% of available space
plot_all(Multiple)
Out[12]:

Fonts

Back to Top

Sometimes, if you have too many plots, the font scaling can get too drastic and you end up not seeing anything, so by default, the fonts will not be scaled to less than 80%. Should you want to change this default font scaling limit (to make the fonts smaller, for example), use the fontlimit argument when executing get command.

In [13]:
canvas.clear()
Multiple = EzTemplate.Multi(rows=5, columns=5)
for i in range(Multiple.rows * Multiple.columns):
    template = Multiple.get(column = i % Multiple.columns, row = i // Multiple.columns, fontlimit=.1)  # This is the VCS template object you could further edit
    display = canvas.plot(clt[i*2], template, gm)
canvas.plot(box)
Out[13]:

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.

If you have questions about this notebook, please email our CDAT Support address, cdat-support@llnl.gov.