{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_xy_35.py\nThis script illustrates the following concepts:\n   - Use of opacity resources\n     to control opacity of curves and markers in\n     XYPlots.\n   - How to set line opacity and marker opacity to\n     different values using RGBA tuples\n   - Opacity is still functional in colorless plots\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/xy_35.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/xy_35_lg.png\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import packages:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nfrom matplotlib import pyplot as plt\nfrom matplotlib import colors\n\nfrom geocat.viz import util as gvutil"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Create data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Make array of x-values, 64 evenly spaced values between 0 and 1\nf = np.linspace(0., 1., 64)\n\ntwopi = 2 * np.pi\n\n# Create three arrays of y-values to be plotted\nx = np.cos(f * 2 * twopi)\ny = np.cos(f * 3 * twopi)\nz = x * y"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Create figure with 6 axes\nfig, axes = plt.subplots(figsize=(7, 10), nrows=3, ncols=2)\nplt.subplots_adjust(wspace=0.3, hspace=.4)\n\n# Plot first graph\naxesList = [\n    axes[0, 0], axes[0, 1], axes[1, 0], axes[1, 1], axes[2, 0], axes[2, 1]\n]\n\n# Create array of titles of each plot\ntitles = [\n    'Opaque lines', 'Uniformly translucent', 'Independently translucent',\n    'Uniformly translucent markers', 'Independently translucent markers',\n    'Opacities still apply in MonoColor modes'\n]\n\n# Create ticks, axis limits, and titles for each of the 6 plots\nfor ax in range(6):\n\n    # Use geocat-viz function to set main title of plot\n    gvutil.set_titles_and_labels(axesList[ax],\n                                 maintitle=titles[ax],\n                                 maintitlefontsize=10)\n\n    # Use geocat-viz function to set limits and tick locations on x and y axes\n    gvutil.set_axes_limits_and_ticks(axesList[ax],\n                                     xlim=[0, 1],\n                                     ylim=[-1.2, 1.2],\n                                     yticks=np.arange(-1.5, 1.5, 0.5),\n                                     yticklabels=np.arange(-1.5, 1.5, 0.5))\n\n    # Use geocat-viz function to add major and minor ticks on the x and y axes\n    gvutil.add_major_minor_ticks(axesList[ax],\n                                 x_minor_per_major=4,\n                                 y_minor_per_major=5,\n                                 labelsize=\"small\")\n\n# Set standard alpha (transparency) value\nalpha = 0.4\n\n# Plot first graph:\n\n# Plot x, y, and z lines\nline1 = axesList[0].plot(f, x, color='red')\nline2 = axesList[0].plot(f, y, color='limegreen')\nline3 = axesList[0].plot(f, z, color='blue')\n\n# Plot second graph:\n\n# Plot x, y, and z lines with the same level of transparency\nline1 = axesList[1].plot(f, x, color='red', alpha=alpha)\nline2 = axesList[1].plot(f, y, color='limegreen', alpha=alpha)\nline3 = axesList[1].plot(f, z, color='blue', alpha=alpha)\n\n# Plot third graph:\n\n# Plot x, y, and z lines with varying transparencies\nline1 = axesList[2].plot(f, x, color='red', alpha=0.8)\nline2 = axesList[2].plot(f, y, color='limegreen', alpha=0.4)\nline3 = axesList[2].plot(f, z, color='blue', alpha=0.15)\n\n# Plot fourth graph:\n\n# Plot x, y, and z lines and markers with same level of transparency\n# 'ms' is the marker size\n# 'markevery' allows you to place a marker every num steps in the x direction\n# 'mec' is marker edge color\n# 'mfc' is marker face color\nline1 = axesList[3].plot(f,\n                         x,\n                         'o',\n                         ls='-',\n                         color='red',\n                         alpha=alpha,\n                         ms=3,\n                         markevery=.05,\n                         mec='None',\n                         mfc='limegreen')\nline2 = axesList[3].plot(f,\n                         y,\n                         'o',\n                         ls='-',\n                         color='limegreen',\n                         alpha=alpha,\n                         ms=3,\n                         markevery=.05,\n                         mec='None',\n                         mfc='blue')\nline3 = axesList[3].plot(f,\n                         z,\n                         'o',\n                         ls='-',\n                         color='blue',\n                         alpha=alpha,\n                         ms=3,\n                         markevery=.05,\n                         mec='None',\n                         mfc='red')\n\n# Plot fifth graph:\n\n# Create RGBA tuples for lines\nlcolor1 = colors.to_rgba('red', alpha=alpha)\nlcolor2 = colors.to_rgba('limegreen', alpha=alpha)\nlcolor3 = colors.to_rgba('blue', alpha=alpha)\n\n# Create RGBA tuples for markers\nmcolor1 = colors.to_rgba('limegreen', alpha=1.0)\nmcolor2 = colors.to_rgba('blue', alpha=0.4)\nmcolor3 = colors.to_rgba('red', alpha=0.15)\n\n# Plot x, y, and z lines and markers- the lines have the same\n# transparency level, but the markers vary in alpha value\nline1 = axesList[4].plot(f,\n                         x,\n                         'o',\n                         ls='-',\n                         color=lcolor1,\n                         ms=3,\n                         markevery=.1,\n                         mec='None',\n                         mfc=mcolor1)\nline2 = axesList[4].plot(f,\n                         y,\n                         'o',\n                         ls='-',\n                         color=lcolor2,\n                         ms=3,\n                         markevery=.1,\n                         mec='None',\n                         mfc=mcolor2)\nline3 = axesList[4].plot(f,\n                         z,\n                         'o',\n                         ls='-',\n                         color=lcolor3,\n                         ms=3,\n                         markevery=.1,\n                         mec='None',\n                         mfc=mcolor3)\n\n# Plot sixth graph:\n\n# Create RGBA tuples for lines\nlcolor1 = colors.to_rgba('black', alpha=alpha)\nlcolor2 = colors.to_rgba('black', alpha=alpha)\nlcolor3 = colors.to_rgba('black', alpha=alpha)\n\n# Create RGBA tuples for markers\nmcolor1 = colors.to_rgba('black', alpha=1.0)\nmcolor2 = colors.to_rgba('black', alpha=0.4)\nmcolor3 = colors.to_rgba('black', alpha=0.15)\n\n# Plot x, y, and z lines and markers in black- the lines have the same\n# transparency level, but the markers vary in alpha value\nline1 = axesList[5].plot(f,\n                         x,\n                         'o',\n                         ls='-',\n                         color=lcolor1,\n                         ms=3,\n                         markevery=.1,\n                         mec='None',\n                         mfc=mcolor1)\nline2 = axesList[5].plot(f,\n                         y,\n                         'o',\n                         ls='-',\n                         color=lcolor2,\n                         ms=3,\n                         markevery=.1,\n                         mec='None',\n                         mfc=mcolor2)\nline3 = axesList[5].plot(f,\n                         z,\n                         'o',\n                         ls='-',\n                         color=lcolor3,\n                         ms=3,\n                         markevery=.1,\n                         mec='None',\n                         mfc=mcolor3)\n\nplt.show()"
      ]
    }
  ],
  "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.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}