{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_table_2.py\nThis script illustrates the following concepts:\n   - Drawing a table with headers\n   - Filling table cells with a given color\n   - Specifying the position of individual tables\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/table_2.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/table_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"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Set row headers (first column)\nrow_text = [\n    \"\", \"\", \"SLP_ERA40\", \"Tsfc_ERA40\", \"Prc_GPCP\", \"Prc 30S-30N_GPCP\", \"LW_ERS\",\n    \"SW_ERS\", \"U300_ERA40\", \"Guess_BOGUS\", \"RH_NCEP\", \"LHFLX_ERA40\",\n    \"TWP_ERA40\", \"CLDTOT_NCEP\", \"O3_NASA\", \"Q_JMA\", \"PBLH_JMA\", \"Omega_CAS\"\n]\n\n# Set colors of row headers (first column)\nrowcolors = [\n    'skyblue', 'skyblue', 'lightgray', 'lightgray', 'lightgray', 'lightgray',\n    'lightgray', 'lightgray', 'lightgray', 'lightgray', 'lightgray',\n    'lightgray', 'lightgray', 'lightgray', 'lightgray', 'lightgray',\n    'lightgray', 'lightgray'\n]\n\n# Set cell values (second and third column)\ncell_text = [[\"Case A\", \"Case B\"], [\"ANN\", \"ANN\"], [\"1.230\", \"1.129\"],\n             [\"0.988\", \"0.996\"], [\"1.092\", \"1.016\"], [\"1.172\", \"1.134\"],\n             [\"1.064\", \"1.023\"], [\"0.966\", \"0.962\"], [\"1.079\", \"1.048\"],\n             [\"0.781\", \"0.852\"], [\"1.122\", \"0.911\"], [\"1.000\", \"0.835\"],\n             [\"0.998\", \"0.712\"], [\"1.321\", \"1.122\"], [\"0.842\", \"0.956\"],\n             [\"0.978\", \"0.832\"], [\"0.998\", \"0.900\"], [\"0.811\", \"1.311\"]]\n\n# Set colors of cells (second and third columns)\ncolors = [['lightgray', 'lightgray'], ['lightgray', 'lightgray'],\n          [\"White\", \"palegreen\"], [\"White\", \"hotpink\"], [\"White\", \"palegreen\"],\n          [\"White\", \"palegreen\"], [\"White\",\n                                   \"palegreen\"], [\"White\", \"palegreen\"],\n          [\"White\", \"palegreen\"], [\"White\", \"hotpink\"], [\"White\", \"palegreen\"],\n          [\"White\", \"palegreen\"], [\"White\", \"palegreen\"],\n          [\"White\", \"palegreen\"], [\"White\", \"hotpink\"], [\"White\", \"palegreen\"],\n          [\"White\", \"palegreen\"], [\"White\", \"hotpink\"]]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Create figure and axis\nfig, ax = plt.subplots()\n\n# Set axis aspect ratio\nax.set_aspect(2)\n\n# Make axis spines invisible\nfig.patch.set_visible(False)\nax.axis('off')\n\n# Plot first table\ntable = ax.table(rowLabels=row_text,\n                 rowColours=rowcolors,\n                 rowLoc='center',\n                 cellText=cell_text,\n                 cellColours=colors,\n                 cellLoc='center',\n                 loc='center')\n\n# Plot single-cell table in upper left of first table\nplt.table(cellText=[['CAM METRICS']],\n          cellColours=[['skyblue']],\n          cellLoc='center',\n          bbox=[-.694, .815, 0.694, 0.091])\n\n# Give plot a tight layout\nfig.tight_layout()\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
}