{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_scatter_4.py\nThis script illustrates the following concepts:\n   - Drawing a scatter plot with a regression line\n   - Drawing a time series plot\n   - Calculating the least squared regression for a one dimensional array\n   - Smoothing data so that seasonal cycle is less prominent\n   - Changing the markers in an XY plot\n   - Changing the marker color in an XY plot\n   - Changing the marker size 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/scatter_4.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/scatter_4_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/b003_TS_200-299.nc\"),\n                     decode_times=False)\n# Extract variable\nts = ds.TS.sel(lat=60, lon=180, method='nearest')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Preprocess data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Smooth data so that seasonal cycle is less prominent.\n# This is for demo purposes only  so that the regression line is more sloped.\nts_rolled = ts.rolling(time=40, center=True).mean().dropna('time')\n\n# Calculate regression line\nm, b = np.polyfit(ts_rolled.time, ts_rolled.values, 1)\nregline_vals = [m * x + b for x in ts.time]"
      ]
    },
    {
      "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=(6.2, 6))\nax = plt.gca()\n\n# Scatter-plot the data\nplt.scatter(ts_rolled.time, ts_rolled.values, c='red', s=3)\n\n# Plot a regression line\nplt.plot(ts.time, regline_vals, 'black')\n\n# specify X and Y axis limits\nplt.xlim([6000, 9500])\nplt.ylim([268.0, 271.5])\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=5,\n                             labelsize=12)\n\n# Use geocat.viz.util convenience function to set titles and labels without calling several matplotlib functions\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Output from regline\",\n                             xlabel=\"simulation time\",\n                             ylabel=\"Surface temperature\")\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
}