{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Polar Plots \n", "\n", "This Notebook demonstrates how to create polar plots in VCS\n", "\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 the Jupyter notebook](PolarPlots.ipynb)" ] }, { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Prepare modules and function to visualize\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import vcs\n", "import vcsaddons\n", "import numpy\n", "\n", "# class to visualize canvas\n", "import tempfile\n", "import base64\n", "class VCSAddonsNotebook(object):\n", " def __init__(self, x):\n", " self.x = x\n", " def _repr_png_(self):\n", " fnm = tempfile.mktemp()+\".png\"\n", " x.png(fnm)\n", " encoded = base64.b64encode(open(fnm, \"rb\").read())\n", " return encoded\n", " def __call__(self):\n", " return self\n", " \n", "def show(canvas):\n", " return VCSAddonsNotebook(canvas)()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Prepare data and vcs objects\n", "\n", "\n", "[Back to Top](#top)\n", "\n", "Here we define some dataset for later use in the notebook, feel free to set r to any of these to see the changes." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Angles\n", "nPoints = 75\n", "theta0 = .001\n", "e = numpy.exp(1.)\n", "pi = numpy.pi\n", "thetaN = 2.*pi\n", "delta = (thetaN-theta0)/(nPoints-1)\n", "theta = numpy.arange(theta0,thetaN,delta)\n", "\n", "# Archimede's spiral\n", "r_archimede = theta\n", "# Rose Curves\n", "nPetals = 6\n", "r_rose = 4.*numpy.cos(nPetals*theta)\n", "# simple\n", "r_simple = 5. * numpy.sin(theta)\n", "# Another simple one\n", "r_simple_2 = 4. - 4.*numpy.cos(theta)\n", "# Leaf\n", "r_leaf = (1 + 0.9*numpy.cos(8*theta))*(1 + 0.1*numpy.cos(24*theta))*(0.9 + 0.05*numpy.cos(200*theta))*(1 +numpy.sin(theta))\n", "# Love\n", "r_love = 2*pi/numpy.sqrt(theta) + pi/4. -2.*numpy.sin(theta)+numpy.sin(theta)*numpy.sqrt(numpy.abs(numpy.cos(theta)))/(numpy.sin(theta)+1.4)\n", "\n", "# set which curve to vizualize\n", "r = r_archimede\n", "\n", "# Initialize vcs canvas\n", "x=vcs.init(bg=True, geometry=(600,600))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic (default) Plot\n", "\n", "Let's plot this with a very basic plot.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create polar graphic method\n", "polar1=vcs.init(bg=True)\n", "polar = vcsaddons.createpolar()\n", "\n", "# Associate vcs canvas with it\n", "polar.x = x\n", "# Plot\n", "show(polar.plot(r,theta))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Controlling the markers\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polar.markersizes = [2.]\n", "polar.markercolors = [\"red\"]\n", "polar.markertypes = [\"square\"]\n", "x.clear()\n", "show(polar.plot(r,theta))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Plotting Multiple Sets (groups) At Once\n", "\n", "We can plot 3 different sets/groups at once, each with their own set of color/markers.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r2 = numpy.array([r,r_simple,r_simple_2])\n", "polar.markercolors = [\"red\",\"green\",\"blue\"]\n", "polar.markertypes = [\"square\",\"dot\",\"diamond\"]\n", "polar.markersizes = [2.,5.,2.]\n", "x.clear()\n", "show(polar.plot(r2,theta))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Clockwise Plots\n", "\n", "Sometimes it can be useful to have $\\theta$ rotating clockwise.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polar.markercolors = [\"red\"]\n", "polar.markersizes= [1]\n", "polar.markertypes = [\"square\"]\n", "import EzTemplate\n", "M = EzTemplate.Multi(columns=2,rows=1)\n", "x.clear()\n", "polar.plot(r,theta,template=M.get(row=0,column=0))\n", "polar.clockwise = True\n", "show(polar.plot(r,theta,template=M.get(row=0,column=1)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Connecting The Markers\n", "\n", "We can also connect markers.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polar.clockwise = False\n", "polar.linepriority=1\n", "polar.linetypes=[\"dot\"]\n", "polar.linecolors = [\"blue\"]\n", "polar.linewidths = [3.]\n", "x.clear()\n", "show(polar.plot(r,theta))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polar.theta_tick_count = 3\n", "x.clear()\n", "show(polar.plot(r,theta))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Controlling Magnitude (Radial) Labels/Ticks\n", "\n", "We can control the value of the magnitude labels.\n", "Using vcs templates and text orientation objects we can control these labels angle.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ticks = {}\n", "for a in range(45,361,45):\n", " ticks[float(a)/180.*numpy.pi] = r\"$%i^o$\" % a\n", "polar.xticlabels1 = ticks\n", "#polar.yticlabels1 = {1.:\"one\",3.:\"three\"}\n", "polar.datawc_y1 = 0\n", "polar.datawc_y2= 7\n", "polar.yticlabels1 = {1.:\"one\",3.:\"three\",5:\"five\"}\n", "polar.magnitude_tick_angle = pi/4.\n", "#polar.yticlabels1 = None\n", "x.clear()\n", "to = vcs.createtextorientation()\n", "to.angle = -45\n", "tmpl = vcs.createtemplate()\n", "tmpl.ylabel1.textorientation = to\n", "show(polar.plot(r,theta, template=tmpl))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Angular ($\\theta$) Offset\n", "\n", "Sometimes we need $\\theta$ to start at some other values than 0 radians.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polar.theta_offset = pi/4.\n", "to.angle = -90\n", "x.clear()\n", "show(polar.plot(r,theta, template=tmpl))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Magnitude Sub ticks\n", "\n", "We can add sub ticks on the magnitude (radial) circles\n", "Using vcs template and line objects we can control the appearance of these subticks.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# reset a few things\n", "polar.theta_offset = 0.\n", "polar.magnitude_tick_angle = 0\n", "to.angle = 0\n", "\n", "dot = vcs.createline()\n", "dot.type=\"dot\"\n", "dot.color = [\"grey\"]\n", "tmpl.ymintic1.line = dot\n", "tmpl.ymintic1.priority = 1\n", "polar.magnitude_mintics = [.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5]\n", "x.clear()\n", "show(polar.plot(r,theta, template=tmpl))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Non Linear Magnitude (Radial) Scales\n", "\n", "Sometimes it can be useful to have a non linear scale for the radius.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "polar.magnitude_ticks = [1,1.1,2,7]\n", "polar.datawc_y1 = 1.e20\n", "polar.datawc_y2 = 1.e20\n", "x.clear()\n", "show(polar.plot(r,theta, template=tmpl))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Amplitude To Control Markers Colors\n", "\n", "It can be useful to link the markers color to the magnitude.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.clear()\n", "polar = vcsaddons.createpolar()\n", "polar.x=x\n", "polar.markercolors = [16, 66, 116, 143, 162, 181, 200, 219]\n", "polar.markercolorsource = \"magnitude\"\n", "tmpl = vcs.createtemplate()\n", "tmpl.legend.x1=.9\n", "tmpl.legend.x2=.99\n", "tmpl.legend.y1 = .2\n", "tmpl.legend.y2=.8\n", "x.clear()\n", "show(polar.plot(r,theta,template=tmpl))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using $\\theta$ To Control Markers Colors\n", "\n", "It can be useful to link the markers color to $\\theta$.\n", "\n", "[Back To Top](#top)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x.clear()\n", "polar = vcsaddons.createpolar()\n", "polar.x=x\n", "polar.markercolors = [16, 66, 116, 143, 162, 181, 200, 219]\n", "polar.markercolorsource = \"theta\"\n", "tmpl = vcs.createtemplate()\n", "tmpl.legend.priority=0\n", "x.clear()\n", "show(polar.plot(r,theta,template=tmpl))" ] } ], "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 }