{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_xy_12.py\nThis script illustrates the following concepts:\n   - Emphasizing part of a curve in an XY plot\n   - Drawing longitude labels on the X axis\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/xy_12.ncl\n    - Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/xy_12_1_lg.png and https://www.ncl.ucar.edu/Applications/Images/xy_12_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\n# xarrays\nds = xr.open_dataset(gdf.get(\"netcdf_files/uv300.nc\"))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Method 1: Splitting the line into parts and coloring them differently\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "U = ds.isel(time=0, lon=5, drop=True).U\n\nplt.figure(figsize=(7, 7))\nax = plt.axes()\n\nbins = [0, 5, 20]\n# Slicing data in Python excludes the last value. To include the last value we\n# can increment it by 1. This ensures that the different colored line segments\n# touch\nstart = U.data[bins[0]:bins[1] + 1]\nhighlight = U.data[bins[1]:bins[2] + 1]\nend = U.data[bins[2]:]\n\nax.plot(U.lat[bins[0]:bins[1] + 1], start, color='black', linewidth=0.5)\nax.plot(U.lat[bins[1]:bins[2] + 1], highlight, color='red', linewidth=1)\nax.plot(U.lat[bins[2]:], end, color='black', linewidth=0.5)\n\n# Use geocat.viz.util convenience function to set axes parameters\ngvutil.set_axes_limits_and_ticks(\n    ax,\n    ylim=(-10, 40),\n    xlim=(-90, 90),\n    xticks=np.arange(-90, 91, 30),\n    yticks=np.arange(-10, 41, 10),\n    xticklabels=['90S', '60S', '30S', '0', '30N', '60N', '90N'])\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=3,\n                             y_minor_per_major=5,\n                             labelsize=14)\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Highlight Part of a Line\",\n                             ylabel=U.long_name + \" \" + U.units)\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Method 2: Drawing a polygon around the section of interest\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "U = ds.isel(time=0, lon=84, drop=True).U\n\nplt.figure(figsize=(7, 7))\nax = plt.axes()\n\nbins = [5, 24]\n# Slicing data in Python excludes the last value. To include the last value we\n# can increment it by 1. This ensures that the highlight extends thorugh the\n# last bin value\nhighlight = U.data[bins[0]:bins[1] + 1]\n\n# Define bounds for region centered on the data with a width of 4\nnlat = np.shape(highlight)[0]\ntop = np.empty(nlat)\nbottom = np.empty(nlat)\n\nfor k in range(0, nlat):\n    top[k] = highlight[k] + 2\n    bottom[k] = highlight[k] - 2\n\n# Plot curves that bound the region to be colored\nax.plot(U.lat[bins[0]:bins[1] + 1], top, color='salmon')\nax.plot(U.lat[bins[0]:bins[1] + 1], bottom, color='salmon')\n\n# Fill the area between the bounds\nax.fill_between(U.lat[bins[0]:bins[1] + 1], top, bottom, color='salmon')\n\nax.plot(U.lat, U.data, color='black', linewidth=0.5)\n\n# Use geocat.viz.util convenience function to set axes parameters\ngvutil.set_axes_limits_and_ticks(\n    ax,\n    ylim=(-10, 50),\n    xlim=(-90, 90),\n    xticks=np.arange(-90, 91, 30),\n    yticks=np.arange(-10, 51, 10),\n    xticklabels=['90S', '60S', '30S', '0', '30N', '60N', '90N'])\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=3,\n                             y_minor_per_major=5,\n                             labelsize=14)\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Highlight Part of a Line\",\n                             ylabel=U.long_name + \" \" + U.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
}