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.)
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.
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)
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.
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
Rather than accessing templates in order, you can retrieve a specific object directly.
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)
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.
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)
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.
Multiple.list()
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.
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)
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
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.
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)
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.
Multiple.spacing.vertical = .1 # 10 % of total page height
Multiple.spacing.horizontal = .2 # 20 % of total page width
plot_all(Multiple)
As we mentioned earlier, EzTemplate tries to make the best possible use of the free space to plot the overall legend.
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.
Multiple.legend.direction = "vertical"
plot_all(Multiple)
Multiple.legend.thickness = .15 # 15% of available space
plot_all(Multiple)
Multiple.legend.stretch = .55 # 55% of available space
plot_all(Multiple)
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.
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)
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.