{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>16. Introduction to Matplotlib</h1>\n",
    "<h2>11/15/2023</h2>\n",
    "\n",
    "<h2>16.0 Last Time...</h2>\n",
    "<ul>\n",
    "    <li>A masked array has a <b>mask</b> attribute that allows us to identify suspicious or unwanted data.</li>\n",
    "    <li>We can use direct assignment, assignment by condition, and filling to create a masked array.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>16.1 What is Matplotlib?</h2>\n",
    "\n",
    "There's another object-oriented programming language out there called Matlab that handles plotting especially well. Unfortunately, Matlab costs a lot of money, so Python's overtaken it in popularity for good reason!\n",
    "\n",
    "And there's no reason we can't make use of Matlab's helpful plotting interface in this lovely open-source language...\n",
    "\n",
    "The submodule we want is called <b>matplotlib.pyplot</b>. It's fast and efficient, and the plots look great! You'll see a lot of Matlab-style plots applied pretty much right out of the box in published scientific articles."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, 'y = 1/2x')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 26156 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Let's start by creating a straight line.\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.plot([1,2,3,4])\n",
    "plt.ylabel('ints')\n",
    "plt.xlabel(\"indeces\")\n",
    "plt.title('y = 1/2x')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay, so far so good! But why does the y-axis go from 1-4 when the x-axis goes from 0-3? If you just provide a single list or array to the plot() command (rather than giving it both the x and the y values), it assumes you're dealing with a sequence of y values and automatically generates x values for you. And, of course, Python defaults to counting starting at 0, so it's created a default x vector that has the same length as y but starts at 0: [0,1,2,3].\n",
    "\n",
    "If you're not working with evenly spaced data, you can provide both x and y values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22832 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot([1,2,3,4],[1,4,9,16])\n",
    "plt.xlabel('yea')\n",
    "plt.ylabel('no')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you wanted to plot x squared with more data, you can specify that as well..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1111ea8d0>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 20776 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "x = np.arange(1,4,0.001)\n",
    "plt.plot(x,x**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>16.2 Formatting your Plot</h2>\n",
    "\n",
    "Every time there's an x,y pair of arguments, there's an optional third argument that is called the <b>format string</b> that will indicate the color and line type.\n",
    "\n",
    "Here's the list! You can also specify things outside this list, as we'll see, but these are the quick ones:\n",
    "\n",
    "<b>Line Style Specifiers</b>\n",
    "<table style=\"width:100%\">\n",
    "    <tr>\n",
    "        <th>Specifier</th>\n",
    "        <th>Line Style</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'-'</td>\n",
    "        <td>Solid line (default)</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'--'</td>\n",
    "        <td>Dashed line</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>':'</td>\n",
    "        <td>Dotted line</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'-.'</td>\n",
    "        <td>Dash-dot line</td>\n",
    "    </tr>\n",
    "</table>\n",
    "\n",
    "<b>Marker Specifiers</b>\n",
    "<table style=\"width:100%\">\n",
    "    <tr>\n",
    "        <th>Specifier</th>\n",
    "        <th>Marker Type</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'+'</td>\n",
    "        <td>Plus sign</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'o'</td>\n",
    "        <td>Circle</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'*'</td>\n",
    "        <td>Asterisk</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'.'</td>\n",
    "        <td>Point</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'x'</td>\n",
    "        <td>Cross</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'square' or 's'</td>\n",
    "        <td>Square</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'diamond' or 'd'</td>\n",
    "        <td>Diamond</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'^'</td>\n",
    "        <td>Upward-pointing triangle</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'v'</td>\n",
    "        <td>Downward-pointing triangle</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'&gt'</td>\n",
    "        <td>Right-pointing triangle</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'&lt'</td>\n",
    "        <td>Left-pointing triangle</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'pentagram' or 'p'</td>\n",
    "        <td>Five-pointed star (pentagram)</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>'hexagram' or 'h'</td>\n",
    "        <td>Six-pointed star (hexagram)</td>\n",
    "    </tr>\n",
    "</table>\n",
    "\n",
    "<b>Color Specifiers</b>\n",
    "<table style=\"width:100%\">\n",
    "    <tr>\n",
    "        <th>Specifier</th>\n",
    "        <th>Color</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>r</td>\n",
    "        <td>Red</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>g</td>\n",
    "        <td>Green</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>b</td>\n",
    "        <td>Blue</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>c</td>\n",
    "        <td>Cyan</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>m</td>\n",
    "        <td>Magenta</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>y</td>\n",
    "        <td>Yellow</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>k</td>\n",
    "        <td>Black</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>w</td>\n",
    "        <td>White</td>\n",
    "    </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1111f4090>]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 17080 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# How about plotting some red circles separated by dotted lines?\n",
    "plt.plot([1,2,3,4],[1,4,9,16],\"ro:\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also specify the axes that we want to see using the <b>matplotlib.pyplot.axis()</b> module, which takes as input a list of [xmin, xmax, ymin, ymax]."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14716 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot([1,2,3,4],[1,4,9,16],\"g-.^\")\n",
    "plt.axis([0,10,0,30])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also plot using arrays! We're not just limited to lists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = np.arange(0,5,0.2)\n",
    "\n",
    "# Create an array that counts from 0 to 5 at 0.2 intervals.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>1. Create a plot where the x values are given by t and the y values are given by t. Use red circles separated by dashes.</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x1105bcf90>]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15384 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(t,t,\"r--o\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>2. Create a plot where the x values are given by t and the y values are given by t squared. Use blue squares separated by dots.</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x14aa2b990>]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 13956 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(t,t**2,\"bs:\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<b>3. Create a plot where the x values are given by t and the y values are given by t cubed. Use green upward-pointing triangles.</b>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x14ab5b990>]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 12912 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.plot(t,t**3,\"g^\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>16.3 Take-Home Points</h2>\n",
    "<ul>\n",
    "    <li><b>matplotlib</b>'s <b>pyplot</b> module lets us use Matlab's powerful plotting tools in Python.</li>\n",
    "    <li>The <b>matplotlib.pyplot.plot()</b> function is a simple way to plot 2-D data.</li>\n",
    "    <li>We can specify axis limits as well as line style and color.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
