{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Color maps of UV-CDAT \n", "\n", "Grep all available color maps and visualize them using EzTemplate\n", "\n", "The CDAT software was developed by LLNL. This tutorial was written by Charles Doutriaux and Jiwoo Lee (2017). 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", "[Download the Jupyter Notebook](Colormap_Tutorial.ipynb)\n" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Import modules\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import vcs\n", "from vcsaddons import EzTemplate\n", "import MV2\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Prepare a dummy array\n", "[Back to Top](#top)\n", "- We need a 2-D dummy array that will contain monotonically increasing numbers each of two rows, to be used for the visualization" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a=[]\n", "num_levels = 224\n", "\n", "for i in range(0,num_levels):\n", " a.append(i/float(num_levels))\n", "\n", "for i in range(0,num_levels):\n", " a.append(i/float(num_levels))\n", "\n", "a = MV2.array(a)\n", "a = MV2.reshape(a, (-1,num_levels))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Colormap visualizing function\n", "[Back to Top](#top)\n", "\n", "- Below function will visualize given colormaps to provide a selection guidance." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math\n", "\n", "def visualize(cmlst, v, loadcmap):\n", " \n", " T = vcs.createtemplate()\n", " T.blank(['title','mean','min','max','dataname','crdate','crtime',\n", " 'units','zvalue','tvalue','xunits','yunits','xname','yname', 'legend'])\n", "\n", " cmlst_num = len(cmlst)\n", " nrows = int(math.ceil(cmlst_num/2.))\n", " \n", " M = EzTemplate.Multi(template=T, rows=nrows, columns=2)\n", " M.margins.top = 0.05\n", " M.margins.bottom = 0.05\n", " M.margins.left = 0.2\n", " M.margins.right = 0.2\n", " M.spacing.vertical= 0.005\n", " \n", " for i in range(0, cmlst_num):\n", " box = v.createboxfill()\n", " box.color_1 = 16\n", " box.color_2 = 240\n", " box.xticlabels('','') # Hide x-axis tick labels\n", " box.yticlabels('','') # Hide y-axis tick labels\n", " box.colormap = loadcmap(cmlst[i])\n", " \n", " if i >= nrows: #right column\n", " jj=1\n", " ii=i-nrows\n", " x = 0.81 # text starting x position\n", " else: # left column\n", " jj=0\n", " ii=i\n", " x = 0.01 # text starting x position\n", "\n", " t = M.get(row=ii, column=jj, legend='none')\n", " t.blank(['legend'])\n", "\n", " v.plot(a, t, box, bg=1)\n", "\n", " subplot_title = vcs.createtext()\n", " subplot_title.string = box.colormap\n", " subplot_title.x = x\n", " subplot_title.y = (t.box1.y1 + t.box1.y2) / 2.\n", " subplot_title.height = 8\n", " subplot_title.halign = 'left'\n", " subplot_title.valign = 'half'\n", " subplot_title.color = 'black'\n", " v.plot(subplot_title, t)\n", " v.removeobject(box)\n", " v.removeobject(subplot_title)\n", " \n", " #print box.colormap ## List available color maps\n", " return(v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Open a VCS canvas\n", "[Back to Top](#top)\n", "\n", "- Let's open a VCS canvas to play with!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v = vcs.init(geometry={\"width\":600,\"height\":1200}, bg=1)\n", "vcs.utils.defaultColorsRange = range(16,240)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# VCS colormaps \n", "[Back to Top](#top)\n", "\n", "- VCS is default module that is being used for the UV-CDAT. You can use VCS color maps which is loaded at the beginning." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "cmlst = v.listelements('colormap')\n", "loadcmap = str\n", "v = visualize(cmlst, v, loadcmap)\n", "\n", "plot_title = v.createtext()\n", "plot_title.string = 'VCS Colormaps'\n", "plot_title.height = 15\n", "plot_title.halign = 'center'\n", "plot_title.valign = 'bottom'\n", "plot_title.x = 0.5\n", "plot_title.y = 0.96\n", "v.plot(plot_title)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matplotlib colormaps in VCS\n", "[Back to Top](#top)\n", "\n", "- You can load and visualize [Matplotlib colormaps](#https://matplotlib.org/examples/color/colormaps_reference.html) in the VCS canvas.\n", "- Use [vcs.colors.matplotlib2vcs](https://uvcdat.llnl.gov/docs/vcs/misc/colors.html) to convert \n", "- For now if same name is already being used in VCS, new name with tail `_000` is assigned, as showing below as \"warning messages\".\n", "- You can also use `vcs.utils.loadmatplotlibcolormaps()` to bring all Matplotlib colormaps to VCS" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "v.clear()\n", "\n", "import matplotlib.pyplot as plt\n", "\n", "# Get list of all available colormaps from matplotlib (except '_r' which is reversed one)\n", "cmlst = [x for x in plt.colormaps() if not '_r' in x]\n", "cmlst = sorted(cmlst, key=lambda s: s.lower()) # Sort as case-insensitive alphabet order\n", "\n", "loadcmap = vcs.colors.matplotlib2vcs\n", "v = visualize(cmlst, v, loadcmap)\n", "\n", "plot_title.string = 'Matplotlib Colormaps for VCS'\n", "#v.plot(plot_title)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.7.3" }, "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": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }