{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "D-zPDF33DONE"
   },
   "source": [
    "# Plotting with Pandas and Matplotlib\n",
    "\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "I4ag5MBwDONH"
   },
   "source": [
    "### Learning Objectives\n",
    "*After this lesson, you will be able to:*\n",
    "- Implement different types of plots on a given dataset.\n",
    "\n",
    "<!--\n",
    "Talking points:\n",
    "We will use Pandas and Matplotlib to create simple plots.\n",
    "We'll cover plotting line plots, scatter plots, bar plots, and histograms, and a brief introduction on how to manipulate the style of your plots with Matplotlib.\n",
    "-->\n",
    "\n",
    "---------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "qDihbOCjDONK"
   },
   "source": [
    "## Recap\n",
    "\n",
    "In the last lesson, we learned about when to use the different types of plots. Can anyone give an example of when we would use a:\n",
    " * line plot?\n",
    " * bar plot?\n",
    " * histogram?\n",
    " * scatter plot?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "yjT2l0BuDONM"
   },
   "source": [
    "### Pandas and Matplotlib\n",
    "\n",
    "<!--\n",
    "Talking points:\n",
    "In this lesson, we will introduce how plotting works in Pandas and Matplotlib. It is important to know that Pandas uses Matplotlib behind the scenes to make plots. So, you will notice that Pandas plotting methods often use similar parameter names as Matplotlib methods. Further, you can use Matplotlib functions in combination with Pandas methods to alter the plots after drawing them. For example, you can use Matplotlib's `xlabel` and `title` functions to label the plot's x-axis and title, respectively, after it is drawn.\n",
    "-->\n",
    "\n",
    "As we explore different types of plots, notice:\n",
    "\n",
    "1. Different types of plots are drawn very similarly -- they even tend to share parameter names.\n",
    "2. In Pandas, calling `plot()` on a `DataFrame` is different than calling it on a `Series`. Although the methods are both named `plot`, they may take different parameters."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "2HqUvXmYDONP"
   },
   "source": [
    "*Sometimes Pandas can be a little frustrating... perserverence is key!*\n",
    "\n",
    "<img src=\"https://media.giphy.com/media/EPcvhM28ER9XW/giphy.gif\" width=\"200\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "SN6Z5XUNDONT"
   },
   "source": [
    "## Lesson Guide\n",
    "\n",
    "- [Line Plots](#line-plots)\n",
    "- [Bar Plots](#bar-plots)\n",
    "- [Histograms](#histograms)\n",
    "- [Scatter Plots](#scatter-plots)\n",
    "- [Using Seaborn](#using-seaborn)\n",
    "- [OPTIONAL: Understanding Matplotlib (Figures, Subplots, and Axes)](#matplotlib)\n",
    "- [OPTIONAL: Additional Topics](#additional-topics)\n",
    "\n",
    "- [Summary](#summary)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "bqte2ZN4DONV"
   },
   "source": [
    "## Plotting with Pandas: How?\n",
    "\n",
    "`<data_set>.<columns>.plot()`\n",
    "\n",
    "**Note**: These are example plots on a ficticious dataset. We'll work with real ones in just a minute!\n",
    "\n",
    "`population['states'].value_counts().plot()` creates:\n",
    "\n",
    "![](https://exceljet.net/sites/default/files/styles/original_with_watermark/public/images/charttypes/line%20chart2.png?itok=lG1hqRu4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "WNoBS5juDONW"
   },
   "source": [
    "## Plotting: Visualization Types\n",
    "\n",
    "Line charts are default.\n",
    "\n",
    "`# line chart`\n",
    "\n",
    "`population['states'].value_counts().plot()`\n",
    "\n",
    "For other charts:\n",
    "\n",
    "`population['states'].plot(kind='bar')`\n",
    "\n",
    "`population['states'].plot(kind='hist', bins=3);`\n",
    "\n",
    "`population['states'].plot(kind='scatter', x='states', y='population')`\n",
    "\n",
    "Let's try!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "MxzMTLp3DONY"
   },
   "source": [
    "### Import packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "0CkXJSdxDONa"
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "# set the plots to display in the Jupyter notebook\n",
    "%matplotlib inline\n",
    "\n",
    "# change plotting colors per client request\n",
    "plt.style.use('fivethirtyeight')\n",
    "\n",
    "# Increase default figure and font sizes for easier viewing.\n",
    "plt.rcParams['figure.figsize'] = (8, 6)\n",
    "plt.rcParams['font.size'] = 14"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "h0ZGCLuEDONf"
   },
   "source": [
    "### Load in data sets for visualization\n",
    "\n",
    "- [Football Records](https://www.kaggle.com/martj42/international-football-results-from-1872-to-2017): International football results from 1872 to 2018\n",
    "- [Avocado Prices](https://www.kaggle.com/neuromusic/avocado-prices): Historical data on avocado prices and sales volume in multiple US markets\n",
    "- [Chocolate Bar Ratings](https://www.kaggle.com/rtatman/chocolate-bar-ratings): Expert ratings of over 1,700 chocolate bars\n",
    "\n",
    "These have been included in `../datasets` of this repo for your convenience."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "foot = pd.read_csv('../data/international_football_results.csv')\n",
    "avo = pd.read_csv('../data/avocado.csv')\n",
    "choc = pd.read_csv('../data/chocolate_ratings.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "CrFBAr-yDONk"
   },
   "source": [
    "<a id=\"line-plots\"></a>\n",
    "## Line plots: Show the trend of a numerical variable over time\n",
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's focus on the football scores for starters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>home_team</th>\n",
       "      <th>away_team</th>\n",
       "      <th>home_score</th>\n",
       "      <th>away_score</th>\n",
       "      <th>tournament</th>\n",
       "      <th>city</th>\n",
       "      <th>country</th>\n",
       "      <th>neutral</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1872-11-30</td>\n",
       "      <td>Scotland</td>\n",
       "      <td>England</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>Friendly</td>\n",
       "      <td>Glasgow</td>\n",
       "      <td>Scotland</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1873-03-08</td>\n",
       "      <td>England</td>\n",
       "      <td>Scotland</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>Friendly</td>\n",
       "      <td>London</td>\n",
       "      <td>England</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1874-03-07</td>\n",
       "      <td>Scotland</td>\n",
       "      <td>England</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>Friendly</td>\n",
       "      <td>Glasgow</td>\n",
       "      <td>Scotland</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date home_team away_team  home_score  away_score tournament     city  \\\n",
       "0  1872-11-30  Scotland   England           0           0   Friendly  Glasgow   \n",
       "1  1873-03-08   England  Scotland           4           2   Friendly   London   \n",
       "2  1874-03-07  Scotland   England           2           1   Friendly  Glasgow   \n",
       "\n",
       "    country  neutral  \n",
       "0  Scotland    False  \n",
       "1   England    False  \n",
       "2  Scotland    False  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foot.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can extract the year by converting the date to a `datetime64[ns]` object, and then using the `pd.Series.dt.year` property to return the year (as an `int`). We'll learn more about this in future lessons."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "foot['year'] = pd.to_datetime(foot['date']).dt.year"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1872-11-30</td>\n",
       "      <td>1872</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1873-03-08</td>\n",
       "      <td>1873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1874-03-07</td>\n",
       "      <td>1874</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         date  year\n",
       "0  1872-11-30  1872\n",
       "1  1873-03-08  1873\n",
       "2  1874-03-07  1874"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foot[['date', 'year']].head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then get the number of games played every year by using `pd.Series.value_counts`, and using the `sort_index()` method to ensure our year is sorted chronologically."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1872    1\n",
       "1873    1\n",
       "1874    1\n",
       "1875    1\n",
       "1876    2\n",
       "Name: year, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foot['year'].value_counts().sort_index().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using this date, we can use the `pd.Series.plot()` method to graph **count of games** against **year of game**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "SQRNn8NbDONs",
    "outputId": "b042fdea-b151-4287-9525-0038cb9dcfbc"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32680 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "foot['year'].value_counts().sort_index().plot();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "YMx-ggN0DONx"
   },
   "source": [
    "### Knowledge Check <img src=\"http://cdn.shopify.com/s/files/1/1061/1924/products/Up_Pointing_Hand_Emoji_grande.png?v=1480481048\" width=\"30px\" align=\"left\">\n",
    "\n",
    "Why does it make sense to use a line plot for this visualization? \n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "C54zib1EDONy"
   },
   "source": [
    "### Another example\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "rGlS563hDONz",
    "outputId": "90f67495-1fe1-4a3f-930b-8281a43192fe"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Brazil       552\n",
       "Argentina    535\n",
       "Germany      495\n",
       "Mexico       494\n",
       "England      483\n",
       "Name: home_team, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foot['home_team'].sort_index().value_counts().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "FQHr3oqpDON3"
   },
   "source": [
    "### Knowledge Check <img src=\"http://cdn.shopify.com/s/files/1/1061/1924/products/Up_Pointing_Hand_Emoji_grande.png?v=1480481048\" width=\"30px\" align=\"left\">\n",
    "\n",
    "Why would it **NOT** make sense to use a line plot for this visualization?\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "JaFxqHmuDON7"
   },
   "source": [
    "<a id=\"bar-plots\"></a>\n",
    "## Bar Plots: Show a numerical comparison across different categories\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "aib7n5nMDON8"
   },
   "source": [
    "Count the number of games played in each country in the football dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "0NKT6cViDON8",
    "outputId": "20117947-0dda-4db3-8bd5-1510caa7a005"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "USA         1087\n",
       "France       775\n",
       "England      659\n",
       "Malaysia     634\n",
       "Sweden       632\n",
       "Name: country, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "foot['country'].value_counts().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's view the same information, but in a bar chart instead. Note we are using `.head()` to return the top 5. Also note that `value_counts()` automatically sorts by the value (read the docs!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "-um6OqVnDOOA",
    "outputId": "4726c40f-b1a8-4bc4-ad4a-d24e6c63e1a0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='away_team'>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 22976 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "foot.head(20).groupby(['away_team', 'home_team']).size().unstack().plot(kind='bar', stacked=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "KdwuxIbXDOOE"
   },
   "source": [
    "<a id=\"histograms\"></a>\n",
    "## Histograms: Show the distribution of a numerical variable\n",
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's change to the chocolate bar dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Company \\n(Maker-if known)</th>\n",
       "      <th>Specific Bean Origin\\nor Bar Name</th>\n",
       "      <th>REF</th>\n",
       "      <th>Review\\nDate</th>\n",
       "      <th>Cocoa\\nPercent</th>\n",
       "      <th>Company\\nLocation</th>\n",
       "      <th>Rating</th>\n",
       "      <th>Bean\\nType</th>\n",
       "      <th>Broad Bean\\nOrigin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Agua Grande</td>\n",
       "      <td>1876</td>\n",
       "      <td>2016</td>\n",
       "      <td>63%</td>\n",
       "      <td>France</td>\n",
       "      <td>3.75</td>\n",
       "      <td></td>\n",
       "      <td>Sao Tome</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Kpime</td>\n",
       "      <td>1676</td>\n",
       "      <td>2015</td>\n",
       "      <td>70%</td>\n",
       "      <td>France</td>\n",
       "      <td>2.75</td>\n",
       "      <td></td>\n",
       "      <td>Togo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Atsane</td>\n",
       "      <td>1676</td>\n",
       "      <td>2015</td>\n",
       "      <td>70%</td>\n",
       "      <td>France</td>\n",
       "      <td>3.00</td>\n",
       "      <td></td>\n",
       "      <td>Togo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Akata</td>\n",
       "      <td>1680</td>\n",
       "      <td>2015</td>\n",
       "      <td>70%</td>\n",
       "      <td>France</td>\n",
       "      <td>3.50</td>\n",
       "      <td></td>\n",
       "      <td>Togo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Quilla</td>\n",
       "      <td>1704</td>\n",
       "      <td>2015</td>\n",
       "      <td>70%</td>\n",
       "      <td>France</td>\n",
       "      <td>3.50</td>\n",
       "      <td></td>\n",
       "      <td>Peru</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Company \\n(Maker-if known) Specific Bean Origin\\nor Bar Name   REF  \\\n",
       "0                   A. Morin                       Agua Grande  1876   \n",
       "1                   A. Morin                             Kpime  1676   \n",
       "2                   A. Morin                            Atsane  1676   \n",
       "3                   A. Morin                             Akata  1680   \n",
       "4                   A. Morin                            Quilla  1704   \n",
       "\n",
       "   Review\\nDate Cocoa\\nPercent Company\\nLocation  Rating Bean\\nType  \\\n",
       "0          2016            63%            France    3.75              \n",
       "1          2015            70%            France    2.75              \n",
       "2          2015            70%            France    3.00              \n",
       "3          2015            70%            France    3.50              \n",
       "4          2015            70%            France    3.50              \n",
       "\n",
       "  Broad Bean\\nOrigin  \n",
       "0           Sao Tome  \n",
       "1               Togo  \n",
       "2               Togo  \n",
       "3               Togo  \n",
       "4               Peru  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choc.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "lbRp1-zJDOOG"
   },
   "source": [
    "### How would you split the `Rating` values into 3 equally sized bins?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "3GA8RVSRDOOG",
    "outputId": "6fd9da5c-9695-45e2-893e-50a97dea8db0"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3.75, 2.75, 3.  , 3.5 , 4.  , 3.25, 2.5 , 5.  , 1.75, 1.5 , 2.25,\n",
       "       2.  , 1.  ])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choc['Rating'].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use a histogram! The `bins=n` kwarg allows us to specify the number of bins ('buckets') of values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       1876\n",
       "1       1676\n",
       "2       1676\n",
       "3       1680\n",
       "4       1704\n",
       "        ... \n",
       "1790     647\n",
       "1791     749\n",
       "1792     749\n",
       "1793     781\n",
       "1794     486\n",
       "Name: REF, Length: 1795, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choc.REF"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1795.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,\n",
       "            0.],\n",
       "        [1944.,  157.,  150.,  176.,  178.,  187.,  191.,  215.,  197.,\n",
       "          195.]]),\n",
       " array([1.0000e+00, 1.9610e+02, 3.9120e+02, 5.8630e+02, 7.8140e+02,\n",
       "        9.7650e+02, 1.1716e+03, 1.3667e+03, 1.5618e+03, 1.7569e+03,\n",
       "        1.9520e+03]),\n",
       " <a list of 2 BarContainer objects>)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15560 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# choc.select_dtypes(include='number')\n",
    "plt.hist([choc['Rating'], choc['REF']], stacked=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "NX3B8D50DOOM",
    "outputId": "d6d35a0e-66c3-4ae3-8e0f-beca1ee6be2e"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16276 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "choc['Rating'].plot(kind='hist', bins=3);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sometimes it is helpful to increase this number if you think you might have an outlier or a zero-weighted set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "SvtT0px2DOOQ",
    "outputId": "6f0840c3-e0f0-4c4f-f9e3-199921b567b5"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 24392 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "choc['Rating'].plot(kind='hist', bins=20)\n",
    "plt.ylabel('Number of Ratings')\n",
    "plt.xlabel('Chocolate Rating')\n",
    "plt.title('My Title');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ok1NR7AgDOOU"
   },
   "source": [
    "### Knowledge check: \n",
    "What does the y-axis represent on a histogram? What about the x-axis? How would you explain a histogram to a non-technical person?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "St7-JnIMDOOV"
   },
   "source": [
    "### Making histograms of an entire dataframe:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "9Z_Qlm-DDOOW",
    "outputId": "9167113b-c728-494c-852c-70feffa51be8"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 34916 characters]\n",
      "text/plain": [
       "<Figure size 1152x576 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "choc.hist(figsize=(16,8));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "JgMCXRAwDOOa"
   },
   "source": [
    "### Why doesn't it make plots of ALL the columns in the dataframe?\n",
    "\n",
    "Hint: what is different about the columns it plots vs. the ones it left out?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "BDjFPIqXDOOa",
    "outputId": "381b4346-18b3-4437-c1bc-52bb1d80d901"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Company \\n(Maker-if known)</th>\n",
       "      <th>Specific Bean Origin\\nor Bar Name</th>\n",
       "      <th>REF</th>\n",
       "      <th>Review\\nDate</th>\n",
       "      <th>Cocoa\\nPercent</th>\n",
       "      <th>Company\\nLocation</th>\n",
       "      <th>Rating</th>\n",
       "      <th>Bean\\nType</th>\n",
       "      <th>Broad Bean\\nOrigin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Agua Grande</td>\n",
       "      <td>1876</td>\n",
       "      <td>2016</td>\n",
       "      <td>63%</td>\n",
       "      <td>France</td>\n",
       "      <td>3.75</td>\n",
       "      <td></td>\n",
       "      <td>Sao Tome</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Kpime</td>\n",
       "      <td>1676</td>\n",
       "      <td>2015</td>\n",
       "      <td>70%</td>\n",
       "      <td>France</td>\n",
       "      <td>2.75</td>\n",
       "      <td></td>\n",
       "      <td>Togo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A. Morin</td>\n",
       "      <td>Atsane</td>\n",
       "      <td>1676</td>\n",
       "      <td>2015</td>\n",
       "      <td>70%</td>\n",
       "      <td>France</td>\n",
       "      <td>3.00</td>\n",
       "      <td></td>\n",
       "      <td>Togo</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  Company \\n(Maker-if known) Specific Bean Origin\\nor Bar Name   REF  \\\n",
       "0                   A. Morin                       Agua Grande  1876   \n",
       "1                   A. Morin                             Kpime  1676   \n",
       "2                   A. Morin                            Atsane  1676   \n",
       "\n",
       "   Review\\nDate Cocoa\\nPercent Company\\nLocation  Rating Bean\\nType  \\\n",
       "0          2016            63%            France    3.75              \n",
       "1          2015            70%            France    2.75              \n",
       "2          2015            70%            France    3.00              \n",
       "\n",
       "  Broad Bean\\nOrigin  \n",
       "0           Sao Tome  \n",
       "1               Togo  \n",
       "2               Togo  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choc.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's take a look at the data types of all the columns:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Company \\n(Maker-if known)            object\n",
       "Specific Bean Origin\\nor Bar Name     object\n",
       "REF                                    int64\n",
       "Review\\nDate                           int64\n",
       "Cocoa\\nPercent                        object\n",
       "Company\\nLocation                     object\n",
       "Rating                               float64\n",
       "Bean\\nType                            object\n",
       "Broad Bean\\nOrigin                    object\n",
       "dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choc.dtypes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It looks like it included `REF`, `Review Date`, and `Rating`. These have datatypes of `int64`, `int64`, and `float64` respectively. What do these all have in common, that is different from the other data types?\n",
    "<br><br>\n",
    "<details>\n",
    "    <summary>Click for the answer!</summary>\n",
    "    They're all **numeric!** The other columns are **categorical**, specifically string values.\n",
    "</details>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can filter on these types using the `select_dtypes()` DataFrame method (which can be very handy!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>REF</th>\n",
       "      <th>Review\\nDate</th>\n",
       "      <th>Rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1876</td>\n",
       "      <td>2016</td>\n",
       "      <td>3.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1676</td>\n",
       "      <td>2015</td>\n",
       "      <td>2.75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1676</td>\n",
       "      <td>2015</td>\n",
       "      <td>3.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1680</td>\n",
       "      <td>2015</td>\n",
       "      <td>3.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1704</td>\n",
       "      <td>2015</td>\n",
       "      <td>3.50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    REF  Review\\nDate  Rating\n",
       "0  1876          2016    3.75\n",
       "1  1676          2015    2.75\n",
       "2  1676          2015    3.00\n",
       "3  1680          2015    3.50\n",
       "4  1704          2015    3.50"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "choc.select_dtypes(include='number').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "LtmF6np5DOOe"
   },
   "source": [
    "### Challenge: create a histogram of the `Review Date` column, with 10 bins, and label both axes\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 14208 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = choc['Review\\nDate'].hist()\n",
    "ax.set(xlabel='Year', ylabel='Counts');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "1HkkekvDDOOj"
   },
   "source": [
    "<a id=\"scatter-plots\"></a>\n",
    "## Scatter plots: Show the relationship between two numerical variables\n",
    "---\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Scatter plots are very good at showing the **interaction between two numeric variables** (especially when they're continuous)!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>Date</th>\n",
       "      <th>AveragePrice</th>\n",
       "      <th>Total Volume</th>\n",
       "      <th>4046</th>\n",
       "      <th>4225</th>\n",
       "      <th>4770</th>\n",
       "      <th>Total Bags</th>\n",
       "      <th>Small Bags</th>\n",
       "      <th>Large Bags</th>\n",
       "      <th>XLarge Bags</th>\n",
       "      <th>type</th>\n",
       "      <th>year</th>\n",
       "      <th>region</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>2015-12-27</td>\n",
       "      <td>1.33</td>\n",
       "      <td>64236.62</td>\n",
       "      <td>1036.74</td>\n",
       "      <td>54454.85</td>\n",
       "      <td>48.16</td>\n",
       "      <td>8696.87</td>\n",
       "      <td>8603.62</td>\n",
       "      <td>93.25</td>\n",
       "      <td>0.0</td>\n",
       "      <td>conventional</td>\n",
       "      <td>2015</td>\n",
       "      <td>Albany</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2015-12-20</td>\n",
       "      <td>1.35</td>\n",
       "      <td>54876.98</td>\n",
       "      <td>674.28</td>\n",
       "      <td>44638.81</td>\n",
       "      <td>58.33</td>\n",
       "      <td>9505.56</td>\n",
       "      <td>9408.07</td>\n",
       "      <td>97.49</td>\n",
       "      <td>0.0</td>\n",
       "      <td>conventional</td>\n",
       "      <td>2015</td>\n",
       "      <td>Albany</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>2015-12-13</td>\n",
       "      <td>0.93</td>\n",
       "      <td>118220.22</td>\n",
       "      <td>794.70</td>\n",
       "      <td>109149.67</td>\n",
       "      <td>130.50</td>\n",
       "      <td>8145.35</td>\n",
       "      <td>8042.21</td>\n",
       "      <td>103.14</td>\n",
       "      <td>0.0</td>\n",
       "      <td>conventional</td>\n",
       "      <td>2015</td>\n",
       "      <td>Albany</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0        Date  AveragePrice  Total Volume     4046       4225  \\\n",
       "0           0  2015-12-27          1.33      64236.62  1036.74   54454.85   \n",
       "1           1  2015-12-20          1.35      54876.98   674.28   44638.81   \n",
       "2           2  2015-12-13          0.93     118220.22   794.70  109149.67   \n",
       "\n",
       "     4770  Total Bags  Small Bags  Large Bags  XLarge Bags          type  \\\n",
       "0   48.16     8696.87     8603.62       93.25          0.0  conventional   \n",
       "1   58.33     9505.56     9408.07       97.49          0.0  conventional   \n",
       "2  130.50     8145.35     8042.21      103.14          0.0  conventional   \n",
       "\n",
       "   year  region  \n",
       "0  2015  Albany  \n",
       "1  2015  Albany  \n",
       "2  2015  Albany  "
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avo.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 39224 characters]\n",
      "text/plain": [
       "<Figure size 720x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "avo.plot(kind='scatter', x='Total Volume', y='AveragePrice', \\\n",
    "        color='dodgerblue', figsize=(10,4), s=10, alpha=0.5);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Oh snap! What did we just make?! It's a [price elasticity curve!](https://en.wikipedia.org/wiki/Price_elasticity_of_demand)\n",
    "\n",
    "<img src=\"https://upload.wikimedia.org/wikipedia/commons/3/36/Marshall_PED.png\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "cI3W9z6pDOO1"
   },
   "source": [
    "We can also use a thing called a **scatter matrix** or a **pairplot**, which is a grid of scatter plots. This allows you to quickly **view the interaction of N x M features**. You are generally looking for a trend between variables (a line or curve). Using machine learning, you can fit these curves to provide predictive power."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Total Bags</th>\n",
       "      <th>Small Bags</th>\n",
       "      <th>Large Bags</th>\n",
       "      <th>XLarge Bags</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>8696.87</td>\n",
       "      <td>8603.62</td>\n",
       "      <td>93.25</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9505.56</td>\n",
       "      <td>9408.07</td>\n",
       "      <td>97.49</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>8145.35</td>\n",
       "      <td>8042.21</td>\n",
       "      <td>103.14</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5811.16</td>\n",
       "      <td>5677.40</td>\n",
       "      <td>133.76</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6183.95</td>\n",
       "      <td>5986.26</td>\n",
       "      <td>197.69</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18244</th>\n",
       "      <td>13498.67</td>\n",
       "      <td>13066.82</td>\n",
       "      <td>431.85</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18245</th>\n",
       "      <td>9264.84</td>\n",
       "      <td>8940.04</td>\n",
       "      <td>324.80</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18246</th>\n",
       "      <td>9394.11</td>\n",
       "      <td>9351.80</td>\n",
       "      <td>42.31</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18247</th>\n",
       "      <td>10969.54</td>\n",
       "      <td>10919.54</td>\n",
       "      <td>50.00</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18248</th>\n",
       "      <td>12014.15</td>\n",
       "      <td>11988.14</td>\n",
       "      <td>26.01</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>18249 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       Total Bags  Small Bags  Large Bags  XLarge Bags\n",
       "0         8696.87     8603.62       93.25          0.0\n",
       "1         9505.56     9408.07       97.49          0.0\n",
       "2         8145.35     8042.21      103.14          0.0\n",
       "3         5811.16     5677.40      133.76          0.0\n",
       "4         6183.95     5986.26      197.69          0.0\n",
       "...           ...         ...         ...          ...\n",
       "18244    13498.67    13066.82      431.85          0.0\n",
       "18245     9264.84     8940.04      324.80          0.0\n",
       "18246     9394.11     9351.80       42.31          0.0\n",
       "18247    10969.54    10919.54       50.00          0.0\n",
       "18248    12014.15    11988.14       26.01          0.0\n",
       "\n",
       "[18249 rows x 4 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avo.select_dtypes(include='number').iloc[:,-5:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "0vTeuwo7DOO1",
    "outputId": "f4645002-dc3b-49f9-d751-42c43c9c58aa"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 155144 characters]\n",
      "text/plain": [
       "<Figure size 864x864 with 16 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pd.plotting.scatter_matrix(\n",
    "    avo.select_dtypes(include='number').iloc[:,-5:-1],\n",
    "    figsize=(12,12)\n",
    ");"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also use a very handy parameter, `c`, which allows us to color the dots in a scatter plot. This is extremely helpful when doing **classification problems**, often you will set the color to the class label."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['conventional', 'organic'], dtype=object)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avo['type'].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's map the `type` field to the color of the dot in our price elasticity curve. To use the type field, we need to convert it from a string into a number. We can use `pd.Series.apply()` for this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'conventional': 0, 'organic': 1}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mapping_dict = {}\n",
    "initial_class_label = 0\n",
    "for type in list(avo['type'].unique()):\n",
    "    mapping_dict[type] = initial_class_label\n",
    "    initial_class_label += 1\n",
    "mapping_dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can see we have two unique type labels, `conventional` and `organic`. Although that is the case for this dataset, let's create a function that will store the labels in a dictionary, incrementing the number up by `1` for each new label. This way, if we receive an additional type label in the future, our code won't break. Always think about extensible code!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0        0\n",
       "1        0\n",
       "2        0\n",
       "3        0\n",
       "4        0\n",
       "        ..\n",
       "18244    1\n",
       "18245    1\n",
       "18246    1\n",
       "18247    1\n",
       "18248    1\n",
       "Name: type, Length: 18249, dtype: int64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avo.type.map(mapping_dict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can use this `mapping_dict` dictionary to map the values using `.apply()`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>type</th>\n",
       "      <th>type_as_num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>conventional</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>conventional</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>conventional</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           type  type_as_num\n",
       "0  conventional            0\n",
       "1  conventional            0\n",
       "2  conventional            0"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "avo['type_as_num'] = avo['type'].apply(lambda x: mapping_dict[x])\n",
    "avo[['type', 'type_as_num']].head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can use this **binary class label** as our `c` parameter to gain some insight:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 0, 'Volume')"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 42600 characters]\n",
      "text/plain": [
       "<Figure size 576x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "avo.plot(kind='scatter', x='Total Volume', y='AveragePrice', \\\n",
    "        c='type_as_num', colormap='winter', figsize=(8,4))\n",
    "plt.xlabel('Volume')\n",
    "# plt.savefig('./avo_price.png');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Amazing! It looks like the organic avocados (value of `1`) totally occupy the lower volume, higher price bracket. Those dang kids with their toast and unicycles driving up the price of my 'cados!\n",
    "\n",
    "Here, we can also see a 'more' continuous `c` parameter, `year`, which makes use of the gradient a little better. There are tons of gradients you can use, check them out [here](https://matplotlib.org/examples/color/colormaps_reference.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<AxesSubplot:xlabel='Total Volume', ylabel='AveragePrice'>"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 52220 characters]\n",
      "text/plain": [
       "<Figure size 576x288 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "avo.plot(kind='scatter', x='Total Volume', y='AveragePrice', \\\n",
    "        c='year', colormap='winter', figsize=(8,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 77812 characters]\n",
      "text/plain": [
       "<Figure size 576x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sns.scatterplot(x='Total Volume', y='AveragePrice', hue='type', data=avo);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can save the plot to a file, using the `plt.savefig()` method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Enp2scMwDOO8",
    "outputId": "9e479b9b-7a9a-42ce-8268-1788d23330e7"
   },
   "outputs": [],
   "source": [
    "avo.plot(kind='scatter', x='Total Volume', y='AveragePrice', \\\n",
    "        c='type_as_num', colormap='winter', figsize=(8,4))\n",
    "plt.xlabel('Volume')\n",
    "plt.savefig('avo_price.png');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Ron6_N0BDOO-"
   },
   "source": [
    "<a id=\"summary\"></a>\n",
    "### Summary\n",
    "\n",
    "In this lesson, we showed examples of how to create a variety of plots using Pandas and Matplotlib. We also showed how to use each plot to effectively display data.\n",
    "\n",
    "Do not be concerned if you do not remember everything — this will come with practice! Although there are many plot styles, many similarities exist between how each plot is drawn. For example, they have most parameters in common, and the same Matplotlib functions are used to modify the plot area.\n",
    "\n",
    "We looked at:\n",
    "- Line plots\n",
    "- Bar plots\n",
    "- Histograms\n",
    "- Scatter plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "nLX7Hs6zDOO-"
   },
   "source": [
    "### Additional Resources\n",
    "\n",
    "Always read the documentation!\n",
    "\n",
    "- [Pandas Plotting Documentation](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html)\n",
    "\n",
    "- [Matplotlib Documentation](https://matplotlib.org/)\n",
    "\n",
    "- [Matplotlib sample plots](https://matplotlib.org/tutorials/introductory/sample_plots.html)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "04-plotting-with-pandas.ipynb",
   "provenance": [],
   "version": "0.3.2"
  },
  "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
}
