{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_xy_7_2.py\nAn example of a double y plot: two separate line with their own unique axis.\n\nThis script illustrates the following concepts:\n   - Drawing an XY plot with two different Y axes\n   - Changing the title on the Y axis\n   - Changing the line dash pattern in an XY plot\n   - Changing the line color for multiple curves in an XY plot\n   - Setting the mininum/maximum value of the Y axis in 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_7.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/xy_7_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 matplotlib.pyplot as plt\nimport numpy as np\nimport xarray as xr\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/TestData.xy3.nc\"))\n# Extract a slice of the data\nds = ds.isel(case=0, time=slice(0, 36))"
      ]
    },
    {
      "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 (with two different y-axes)\nfig, ax1 = plt.subplots(figsize=(7, 6.5))\n\n# Plot data\nax1.plot(ds.time, ds.T, color=\"blue\", linestyle=\"-\", linewidth=0.9)\n\n# Usa geocat.viz.util convenience function to add minor and major tick lines\ngvutil.add_major_minor_ticks(ax1, x_minor_per_major=5, labelsize=14)\n\n# Usa geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions\n# Set axes limits, and tick values\ngvutil.set_axes_limits_and_ticks(ax1,\n                                 xlim=(1970, 1973),\n                                 ylim=(0.0, 16.0),\n                                 yticks=np.arange(0, 17, 3))\n\n# Usa geocat.viz.util convenience function to set titles and labels without calling several matplotlib functions\ngvutil.set_titles_and_labels(ax1,\n                             maintitle=\"Curves Offset\",\n                             xlabel=ds.time.long_name,\n                             ylabel=f\"{ds.T.long_name} [solid]\")\n\n# Create second y-axis\nax2 = ax1.twinx()\n\n# Use geocat.viz.util convenience function to add minor and major tick lines\ngvutil.add_major_minor_ticks(ax2, x_minor_per_major=5, labelsize=14)\n\n# Line-plot data\nax2.plot(ds.time,\n         ds.P,\n         color=\"red\",\n         linestyle=\"--\",\n         dashes=[6.5, 3.7],\n         linewidth=0.9)\n\n# Use geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions\n# Set axes limits, and tick values\ngvutil.set_axes_limits_and_ticks(ax2,\n                                 ylim=(1008.0, 1024.0),\n                                 yticks=np.arange(1008, 1025, 3))\n\n# Set second y-axis label\nax2.set_ylabel(f\"{ds.P.long_name} [dash]\", fontsize=16)\n\n# Show the plot\nplt.tight_layout()\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
}