{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Shading Using Patterns in VCS \n", "\n", "\n", "This notebook shows how to use patterns in vcs.\n", "\n", "Pattern can be used with isofill, boxfill, meshfill and fillarea object.\n", "\n", "In this notebook we are using primirarly boxfill\n", "\n", "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", "[Download Jupyter Notebook](Shading_With_Patterns_in_VCS.ipynb)" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Prepare Notebook Elements\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import requests\n", "r = requests.get(\"https://uvcdat.llnl.gov/cdat/sample_data/clt.nc\",stream=True)\n", "with open(\"clt.nc\",\"wb\") as f:\n", " for chunk in r.iter_content(chunk_size=1024):\n", " if chunk: # filter local_filename keep-alive new chunks\n", " f.write(chunk)\n", "\n", "import cdms2\n", "# and load data\n", "f = cdms2.open(\"clt.nc\")\n", "clt = f(\"clt\",time=slice(0,1),squeeze=1) # Get first month" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create default Graphic Method\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import vcs\n", "import cdms2\n", "x=vcs.init(bg=True, geometry=(800,600))\n", "gm = vcs.createboxfill()\n", "gm.boxfill_type = \"custom\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# Let's look at the data w/o pattern\n", "x.plot(clt,gm)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Mask some data\n", "\n", "Now let's assume we are only interested in areas where clt is greater than 60%\n", "let's shade out areas where clt is < 60%\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import MV2\n", "bad = MV2.less(clt,60.).astype(\"f\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Method 1: Regular Masking\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "# let's create a second boxfill method \n", "gm2 = vcs.createboxfill()\n", "gm2.boxfill_type = \"custom\"\n", "# and a template for it\n", "tmpl2 = vcs.createtemplate()\n", "tmpl2.legend.priority=0\n", "gm2.levels = [[0.5,1.]] \n", "gm2.fillareacolors = [\"black\",]\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Method 2: Using Opacity\n", "Let's use some opacity to \"see\" what's bellow\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gm2.fillareaopacity = [50]\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Method 3: Using Patterns\n", "Rather than opacity, we can use patterns, that let us see better what's \"underneath\"\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gm2.fillareastyle = \"pattern\"\n", "gm2.fillareaindices = [10]\n", "gm2.fillareaopacity = [100]\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we can control the size of patterns\n", "gm2.fillareapixelscale = 2.\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Controling Patterns Size\n", "We can make the patterns bigger or smaller, using spacing\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Bigger\n", "gm2.fillareapixelspacing = [20,20]\n", "gm2.fillareapixelscale=None\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# or smaller\n", "gm2.fillareapixelspacing = [5,5]\n", "gm2.fillareapixelscale=None\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Size and Opacity\n", "We can still add opacity\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gm2.fillareaopacity = [25.]\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pattern Color can also be controled\n", "Using hatch rather than pattern we can control the shading color\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gm2.fillareaopacity = [100.]\n", "gm2.fillareastyle = \"hatch\"\n", "gm2.fillareacolors = [\"red\"]\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Patterns legend\n", "\n", "[Back to Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# could even have a legend\n", "tmpl2.legend.x1 = .54\n", "tmpl2.legend.x2 = .62\n", "tmpl2.legend.y1 = .885\n", "tmpl2.legend.y2 = .985\n", "tmpl2.legend.priority=0\n", "gm2.legend = {.5:\" Bad\"}\n", "x.clear()\n", "x.plot(clt,gm)\n", "x.plot(bad,gm2,tmpl2)" ] } ], "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 }