{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_leg_1.py\nThis script illustrates the following concepts:\n   - Drawing a legend inside an XY plot\n   - Changing the width and height of a legend\n   - Turning off the perimeter around a legend\n   - Changing the font size of legend labels\n   - Customizing the labels in a legend\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/leg_1.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/leg_1_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/uv300.nc\"))\n# Extract variables\nuz = ds.U.isel(time=0).mean(dim=['lon'])\nvz = ds.V.isel(time=0).mean(dim=['lon'])"
      ]
    },
    {
      "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=(5, 5))\nax = plt.gca()\n\n# Plot data and add a legend\nplt.plot(vz.lat, vz.values, '--', dashes=[6.5, 3.7], c='gray', label='V')\nplt.plot(uz.lat, uz.values, c='gray', label='U')\nplt.legend(loc='upper left', frameon=False, prop={'weight': 'bold'})\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=12)\n\n# Use geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions\n# Set axes limits, tick values, and tick labels to show latitude & longitude (i.e. North (N) - South (S))\ngvutil.set_axes_limits_and_ticks(\n    ax,\n    xlim=(-90, 90),\n    ylim=(-10, 40),\n    xticks=np.linspace(-90, 90, 7),\n    xticklabels=['90S', '60S', '30S', '0', '30N', '60N', '90N'])\n\n# Show the plot\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
}