{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#MatPlotLib-Basics\" data-toc-modified-id=\"MatPlotLib-Basics-1\">MatPlotLib Basics</a></span><ul class=\"toc-item\"><li><span><a href=\"#Draw-a-line-graph\" data-toc-modified-id=\"Draw-a-line-graph-1.1\">Draw a line graph</a></span></li><li><span><a href=\"#Mutiple-Plots-on-One-Graph\" data-toc-modified-id=\"Mutiple-Plots-on-One-Graph-1.2\">Mutiple Plots on One Graph</a></span></li><li><span><a href=\"#Save-it-to-a-File\" data-toc-modified-id=\"Save-it-to-a-File-1.3\">Save it to a File</a></span></li><li><span><a href=\"#Adjust-the-Axes\" data-toc-modified-id=\"Adjust-the-Axes-1.4\">Adjust the Axes</a></span></li><li><span><a href=\"#Add-a-Grid\" data-toc-modified-id=\"Add-a-Grid-1.5\">Add a Grid</a></span></li><li><span><a href=\"#Change-Line-Types-and-Colors\" data-toc-modified-id=\"Change-Line-Types-and-Colors-1.6\">Change Line Types and Colors</a></span></li><li><span><a href=\"#Labeling-Axes-and-Adding-a-Legend\" data-toc-modified-id=\"Labeling-Axes-and-Adding-a-Legend-1.7\">Labeling Axes and Adding a Legend</a></span></li><li><span><a href=\"#XKCD-Style-:)\" data-toc-modified-id=\"XKCD-Style-:)-1.8\">XKCD Style :)</a></span></li><li><span><a href=\"#Pie-Chart\" data-toc-modified-id=\"Pie-Chart-1.9\">Pie Chart</a></span></li><li><span><a href=\"#Bar-Chart\" data-toc-modified-id=\"Bar-Chart-1.10\">Bar Chart</a></span></li><li><span><a href=\"#Scatter-Plot\" data-toc-modified-id=\"Scatter-Plot-1.11\">Scatter Plot</a></span></li><li><span><a href=\"#Histogram\" data-toc-modified-id=\"Histogram-1.12\">Histogram</a></span></li><li><span><a href=\"#Box-&amp;-Whisker-Plot\" data-toc-modified-id=\"Box-&amp;-Whisker-Plot-1.13\">Box &amp; Whisker Plot</a></span></li><li><span><a href=\"#Activity\" data-toc-modified-id=\"Activity-1.14\">Activity</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MatPlotLib Basics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Draw a line graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:46.835038Z",
     "start_time": "2021-08-12T08:15:46.257835Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15872 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from scipy.stats import norm\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "x = np.arange(-3, 3, 0.01)\n",
    "\n",
    "plt.plot(x, norm.pdf(x))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mutiple Plots on One Graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:46.918143Z",
     "start_time": "2021-08-12T08:15:46.836435Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18372 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(x, norm.pdf(x))\n",
    "plt.plot(x, norm.pdf(x, 1.0, 0.5))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save it to a File"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.154275Z",
     "start_time": "2021-08-12T08:15:46.919500Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18372 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(x, norm.pdf(x))\n",
    "plt.plot(x, norm.pdf(x, 1.0, 0.5))\n",
    "plt.savefig('MyPlot.png', format='png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adjust the Axes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.297505Z",
     "start_time": "2021-08-12T08:15:47.155499Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16168 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "axes = plt.axes()\n",
    "axes.set_xlim([-5, 5])\n",
    "axes.set_ylim([0, 1.0])\n",
    "axes.set_xticks([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])\n",
    "axes.set_yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])\n",
    "plt.plot(x, norm.pdf(x))\n",
    "plt.plot(x, norm.pdf(x, 1.0, 0.5))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Add a Grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.396985Z",
     "start_time": "2021-08-12T08:15:47.298600Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17360 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "axes = plt.axes()\n",
    "axes.set_xlim([-5, 5])\n",
    "axes.set_ylim([0, 1.0])\n",
    "axes.set_xticks([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])\n",
    "axes.set_yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])\n",
    "axes.grid()\n",
    "plt.plot(x, norm.pdf(x))\n",
    "plt.plot(x, norm.pdf(x, 1.0, 0.5))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change Line Types and Colors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.494772Z",
     "start_time": "2021-08-12T08:15:47.397964Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16004 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "axes = plt.axes()\n",
    "axes.set_xlim([-5, 5])\n",
    "axes.set_ylim([0, 1.0])\n",
    "axes.set_xticks([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])\n",
    "axes.set_yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])\n",
    "axes.grid()\n",
    "plt.plot(x, norm.pdf(x), 'b-')\n",
    "plt.plot(x, norm.pdf(x, 1.0, 0.5), 'r:')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Labeling Axes and Adding a Legend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.604652Z",
     "start_time": "2021-08-12T08:15:47.495703Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20308 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "axes = plt.axes()\n",
    "axes.set_xlim([-5, 5])\n",
    "axes.set_ylim([0, 1.0])\n",
    "axes.set_xticks([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])\n",
    "axes.set_yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])\n",
    "axes.grid()\n",
    "plt.xlabel('Greebles')\n",
    "plt.ylabel('Probability')\n",
    "plt.plot(x, norm.pdf(x), 'b-')\n",
    "plt.plot(x, norm.pdf(x, 1.0, 0.5), 'r:')\n",
    "plt.legend(['Sneetches', 'Gacks'], loc=4)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## XKCD Style :)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.696515Z",
     "start_time": "2021-08-12T08:15:47.606350Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'my overall health')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "findfont: Font family ['xkcd', 'xkcd Script', 'Humor Sans', 'Comic Neue', 'Comic Sans MS'] not found. Falling back to DejaVu Sans.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 27376 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.xkcd()\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(1, 1, 1)\n",
    "ax.spines['right'].set_color('none')\n",
    "ax.spines['top'].set_color('none')\n",
    "plt.xticks([])\n",
    "plt.yticks([])\n",
    "ax.set_ylim([-30, 10])\n",
    "\n",
    "data = np.ones(100)\n",
    "data[70:] -= np.arange(30)\n",
    "\n",
    "plt.annotate(\n",
    "    'THE DAY I REALIZED\\nI COULD COOK BACON\\nWHENEVER I WANTED',\n",
    "    xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10))\n",
    "\n",
    "plt.plot(data)\n",
    "\n",
    "plt.xlabel('time')\n",
    "plt.ylabel('my overall health')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pie Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.761553Z",
     "start_time": "2021-08-12T08:15:47.697776Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29904 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Remove XKCD mode:\n",
    "plt.rcdefaults()\n",
    "\n",
    "values = [12, 55, 4, 32, 14]\n",
    "colors = ['r', 'g', 'b', 'c', 'm']\n",
    "explode = [0, 0, 0.2, 0, 0]\n",
    "labels = ['India', 'United States', 'Russia', 'China', 'Europe']\n",
    "plt.pie(values, colors= colors, labels=labels, explode = explode)\n",
    "plt.title('Student Locations')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bar Chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.853378Z",
     "start_time": "2021-08-12T08:15:47.762539Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 8648 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "values = [12, 55, 4, 32, 14]\n",
    "colors = ['r', 'g', 'b', 'c', 'm']\n",
    "plt.bar(range(0,5), values, color= colors)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scatter Plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:47.944110Z",
     "start_time": "2021-08-12T08:15:47.854424Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 38008 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pylab import randn\n",
    "\n",
    "X = randn(500)\n",
    "Y = randn(500)\n",
    "plt.scatter(X,Y)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Histogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:48.069828Z",
     "start_time": "2021-08-12T08:15:47.945067Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 13684 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "incomes = np.random.normal(27000, 15000, 10000)\n",
    "plt.hist(incomes, 50)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Box & Whisker Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Useful for visualizing the spread & skew of data.\n",
    "\n",
    "The red line represents the median of the data, and the box represents the bounds of the 1st and 3rd quartiles.\n",
    "\n",
    "So, half of the data exists within the box.\n",
    "\n",
    "The dotted-line \"whiskers\" indicate the range of the data - except for outliers, which are plotted outside the whiskers. Outliers are 1.5X or more the interquartile range.\n",
    "\n",
    "This example below creates uniformly distributed random numbers between -40 and 60, plus a few outliers above 100 and below -100:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-12T08:15:48.140378Z",
     "start_time": "2021-08-12T08:15:48.070981Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 12316 characters]\n",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "uniformSkewed = np.random.rand(100) * 100 - 40\n",
    "high_outliers = np.random.rand(10) * 50 + 100\n",
    "low_outliers = np.random.rand(10) * -50 - 100\n",
    "data = np.concatenate((uniformSkewed, high_outliers, low_outliers))\n",
    "plt.boxplot(data)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Activity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try creating a scatter plot representing random data on age vs. time spent watching TV. Label the axes."
   ]
  },
  {
   "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.8.11"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
