{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_tm_2.py\nTickmark and Axis Manipulation example\n\nNote: This script is aimed at demonstrating the explicit handling of tick marks, their locations, labels, etc.;\ntherefore, the use of geocat-viz convenience functions is minimized here to show such tick management functions directly\nthroughout this script.\n\nThis script illustrates the following concepts:\n   - Explicitly setting tickmarks and labels on the bottom X axis\n   - Setting the spacing for tickmarks\n   - Setting the mininum/maximum value of the Y axis in an XY plot\n   - Changing the width and height of a plot\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/tm_2.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/tm_1_1_lg.png and https://www.ncl.ucar.edu/Applications/Images/tm_1_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 matplotlib.pyplot as plt\n\nfrom matplotlib.ticker import MultipleLocator, FormatStrFormatter\n\nfrom geocat.viz import util as gvutil"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Note that range() top value is not included in the returned array of values.\n\nx_data = np.arange(1950, 2006)\ny_data = np.random.uniform(-4, 4, 56)\n\n# Print out a formatted message; note the starting 'f' for the string.\nprint(\n    f\"There are { len(x_data) } values in x_data, and { len(y_data) } values in y_data.\"\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate figure and set its size (width, height) in inches.\nplt.figure(1, figsize=(8, 6))\n\n# Make a subplot with major ticks that are multiples of 5.\n\n# Create a subplot grid with two rows and one column (stacked subplots), and\n# set the current plot context to the top subplot.\nax1 = plt.subplot(2, 1, 1)\n\n# Format the tick labels. Use integers for the major ticks.\n# For the minor ticks, use no labels; defaults to NullFormatter.\nax1.xaxis.set_major_formatter(FormatStrFormatter('%d'))\nax1.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))\n\n# Set the major tick spacing.\nmajor_tick_spacing = 5\nax1.xaxis.set_major_locator(MultipleLocator(major_tick_spacing))\nspacingString = f'Tick Spacing = {major_tick_spacing}'\n\n# Draw ticks on all sides of the plot.\nplt.tick_params(which='both', top=True, right=True)\n\n# Increase the length of the tick marks.\nplt.tick_params(which='major', length=10.0, width=0.5)\nplt.tick_params(which='minor', length=5.0, width=0.25)\n\n# Set the minor tick spacing for X and Y axes.\nax1.xaxis.set_minor_locator(MultipleLocator(1.25))\nax1.yaxis.set_minor_locator(MultipleLocator(0.5))\n\n# Add a descriptive string to the top left corner of the plot.\nax1.text(0.01, 1.1, spacingString, transform=ax1.transAxes, fontWeight='bold')\n\n# Plot data and set the X axis limits.\nplt.plot(x_data, y_data, color='black', linewidth=0.5)\n\n# Usa geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions\n# Set axes limits\ngvutil.set_axes_limits_and_ticks(ax1, xlim=(1949, 2006), ylim=(-4.2, 4.2))\n\n# Make a subplot with major ticks that are set to explicit values and minor ticks that are multiples of 1.\n\n# Set the current plot context to the bottom subplot.\nax2 = plt.subplot(2, 1, 2)\n\n# Format the tick labels.\n# For the minor ticks, use no labels; defaults to NullFormatter.\nax2.xaxis.set_major_formatter(FormatStrFormatter('%d'))\nax2.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))\n\n# Draw ticks on all sides of the plot.\nplt.tick_params(which='both', top=True, right=True)\n\n# Increase the length of the tick marks.\nplt.tick_params(which='major', length=10.0, width=0.5)\nplt.tick_params(which='minor', length=5.0, width=0.25)\n\n# Set the minor tick spacing for X and Y axes.\nax2.xaxis.set_minor_locator(MultipleLocator(1))\nax2.yaxis.set_minor_locator(MultipleLocator(0.5))\n\n# Add a descriptive text to the top left corner of the axes.\nax2.text(0.01,\n         1.1,\n         \"Ticks Set Explicitly\",\n         transform=ax2.transAxes,\n         fontWeight='bold')\n\n# Line-plot data\nplt.plot(x_data, y_data, color='black', linewidth=0.5)\n\n# Use geocat.viz.util convenience function to set axes parameters without calling several matplotlib functions\n# Set axes limits, and tick values on x-axes.\ngvutil.set_axes_limits_and_ticks(\n    ax2,\n    xlim=(1949, 2006),\n    ylim=(-4.2, 4.2),\n    xticks=[1950, 1960, 1970, 1980, 1990, 2000, 2005])\n\n# Create more space between subplots\nplt.subplots_adjust(hspace=0.4)\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
}