{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Create Your Own Colormap \n", "\n", "Detailed reference document of the VCS colormap can be found at [here](http://localhost:8888/view/vcs/Colormap_Create/Color_map_create_own.png). Colormap for example 4 provided by Karl Taylor.\n", "\n", "Let's create your own VCS colormap.\n", "\n", "The CDAT software was developed by LLNL. This tutorial was written by Charles Doutriaux and Jiwoo Lee (Oct. 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", "\n", "[Download the Jupyter Notebook](Color_map_create_own.ipynb)\n", "\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", "import MV2\n", "from matplotlib.colors import LinearSegmentedColormap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Prepare a dummy array\n", "([back to top](#top))\n", "- Here I prepare a dummy 2D array (240 x 2), which is being used in this tutorial only for the visualization purpose." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = []\n", "\n", "num_levels = 240\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))\n", "\n", "a.id = 'dummy'\n", "\n", "v = vcs.init()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create a colormap\n", "([back to top](#top))\n", "- List of VCS default colormaps can be shown using `vcs.listelements('colormap')`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vcs.listelements('colormap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- You can create your own colormap by using [`createcolormap`](https://uvcdat.llnl.gov/docs/vcs/Canvas.html?highlight=createcolormap#vcs.Canvas.Canvas.createcolormap)\n", "- Let's make one named 'my_cmap'. Now you will see that one is included in the list of available colormaps\n", "- Note that 'my_cmap' colormap is copy of the default colormap at the begining" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.createcolormap('my_cmap')\n", "vcs.listelements('colormap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Set colormap for the canvas by using [`setcolormap`](https://uvcdat.llnl.gov/docs/vcs/Canvas.html?highlight=setcolormap#vcs.Canvas.Canvas.setcolormap) of VCS" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.setcolormap('my_cmap')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Let's make the colormap palette as pure white first\n", "- Note that 'my_cmap' colormap is copy of the default colormap at the beginning" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(0,240):\n", " v.setcolorcell(i,100,100,100)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Assign colors with index number to 'my_cmap' using [`setcolormap`](https://uvcdat.llnl.gov/docs/vcs/Canvas.html?highlight=setcolorcell#vcs.Canvas.Canvas.setcolorcell) of VCS\n", "- Below example colors are given from [here](http://www.december.com/html/spec/color16codes.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.setcolorcell(0,0,0,0) # black\n", "v.setcolorcell(1,50,50,50) # gray\n", "v.setcolorcell(2,75,75,75) # silver\n", "v.setcolorcell(3,100,100,100) # white\n", "v.setcolorcell(4,50,0,0) # maroon\n", "v.setcolorcell(5,100,0,0) # red\n", "v.setcolorcell(6,50,50,0) # olive\n", "v.setcolorcell(7,100,100,0) # yellow\n", "v.setcolorcell(8,0,50,0) # green\n", "v.setcolorcell(9,0,100,0) # lime\n", "v.setcolorcell(10,0,50,50) # teal\n", "v.setcolorcell(11,0,100,100) # aqua\n", "v.setcolorcell(12,0,0,50) # navy\n", "v.setcolorcell(13,0,0,100) # blue\n", "v.setcolorcell(14,50,0,50) # purple\n", "v.setcolorcell(15,100,0,100) # fuchsia" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Use `box.color_1` and `box.color_2` of `boxfill` to set starting and ending colors" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box = v.createboxfill()\n", "box.color_1 = 0\n", "box.color_2 = 15\n", "box.colormap = 'my_cmap'\n", "v.plot(a,box)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Alternative way of creating own colormaps\n", "([back to top](#top))\n", "\n", "- Alternative way of creating your own VCS colormap uses combined capability of UV-CDAT's VCS and [Matplotlib](https://matplotlib.org/index.html).\n", "- Below simply written function receives name of colormap (`cmap_name` as string), and list of colors (`colors` as list of strings or tuples). User can define `colors` using either (1) [named color of Matplotlib](https://matplotlib.org/examples/color/named_colors.html) or (2) tuples of R, G, B numbers (0-1).\n", "- [`LinearSegmentedColormap`](https://matplotlib.org/devdocs/api/_as_gen/matplotlib.colors.LinearSegmentedColormap.html) of [`matplotlib.colors`](https://matplotlib.org/api/colors_api.html), imported from the [begining](#module), stretchs given colors with smooth transition.\n", "- [`matplotlib2vcs`](https://uvcdat.llnl.gov/docs/vcs/misc/colors.html) of `vcs.colors` convert given Matplotlib colormap to VCS colormap." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def CreateOwnColormap(cmap_name, colors):\n", " \n", " # Create a Matplotlib colomap\n", " cm = LinearSegmentedColormap.from_list(cmap_name, colors, N=240)\n", " \n", " # Convert Matplotlib colormap to VCS one\n", " vcs_cmap = vcs.colors.matplotlib2vcs(cm, vcs_name=cmap_name)\n", " \n", " return vcs_cmap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Define name and colors for your own colormap\n", "([back to top](#top))\n", "\n", "- For colors, use list of either (1) [named color of Matplotlib](https://matplotlib.org/examples/color/named_colors.html) or (2) tuples of R, G, B numbers (0-1) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 1\n", "([back to top](#top))\n", "\n", "- Define colors by using [named color of Matplotlib](https://matplotlib.org/examples/color/named_colors.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Define my own colormap (name and sequentially varying colors)\n", "cmap_name = 'cmap1'\n", "colors = ['white','red','orange',\n", " 'yellow','green','blue','violet',\n", " 'purple','black']\n", "CreateOwnColormap(cmap_name, colors)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Visualize example 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "box = v.createboxfill()\n", "box.color_1 = 0\n", "box.color_2 = 239\n", "\n", "box.colormap = 'cmap1'\n", "v.plot(a,box)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 2\n", "([back to top](#top))\n", "\n", "- Define colors by using list of tuples of R, G, B numbers (0-1) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmap_name2 = 'cmap2'\n", "colors2 = [(1,0,0),(1,1,1),(0,0,1)] # red, white, blue\n", "CreateOwnColormap(cmap_name2, colors2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Visualize example 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.clear()\n", "box.colormap = 'cmap2'\n", "v.plot(a,box)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 3\n", "([back to top](#top))\n", "\n", "- Similar to example 1 but with more complicated set up. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cmap_name3 = 'cmap3'\n", "colors3 = ['white','lavender','royalblue','navy',\n", " 'darkviolet','darkred','tomato','darkorange',\n", " 'orange','yellow']\n", "CreateOwnColormap(cmap_name3, colors3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Visualize example 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.clear()\n", "box.colormap = 'cmap3'\n", "v.plot(a,box)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 4\n", "([back to top](#top))\n", "\n", "- Similar to example 2 but with more complicated set up. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "cmap_name4 = 'cmap4'\n", "\n", "colors4 = [(99,93,60),\n", " (99,80,40),\n", " (99,60,20),\n", " (99,40,11),\n", " (99,0,0),\n", " (51,0,0),\n", " (44,11,62),\n", " (0,40,99),\n", " (20,60,99),\n", " (40,80,99),\n", " (60,93,99),\n", " (80,99,99),\n", " ]\n", "\n", "colors4 = MV2.divide(colors4,100.) # Convert range 0-100 to 0-1\n", "\n", "CreateOwnColormap(cmap_name4, colors4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.clear()\n", "box.colormap = 'cmap4'\n", "v.plot(a,box)" ] } ], "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 }