{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_xy_4.py\nThis script illustrates the following concepts:\n   - Drawing a scatter plot\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   - Creating your own markers for an XY plot\n   - Drawing a legend\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/xy_4.ncl\n    - Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/xy_4_1_lg.png and https://www.ncl.ucar.edu/Applications/Images/xy_4_2_lg.png\n                         \nWays of specifying marks:\n    - matplotlib.markers has an extensive `list <https://matplotlib.org/3.2.1/api/markers_api.html>`_ of predefined markers\n    - Mathematical symbols described `here <https://matplotlib.org/3.2.1/tutorials/text/mathtext.html>`_ can be used\n    - Unicode characters\n    - If you still cannot find the symbol you are looking for, a custom made\n      `Path <https://matplotlib.org/3.2.1/api/path_api.html#matplotlib.path.Path>`_ instance can be used to draw your own marker\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\nimport matplotlib.path as mpath\nimport math\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/AtmJan360_xy_4.nc'),\n                     decode_times=False)\n\n# Extract a slice of the data\nds = ds['T']\nt = ds.isel(lev=0).drop('lev')\nt = t.isel(lat=32).drop('lat')\nt = t.isel(lon=29).drop('lon')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot with standard markers:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(8, 8))\nax = plt.axes()\n\nplt.scatter(t.time, t.data, color='red')\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Scatter Plot\",\n                             xlabel=t['time'].long_name,\n                             ylabel=t.long_name)\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=4,\n                             labelsize=14)\n\n# Calculate xlim by rounding the min value down and the max value up to a\n# multiple of 5\nxmin = 5 * math.floor(t.time.min().data / 5)\nxmax = 5 * math.ceil(t.time.max().data / 5)\n\ngvutil.set_axes_limits_and_ticks(\n    ax,\n    xlim=(xmin, xmax),\n    ylim=(220.0, 232.0),\n    xticklabels=[' ', 131160, ' ', 131170, ' ', 131180, ' ', 131190],\n    yticklabels=np.arange(220.0, 233.0, 2.0))\n\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot with custom markers:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(8, 8))\nax = plt.axes()\n\n# Divide the data into arbitrary sections so each can be drawn with a different\n# type of marker\ndata1 = t.data[0:8]\ntime1 = t.time[0:8]\n\ndata2 = t.data[8:16]\ntime2 = t.time[8:16]\n\ndata3 = t.data[16:24]\ntime3 = t.time[16:24]\n\ndata4 = t.data[24:]\ntime4 = t.time[24:]\n\n# marker='s' creates a square. This is from matplotlib.markers\n# This is not to be confused with the kwarg `s` which sets the marker size\nplt.scatter(time1, data1, color='blue', marker='s', label='matplotlib.markers')\n\n# Use a mathematical symbol for a marker\nplt.scatter(time2,\n            data2,\n            color='green',\n            marker='$\\Omega$',\n            s=100,\n            label='mathematical symbol')\n\n# Unicode symbol marker\nplt.scatter(time3,\n            data3,\n            color='black',\n            marker='$\\u2608$',\n            s=100,\n            label='unicode symbol')\n\n# Create custom path for marker\nverts = [(-0.5, -0.5), (-0.5, 0.5), (0, 0), (0.5, 0.5), (0.5, -0.5), (0, 0)]\npath = mpath.Path(verts)\nplt.scatter(time4, data4, color='red', marker=path, s=100, label='custom path')\n\n# Add legend\nplt.legend()\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Make your own marker\",\n                             xlabel=t['time'].long_name,\n                             ylabel=t.long_name)\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=4,\n                             labelsize=14)\n\ngvutil.set_axes_limits_and_ticks(\n    ax,\n    xlim=(xmin, xmax),\n    ylim=(220.0, 232.0),\n    xticklabels=[' ', 131160, ' ', 131170, ' ', 131180, ' ', 131190],\n    yticklabels=np.arange(220.0, 233.0, 2.0))\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
}