{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ez Templates \n", "\n", "\n", "This tutorial shows how to easily create templates for making images with multiple plots using [CDAT](https://cdat.llnl.gov)'s VCS tool.\n", "\n", "To [download this Jupyter Notebook](EzTemplates.ipynb), 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.)" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Setting Up the Notebook\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import division, print_function\n", "from vcsaddons import EzTemplate\n", "from IPython.display import Image\n", "import vcs\n", "import cdms2\n", "\n", "vcs.download_sample_data_files()\n", "canvas = vcs.init(bg=True)\n", "f = cdms2.open(vcs.sample_data+\"/clt.nc\")\n", "clt = f(\"clt\")\n", "gm = vcs.createisofill()\n", "levels = list(range(0,110,25))\n", "gm.levels = levels\n", "gm.fillareacolors = vcs.getcolors(levels)\n", "\n", "box = vcs.createline()\n", "box.x = [0.001, .999, .999, .001, .001]\n", "box.y = [0.001, .001, .999, .999, .001]\n", "\n", "def plot_all(M):\n", " canvas.clear()\n", " for i in range(M.rows * M.columns):\n", " template = M.get(column = i % M.columns, row = i // M.columns) # This is the VCS template object you could further edit \n", " display = canvas.plot(clt[i*2], template, gm)\n", " return canvas.plot(box)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Templates for 2x3 Plots\n", "\n", "[Back to Top](#top)\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple = EzTemplate.Multi(rows=3, columns=2, x=canvas)\n", "for i in range(6):\n", " template = Multiple.get() # This is the VCS template object you could further edit\n", " display = canvas.plot(clt[i*2], template, gm)\n", "display # To render final image in the notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Accessing a Specific Template\n", "\n", "[Back to Top](#top)\n", "\n", "Rather than accessing templates in order, you can retrieve a specific object directly." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "canvas.clear()\n", "template = Multiple.get(row=0, column=0) # This is the VCS template object you could further edit\n", "canvas.plot(clt[0], template, gm)\n", "template = Multiple.get(row=1, column=1) # This is the VCS template object you could further edit\n", "canvas.plot(clt[6], template, gm)\n", "template = Multiple.get(row=2, column=0) # This is the VCS template object you could further edit\n", "canvas.plot(clt[12], template, gm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Subplot Legends\n", "\n", "[Back to Top](#top)\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas.clear()\n", "canvas.clear()\n", "template = Multiple.get(row=0, column=0) # This is the VCS template object you could further edit\n", "canvas.plot(clt[0], template, gm)\n", "template = Multiple.get(row=1, column=1, legend=\"local\") # This is the VCS template object you could further edit\n", "canvas.plot(clt[6], template, gm)\n", "template = Multiple.get(row=2, column=0) # This is the VCS template object you could further edit\n", "canvas.plot(clt[12], template, gm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Controlling Subplot Aspects\n", "\n", "[Back to Top](#top)\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple.list()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Base Template\n", "\n", "[Back to Top](#top)\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "base_template = vcs.createtemplate()\n", "base_template.blank([\"title\",\"crtime\",\"crdate\",\n", " \"source\",\"dataname\", \"units\",\n", " \"tname\", \"tvalue\", \"zvalue\",\n", " \"min\", \"max\",\n", " \"xname\", \"xlabel1\"])\n", "canvas.clear()\n", "canvas.plot(clt,base_template, gm)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple = EzTemplate.Multi(rows=3, columns=2, template=base_template)\n", "canvas.clear()\n", "for i in range(6):\n", " template = Multiple.get() # This is the VCS template object you could further edit\n", " display = canvas.plot(clt[i*2], template, gm)\n", "display # To render final image in the notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overall Plot Margins\n", "\n", "[Back to Top](#top)\n", "\n", "EzTemplate lets you control the area around the plot by using the `margins` attribute of the Multi object.\n", "\n", "For the purpose of illustration, we use drastic values for the margins. Since more space is available at the bottom, the legend thickness increases." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple.margins.top = .2 # 20% margin at the top\n", "Multiple.margins.bottom = .3 # 30% margin from the bottom\n", "Multiple.margins.left = .2 # 20% from left\n", "Multiple.margins.right = .3 # 30% from right\n", "plot_all(Multiple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Spacing Between Plots\n", "\n", "[Back to Top](#top)\n", "\n", "Users can also control how much spacing should be allowed between plots by using the `spacing` attribute.\n", "\n", "Again, we use drastic values for educational purposes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple.spacing.vertical = .1 # 10 % of total page height\n", "Multiple.spacing.horizontal = .2 # 20 % of total page width\n", "plot_all(Multiple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Controlling the Legend\n", "\n", "[Back to Top](#top)\n", "\n", "As we mentioned earlier, EzTemplate tries to make the best possible use of the free space to plot the overall legend.\n", "\n", "### Legend Direction\n", "\n", "[Back to Top](#top)\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple.legend.direction = \"vertical\"\n", "plot_all(Multiple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Thickness\n", "\n", "[Back to Top](#top)\n", "\n", "The legend bar thickness can also be modified. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple.legend.thickness = .15 # 15% of available space\n", "plot_all(Multiple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Stretching the Legend \n", "\n", "[Back to Top](#top)\n", "\n", "The `stretch` attribute controls the length of the legend bar." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Multiple.legend.stretch = .55 # 55% of available space\n", "plot_all(Multiple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fonts\n", "\n", "[Back to Top](#top)\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "canvas.clear()\n", "Multiple = EzTemplate.Multi(rows=5, columns=5)\n", "for i in range(Multiple.rows * Multiple.columns):\n", " template = Multiple.get(column = i % Multiple.columns, row = i // Multiple.columns, fontlimit=.1) # This is the VCS template object you could further edit\n", " display = canvas.plot(clt[i*2], template, gm)\n", "canvas.plot(box)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "If you have questions about this notebook, please email our [CDAT Support](cdat-support@llnl.gov) address, cdat-support@llnl.gov." ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:cdat81-mesa] *", "language": "python", "name": "conda-env-cdat81-mesa-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": { "height": "calc(100% - 180px)", "left": "10px", "top": "150px", "width": "165px" }, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }