{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# The Basics of Testing Plots\n\nThese are some examples of using the basic functionality of MatPlotCheck.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Setup\nYou will start by importing the required packages. You will be using\n``matplotlib.pyplot`` to create our plots, but any ``matplotlib`` based\nplotter (such as ``pandas.DataFrame.plot``) can be used.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport matplotcheck.base as mpc\nimport matplotcheck.notebook as nb\nimport pandas as pd"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Plot\nNow you will create some data and plot it.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "months = [\n    \"Jan\",\n    \"Feb\",\n    \"Mar\",\n    \"Apr\",\n    \"May\",\n    \"Jun\",\n    \"Jul\",\n    \"Aug\",\n    \"Sep\",\n    \"Oct\",\n    \"Nov\",\n    \"Dec\",\n]\npercip = [\n    0.75,\n    0.83,\n    2.2,\n    2.87,\n    2.80,\n    2.20,\n    1.77,\n    1.85,\n    1.69,\n    1.54,\n    1.22,\n    0.94,\n]\n\nfig, ax = plt.subplots()\nax.bar(months, percip, color=\"blue\")\nax.set(\n    title=\"Average Monthly Precipitation in Boulder, CO\",\n    xlabel=\"Month\",\n    ylabel=\"Percipitation (in)\",\n)\n\nplot_1_hold = nb.convert_axes(plt, which_axes=\"current\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>To use matplotcheck to test a plot, you need a copy of the\n   ``Matplotlib.axis.Axis`` object that the plot is stored in. This can be\n   prove difficult when testing plots in a Jupyter Notebook, where a\n   ``Matplotlib.axis.Axis`` object will not persisit beyond the cell it was\n   created in. In this case, ``nb.convert_axes()`` is used to hold the most\n   recently created plot in the variable ``plot_1_hold``. There are other ways\n   to do this, but in a Jupyter Notebook you *must* use ``nb.convert_axes()``\n   to save the ``Axis`` object.</p></div>\n\n## Testing the plot\nNow you can use matplotcheck to check that the plot has certain attributes\nthat you expect. You need to create a ``PlotTester`` object, and then you can\nrun some tests.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plot_tester_1 = mpc.PlotTester(plot_1_hold)\n\n\n# Test that the plot is a bar plot\nplot_tester_1.assert_plot_type(\"bar\")\n\n# Test that the plot title contains specific words\nplot_tester_1.assert_title_contains([\"average\", \"monthly precip\", \"boulder\"])\n\n# Test that the axis labels contain specific words\nplot_tester_1.assert_axis_label_contains(axis=\"x\", strings_expected=[\"month\"])\nplot_tester_1.assert_axis_label_contains(\n    axis=\"y\", strings_expected=[\"percip\", \"in\"]\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now you can create a DataFrame to store the data that you expect to see in our\nplot. Then you test wether that data exists in the plot.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "expected_x_data = [\n    \"Jan\",\n    \"Feb\",\n    \"Mar\",\n    \"Apr\",\n    \"May\",\n    \"Jun\",\n    \"Jul\",\n    \"Aug\",\n    \"Sep\",\n    \"Oct\",\n    \"Nov\",\n    \"Dec\",\n]\nexpected_y_data = [\n    0.75,\n    0.83,\n    2.20,\n    2.87,\n    2.80,\n    2.20,\n    1.77,\n    1.85,\n    1.69,\n    1.54,\n    1.22,\n    0.94,\n]\nexpected_data = pd.DataFrame(\n    {\"Months\": expected_x_data, \"Percip\": expected_y_data}\n)\n\nplot_tester_1.assert_xydata(\n    expected_data, xcol=\"Months\", ycol=\"Percip\", xlabels=True\n)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Since no errors have been raised by the above assertions, you know that all\nthose tests passed. Now you can run some tests that will fail.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Test that the plot is a scatter plot\ntry:\n    plot_tester_1.assert_plot_type(\"scatter\")\nexcept AssertionError as message:\n    print(\"AssertionError:\", message)\n\n# Test that the title contains specific strings\ntry:\n    plot_tester_1.assert_title_contains([\"Denver\", \"wind speed\"])\nexcept AssertionError as message:\n    print(\"AssertionError:\", message)\n\n# Test that the axis labels contain specific strings\ntry:\n    plot_tester_1.assert_axis_label_contains(\n        axis=\"x\", strings_expected=[\"year\"]\n    )\nexcept AssertionError as message:\n    print(\"AssertionError:\", message)\ntry:\n    plot_tester_1.assert_axis_label_contains(\n        axis=\"y\", strings_expected=[\"wind speed\"]\n    )\nexcept AssertionError as message:\n    print(\"AssertionError:\", message)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "In some cases, you may not want the error message to display exactly what\nwords you are expecting. If you'd like to set your own error message for an\nassertion, you can use the ``message`` flag. For more details, see the\ndocumentation for :py:func:`base.assert_plot_type`.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "try:\n    plot_tester_1.assert_plot_type(\n        plot_type=\"line\", message=\"Make sure you have the correct plot type!\"\n    )\nexcept AssertionError as message:\n    print(\"AssertionError:\", message)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>Some assert functions use a slightly different flag to set a custom error\n   message, and some have special functionality. See the documantation for\n   details.</p></div>\n\n"
      ]
    }
  ],
  "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
}