{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple Plot Example\n",
    "\n",
    "The simplest example of a plot in matlibplot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1c07099af88>]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14944 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "# Create a figure containing a single axes.\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "# Plot some data on the axes.\n",
    "ax.plot([1, 2, 3, 4], [1, 4, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The object-oriented interface and the pyplot interface\n",
    "======================================================\n",
    "\n",
    "As noted above, there are essentially two ways to use Matplotlib:\n",
    "\n",
    "- Explicitly create figures and axes, and call methods on them (the\n",
    "  \"object-oriented (OO) style\").\n",
    "- Rely on pyplot to automatically create and manage the figures and axes, and\n",
    "  use pyplot functions for plotting.\n",
    "\n",
    "So one can do (OO-style)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x20de93df348>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 21752 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "x = np.linspace(0, 2, 100)\n",
    "\n",
    "# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.\n",
    "fig, ax = plt.subplots()  # Create a figure and an axes.\n",
    "ax.plot(x, x, label='linear')  # Plot some data on the axes.\n",
    "ax.plot(x, x**2, label='quadratic')  # Plot more data on the axes...\n",
    "ax.plot(x, x**3, label='cubic')  # ... and some more.\n",
    "ax.set_xlabel('x label')  # Add an x-label to the axes.\n",
    "ax.set_ylabel('y label')  # Add a y-label to the axes.\n",
    "ax.set_title(\"Simple Plot\")  # Add a title to the axes.\n",
    "ax.legend()  # Add a legend."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "or (pyplot-style)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x20de94feb48>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 21752 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = np.linspace(0, 2, 100)\n",
    "\n",
    "plt.plot(x, x, label='linear')  # Plot some data on the (implicit) axes.\n",
    "plt.plot(x, x**2, label='quadratic')  # etc.\n",
    "plt.plot(x, x**3, label='cubic')\n",
    "plt.xlabel('x label')\n",
    "plt.ylabel('y label')\n",
    "plt.title(\"Simple Plot\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a figure with a set of 4 Axes using the OO Approach."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n",
       "        [0., 0., 0., 0., 0., 0., 2., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 2., 1., 6., 4., 1., 1.],\n",
       "        [0., 0., 1., 1., 3., 2., 0., 1., 2., 3.],\n",
       "        [0., 0., 2., 2., 3., 1., 4., 3., 3., 0.],\n",
       "        [0., 0., 0., 1., 1., 5., 5., 4., 2., 0.],\n",
       "        [1., 1., 1., 2., 4., 1., 4., 2., 2., 0.],\n",
       "        [0., 0., 0., 0., 0., 4., 1., 0., 2., 1.],\n",
       "        [0., 0., 0., 1., 0., 0., 2., 1., 1., 1.]]),\n",
       " array([-3.12183853, -2.60011446, -2.0783904 , -1.55666633, -1.03494227,\n",
       "        -0.5132182 ,  0.00850587,  0.53022993,  1.051954  ,  1.57367806,\n",
       "         2.09540213]),\n",
       " array([-3.13862515, -2.64143965, -2.14425415, -1.64706865, -1.14988315,\n",
       "        -0.65269764, -0.15551214,  0.34167336,  0.83885886,  1.33604436,\n",
       "         1.83322987]),\n",
       " <matplotlib.collections.QuadMesh at 0x20de9666348>)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 26868 characters]\n",
      "text/plain": [
       "<Figure size 360x360 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "np.random.seed(19680801)\n",
    "data = np.random.randn(2, 100)\n",
    "\n",
    "fig, axs = plt.subplots(2, 2, figsize=(5, 5))\n",
    "axs[0, 0].hist(data[0])\n",
    "axs[1, 0].scatter(data[0], data[1])\n",
    "axs[0, 1].plot(data[0], data[1])\n",
    "axs[1, 1].hist2d(data[0], data[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a figure with a set of 4 Axes using the pyplot Approach."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],\n",
       "        [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],\n",
       "        [0., 0., 0., 0., 0., 0., 2., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 2., 1., 6., 4., 1., 1.],\n",
       "        [0., 0., 1., 1., 3., 2., 0., 1., 2., 3.],\n",
       "        [0., 0., 2., 2., 3., 1., 4., 3., 3., 0.],\n",
       "        [0., 0., 0., 1., 1., 5., 5., 4., 2., 0.],\n",
       "        [1., 1., 1., 2., 4., 1., 4., 2., 2., 0.],\n",
       "        [0., 0., 0., 0., 0., 4., 1., 0., 2., 1.],\n",
       "        [0., 0., 0., 1., 0., 0., 2., 1., 1., 1.]]),\n",
       " array([-3.12183853, -2.60011446, -2.0783904 , -1.55666633, -1.03494227,\n",
       "        -0.5132182 ,  0.00850587,  0.53022993,  1.051954  ,  1.57367806,\n",
       "         2.09540213]),\n",
       " array([-3.13862515, -2.64143965, -2.14425415, -1.64706865, -1.14988315,\n",
       "        -0.65269764, -0.15551214,  0.34167336,  0.83885886,  1.33604436,\n",
       "         1.83322987]),\n",
       " <matplotlib.collections.QuadMesh at 0x20de9d4ee48>)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 26868 characters]\n",
      "text/plain": [
       "<Figure size 360x360 with 4 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "np.random.seed(19680801)\n",
    "data = np.random.randn(2, 100)\n",
    "\n",
    "plt.figure(figsize=(5, 5))\n",
    "plt.subplot(221)\n",
    "plt.hist(data[0])\n",
    "plt.subplot(222)\n",
    "plt.plot(data[0], data[1])\n",
    "plt.subplot(223)\n",
    "plt.scatter(data[0], data[1])\n",
    "plt.subplot(224)\n",
    "plt.hist2d(data[0], data[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
