{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_xy_13.py\nThis script illustrates the following concepts:\n    - Adding error bars on an XY plot\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/xy_13.ncl\n    - Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/xy_13_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\nimport xarray as xr\nimport matplotlib.pyplot as plt\nfrom matplotlib import collections as mc\n\nimport geocat.datafiles as gdf\nfrom geocat.viz import util as gvutil"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read in data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Open a netCDF data file using xarray default engine and load the data into\n# xarrays\nds = xr.open_dataset(gdf.get(\"netcdf_files/uv300.nc\"))\n\n# Extract data\nV = ds.isel(time=0, lon=30, drop=True).V\n\n# Create arrays to represent the magnitude of error above and below the line\n# This data is arbitrary and you should replace these arrays with the actual\n# error for your dataset\nerr_above = V.data + 1.5\nerr_below = V.data - 1\nx = range(0, 64)  # the x values used to plot the data and error bars\n\n# Make a tuple to represent the bottom and top points of the error bar\nerr_below = tuple(zip(x, err_below))\nerr_above = tuple(zip(x, err_above))\n\n# Make a tuple containing those points to describe the line segment\nsegments = tuple(zip(err_below, err_above))\n\n# Create a line collection so we can plot all of the segments with one call\nbars = mc.LineCollection(segments, colors='black', linewidths=0.5)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(8, 8))\nax = plt.axes()\n\n# Plots the data with markers\nplt.plot(x, V.data, color='black', linewidth=0.5, marker='.')\n\n# Plot the error bars\nax.add_collection(bars)\n\n# Use geocat.viz.util convenience function to set axes parameters\ngvutil.set_axes_limits_and_ticks(ax,\n                                 xlim=(0, 70),\n                                 ylim=(-9, 9),\n                                 xticks=np.arange(0, 71, 10),\n                                 yticks=np.arange(-9, 10, 3),\n                                 yticklabels=np.arange(-9.0, 10.0, 3.0))\n\n# Use geocat.viz.util convenience function to add minor and major tick lines\ngvutil.add_major_minor_ticks(ax,\n                             x_minor_per_major=5,\n                             y_minor_per_major=3,\n                             labelsize=14)\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Example of error bars\",\n                             ylabel=V.long_name + \" \" + V.units)\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
}