{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_bar_2.py\nThis script illustrates the following concepts:\n   - Drawing bars instead of curves in an XY plot\n   - Changing the aspect ratio of a bar plot\n   - Drawing filled bars up or down based on a Y reference value\n   - Setting the minimum/maximum value of the Y axis in a bar plot\n   - Using named colors to indicate a fill color\n   - Creating array of dates to use as x-axis tick labels\n   - Creating a main title\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/bar_2.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/bar_2_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\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 xarrays\nds = xr.open_dataset(gdf.get(\"netcdf_files/soi.nc\"))\ndsoik = ds.DSOI_KET\ndate = ds.date\nnum_months = np.shape(date)[0]\n\n# Dates in the file are represented by year and month (YYYYMM)\n# representing them fractionally will make ploting the data easier\n# This produces the same results as NCL's yyyymm_to_yyyyfrac() function\ndate_frac = np.empty_like(date)\nfor n in np.arange(0, num_months, 1):\n    yyyy = int(date[n] / 100)\n    mon = (date[n] / 100 - yyyy) * 100\n    date_frac[n] = yyyy + (mon - 1) / 12"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate figure (set its size (width, height) in inches) and axes\nplt.figure(figsize=(12, 6))\nax = plt.axes()\n\n# Create a list of colors based on the color bar values\ncolors = ['red' if (value > 0) else 'blue' for value in dsoik[::8]]\nplt.bar(date_frac[::8],\n        dsoik[::8],\n        align='edge',\n        edgecolor='black',\n        color=colors,\n        width=8 / 12,\n        linewidth=.6)\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=4,\n                             y_minor_per_major=5,\n                             labelsize=20)\n\n# Use geocat.viz.util convenience function to set axes parameters\ngvutil.set_axes_limits_and_ticks(ax,\n                                 ylim=(-3, 3),\n                                 yticks=np.linspace(-3, 3, 7),\n                                 yticklabels=np.linspace(-3, 3, 7),\n                                 xlim=(date_frac[40], date_frac[-16]),\n                                 xticks=np.linspace(1900, 1980, 5))\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Darwin Southern Oscillation Index\",\n                             ylabel='Anomalies',\n                             maintitlefontsize=28,\n                             labelfontsize=20)\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
}