{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": [
     "remove_input"
    ]
   },
   "outputs": [],
   "source": [
    "path_data = '../data/'\n",
    "path_images = '../images/'\n",
    "\n",
    "import pandas as pd\n",
    "import matplotlib\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "plt.style.use('bmh')\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 7. Visualization"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tables are a powerful way of organizing and visualizing data. However, large tables of numbers can be difficult to interpret, no matter how organized they are. Sometimes it is much easier to interpret graphs than numbers.\n",
    "\n",
    "In this chapter we will develop some of the fundamental graphical methods of data analysis. Our source of data is the [Internet Movie Database](http://www.imdb.com), an online database that contains information about movies, television shows, video games, and so on. The site [Box Office Mojo](http://www.boxofficemojo.com) provides many summaries of IMDB data, some of which we have adapted. We have also used data summaries from [The Numbers](http://www.the-numbers.com), a site with a tagline that says it is \"where data and the movie business meet.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scatter Plots and Line Graphs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The table `actors` contains data on Hollywood actors, both male and female. The columns are:\n",
    "\n",
    "| ** Column **        | Contents |\n",
    "|---------------------|----------|\n",
    "|`Actor`              | Name of actor |\n",
    "|`Total Gross`        | Total gross domestic box office receipt, in millions of dollars, of all of the actor's movies |\n",
    "| `Number of Movies`  | The number of movies the actor has been in |\n",
    "| `Average per Movie` | Total gross divided by number of movies |\n",
    "| `#1 Movie`          | The highest grossing movie the actor has been in |\n",
    "| `Gross`             | Gross domestic box office receipt, in millions of dollars, of the actor's `#1 Movie` |\n",
    "\n",
    "In the calculation of the gross receipt, the data tabulators did not include movies where an actor had a cameo role or a speaking role that did not involve much screen time.\n",
    "\n",
    "The table has 50 rows, corresponding to the 50 top grossing actors. The table is already sorted by `Total Gross`, so it is easy to see that Harrison Ford is the highest grossing actor. In total, his movies have brought in more money at domestic box office than the movies of any other actor."
   ]
  },
  {
   "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>Actor</th>\n",
       "      <th>Total Gross</th>\n",
       "      <th>Number of Movies</th>\n",
       "      <th>Average per Movie</th>\n",
       "      <th>#1 Movie</th>\n",
       "      <th>Gross</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Harrison Ford</td>\n",
       "      <td>4871.7</td>\n",
       "      <td>41</td>\n",
       "      <td>118.8</td>\n",
       "      <td>Star Wars: The Force Awakens</td>\n",
       "      <td>936.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Samuel L. Jackson</td>\n",
       "      <td>4772.8</td>\n",
       "      <td>69</td>\n",
       "      <td>69.2</td>\n",
       "      <td>The Avengers</td>\n",
       "      <td>623.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Morgan Freeman</td>\n",
       "      <td>4468.3</td>\n",
       "      <td>61</td>\n",
       "      <td>73.3</td>\n",
       "      <td>The Dark Knight</td>\n",
       "      <td>534.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Tom Hanks</td>\n",
       "      <td>4340.8</td>\n",
       "      <td>44</td>\n",
       "      <td>98.7</td>\n",
       "      <td>Toy Story 3</td>\n",
       "      <td>415.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Robert Downey, Jr.</td>\n",
       "      <td>3947.3</td>\n",
       "      <td>53</td>\n",
       "      <td>74.5</td>\n",
       "      <td>The Avengers</td>\n",
       "      <td>623.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Actor  Total Gross  Number of Movies  Average per Movie  \\\n",
       "0       Harrison Ford       4871.7                41              118.8   \n",
       "1   Samuel L. Jackson       4772.8                69               69.2   \n",
       "2      Morgan Freeman       4468.3                61               73.3   \n",
       "3           Tom Hanks       4340.8                44               98.7   \n",
       "4  Robert Downey, Jr.       3947.3                53               74.5   \n",
       "\n",
       "                       #1 Movie  Gross  \n",
       "0  Star Wars: The Force Awakens  936.7  \n",
       "1                  The Avengers  623.4  \n",
       "2               The Dark Knight  534.9  \n",
       "3                   Toy Story 3  415.0  \n",
       "4                  The Avengers  623.4  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actors = pd.read_csv(path_data + 'actors.csv')\n",
    "actors.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Terminology.**\n",
    "A *variable* is a formal name for what we have been calling a \"feature\", such as 'number of movies.' The term *variable* emphasizes that the feature can have different values for different individuals – the numbers of movies that actors have been in varies across all the actors.\n",
    "\n",
    "Variables that have numerical values, such as 'number of movies' or 'average gross receipts per movie' are called *quantitative* or *numerical* variables."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Scatter Plots\n",
    "A *scatter plot* displays the relation between two numerical variables. You saw an example of a scatter plot in an early section where we looked at the number of periods and number of characters in two classic novels.\n",
    "\n",
    "The matplotlib method `scatter` draws a scatter plot consisting of one point for each row of the DataFrame. Its first argument is the label of the column to be plotted on the horizontal axis, and its second argument is the label of the column on the vertical.  \n",
    "\n",
    "[matplotlib Usage](https://matplotlib.org/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py)\n",
    "\n",
    "[matplotlib style](https://matplotlib.org/3.1.1/gallery/style_sheets/style_sheets_reference.html) Restart and clear kernel to apply. To get an idea of the effect of style uncomment (remove the hash symbol) the instruction `#plots.style.use('fivethirtyeight')`\n",
    "and comment `plots.style.use('bmh')` in the first cell of this notebook (import cell).\n",
    "\n",
    "[matplotlib legend](https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.pyplot.legend.html)\n",
    "\n",
    "[matplotlib scatter](https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.scatter.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### matplotlib scatter plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 25772 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = actors['Number of Movies']\n",
    "y = actors['Total Gross']\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "ax.scatter(x, y, c='red', label='Gross/#Movies', edgecolors='black')\n",
    "ax.set_title('Gross Vs Number Movies')\n",
    "ax.set_xlabel('Number of Movies')\n",
    "ax.set_ylabel('Total Gross')\n",
    "\n",
    "\n",
    "\n",
    "legend = ax.legend(loc='upper left', shadow=True, fontsize='x-large')\n",
    "\n",
    "# Put a nicer background color on the legend.\n",
    "legend.get_frame().set_facecolor('white')\n",
    "\n",
    "plt.show()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Pandas scatter plot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21572 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "actors.plot.scatter(x = 'Number of Movies', y = 'Total Gross', c='red', label='Gross Vs Number Movies')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The plot contains 50 points, one point for each actor in the table. You can see that it slopes upwards, in general. The more movies an actor has been in, the more the total gross of all of those movies – in general.\n",
    "\n",
    "Formally, we say that the plot shows an *association* between the variables, and that the association is *positive*: high values of one variable tend to be associated with high values of the other, and low values of one with low values of the other, in general. \n",
    "\n",
    "Of course there is some variability. Some actors have high numbers of movies but middling total gross receipts. Others have middling numbers of movies but high receipts. That the association is positive is simply a statement about the broad general trend.\n",
    "\n",
    "Later in the course we will study how to quantify association. For the moment, we will just think about it qualitatively."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have explored how the number of movies is related to the *total* gross receipt, let's turn our attention to how it is related to the *average* gross receipt per movie."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 20592 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = actors['Number of Movies']\n",
    "y = actors['Average per Movie']\n",
    "\n",
    "plt.scatter(x, y, c='blue')\n",
    "plt.title('Gross Vs Avg per Movie')\n",
    "plt.xlabel('Number of Movies')\n",
    "plt.ylabel('Average per Movie')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is a markedly different picture and shows a *negative* association. In general, the more movies an actor has been in, the *less* the average receipt per movie.\n",
    "\n",
    "Also, one of the points is quite high and off to the left of the plot. It corresponds to one actor who has a low number of movies and high average per movie. This point is an *outlier*. It lies outside the general range of the data. Indeed, it is quite far from all the other points in the plot."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will examine the negative association further by looking at points at the right and left ends of the plot. \n",
    "\n",
    "For the right end, let's zoom in on the main body of the plot by just looking at the portion that doesn't have the outlier."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18212 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "no_outlier = actors[actors['Number of Movies'] > 10]\n",
    "no_outlier.plot.scatter('Number of Movies', 'Average per Movie') #N.B. a pandas scatterplot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The negative association is still clearly visible. Let's identify the actors corresponding to the points that lie on the right hand side of the plot where the number of movies is large:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "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>Actor</th>\n",
       "      <th>Total Gross</th>\n",
       "      <th>Number of Movies</th>\n",
       "      <th>Average per Movie</th>\n",
       "      <th>#1 Movie</th>\n",
       "      <th>Gross</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Samuel L. Jackson</td>\n",
       "      <td>4772.8</td>\n",
       "      <td>69</td>\n",
       "      <td>69.2</td>\n",
       "      <td>The Avengers</td>\n",
       "      <td>623.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Morgan Freeman</td>\n",
       "      <td>4468.3</td>\n",
       "      <td>61</td>\n",
       "      <td>73.3</td>\n",
       "      <td>The Dark Knight</td>\n",
       "      <td>534.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>Robert DeNiro</td>\n",
       "      <td>3081.3</td>\n",
       "      <td>79</td>\n",
       "      <td>39.0</td>\n",
       "      <td>Meet the Fockers</td>\n",
       "      <td>279.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>Liam Neeson</td>\n",
       "      <td>2942.7</td>\n",
       "      <td>63</td>\n",
       "      <td>46.7</td>\n",
       "      <td>The Phantom Menace</td>\n",
       "      <td>474.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Actor  Total Gross  Number of Movies  Average per Movie  \\\n",
       "1   Samuel L. Jackson       4772.8                69               69.2   \n",
       "2      Morgan Freeman       4468.3                61               73.3   \n",
       "19      Robert DeNiro       3081.3                79               39.0   \n",
       "21        Liam Neeson       2942.7                63               46.7   \n",
       "\n",
       "              #1 Movie  Gross  \n",
       "1         The Avengers  623.4  \n",
       "2      The Dark Knight  534.9  \n",
       "19    Meet the Fockers  279.3  \n",
       "21  The Phantom Menace  474.5  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actors[actors['Number of Movies'] > 60]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The actor Robert DeNiro has the highest number of movies and the lowest average receipt per movie. Other actors are at points that are not very far away, but DeNiro's is at the extreme end.\n",
    "\n",
    "To understand the negative association, note that the more movies an actor is in, the more variable those movies might be, in terms of style, genre, and box office draw. For example, an actor might be in some high-grossing action movies or comedies (such as Meet the Fockers), and also in a large number of smaller films that may be excellent but don't draw large crowds. Thus the actor's value of average receipts per movie might be relatively low.\n",
    "\n",
    "To approach this argument from a different direction, let us now take a look at the outlier."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "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>Actor</th>\n",
       "      <th>Total Gross</th>\n",
       "      <th>Number of Movies</th>\n",
       "      <th>Average per Movie</th>\n",
       "      <th>#1 Movie</th>\n",
       "      <th>Gross</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Anthony Daniels</td>\n",
       "      <td>3162.9</td>\n",
       "      <td>7</td>\n",
       "      <td>451.8</td>\n",
       "      <td>Star Wars: The Force Awakens</td>\n",
       "      <td>936.7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              Actor  Total Gross  Number of Movies  Average per Movie  \\\n",
       "14  Anthony Daniels       3162.9                 7              451.8   \n",
       "\n",
       "                        #1 Movie  Gross  \n",
       "14  Star Wars: The Force Awakens  936.7  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actors[actors['Number of Movies'] < 10]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As an actor, Anthony Daniels might not have the stature of Robert DeNiro. But his 7 movies had an astonishingly high average receipt of nearly $452$ million dollars per movie.\n",
    "\n",
    "What were these movies? You might know about the droid C-3PO in Star Wars:\n",
    "![C-3PO](../../images/C-3PO_droid.png)\n",
    "That's [Anthony Daniels](https://en.wikipedia.org/wiki/Anthony_Daniels) inside the metallic suit. He plays C-3PO.\n",
    "\n",
    "Mr. Daniels' entire filmography (apart from cameos) consists of movies in the high-grossing Star Wars franchise. That explains both his high average receipt and his low number of movies.\n",
    "\n",
    "Variables such as genre and production budget have an effect on the association between the number of movies and the average receipt per movie. This example is a reminder that studying the association between two variables often involves understanding other related variables as well. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Line Graphs\n",
    "Line graphs are among the most common visualizations and are often used to study chronological trends and patterns.\n",
    "\n",
    "The table `movies_by_year` contains data on movies produced by U.S. studios in each of the years 1980 through 2015. The columns are:\n",
    "\n",
    "| **Column** | Content |\n",
    "|------------|---------|\n",
    "| `Year` | Year |\n",
    "| `Total Gross` | Total domestic box office gross, in millions of dollars, of all movies released |\n",
    "| `Number of Movies` | Number of movies released |\n",
    "| `#1 Movie` | Highest grossing movie |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "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>Year</th>\n",
       "      <th>Total Gross</th>\n",
       "      <th>Number of Movies</th>\n",
       "      <th>#1 Movie</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2015</td>\n",
       "      <td>11128.5</td>\n",
       "      <td>702</td>\n",
       "      <td>Star Wars: The Force Awakens</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2014</td>\n",
       "      <td>10360.8</td>\n",
       "      <td>702</td>\n",
       "      <td>American Sniper</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2013</td>\n",
       "      <td>10923.6</td>\n",
       "      <td>688</td>\n",
       "      <td>Catching Fire</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2012</td>\n",
       "      <td>10837.4</td>\n",
       "      <td>667</td>\n",
       "      <td>The Avengers</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2011</td>\n",
       "      <td>10174.3</td>\n",
       "      <td>602</td>\n",
       "      <td>Harry Potter / Deathly Hallows (P2)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2010</td>\n",
       "      <td>10565.6</td>\n",
       "      <td>536</td>\n",
       "      <td>Toy Story 3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2009</td>\n",
       "      <td>10595.5</td>\n",
       "      <td>521</td>\n",
       "      <td>Avatar</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2008</td>\n",
       "      <td>9630.7</td>\n",
       "      <td>608</td>\n",
       "      <td>The Dark Knight</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2007</td>\n",
       "      <td>9663.8</td>\n",
       "      <td>631</td>\n",
       "      <td>Spider-Man 3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2006</td>\n",
       "      <td>9209.5</td>\n",
       "      <td>608</td>\n",
       "      <td>Dead Man's Chest</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Year  Total Gross  Number of Movies                             #1 Movie\n",
       "0  2015      11128.5               702         Star Wars: The Force Awakens\n",
       "1  2014      10360.8               702                      American Sniper\n",
       "2  2013      10923.6               688                        Catching Fire\n",
       "3  2012      10837.4               667                         The Avengers\n",
       "4  2011      10174.3               602  Harry Potter / Deathly Hallows (P2)\n",
       "5  2010      10565.6               536                          Toy Story 3\n",
       "6  2009      10595.5               521                               Avatar\n",
       "7  2008       9630.7               608                      The Dark Knight\n",
       "8  2007       9663.8               631                         Spider-Man 3\n",
       "9  2006       9209.5               608                     Dead Man's Chest"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movies_by_year = pd.read_csv(path_data + 'movies_by_year.csv')\n",
    "movies_by_year.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Table method `plot` produces a line graph. Its two arguments are the same as those for `scatter`: first the column on the horizontal axis, then the column on the vertical. Here is a line graph of the number of movies released each year over the years 1980 through 2015."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26468 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#N.B. this is a Pandas plot\n",
    "\n",
    "movies_by_year.plot('Year', 'Number of Movies')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The graph rises sharply and then has a gentle upwards trend though the numbers vary noticeably from year to year. The sharp rise in the early 1980's is due in part to studios returning to the forefront of movie production after some years of filmmaker driven movies in the 1970's. \n",
    "\n",
    "Our focus will be on more recent years. In keeping with the theme of movies, the table of rows corresponding to the years 2000 through 2015 have been assigned to the name `century_21`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "century_21 = movies_by_year[movies_by_year['Year'] > 1999]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 23640 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "century_21.plot('Year', 'Number of Movies')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The global financial crisis of 2008 has a visible effect – in 2009 there is a sharp drop in the number of movies released.\n",
    "\n",
    "The dollar figures, however, didn't suffer much."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 26796 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "century_21.plot('Year', 'Total Gross')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The total domestic gross receipt was higher in 2009 than in 2008, even though there was a financial crisis and a much smaller number of movies were released.\n",
    "\n",
    "One reason for this apparent contradiction is that people tend to go to the movies when there is a recession. [\"In Downturn, Americans Flock to the Movies,\"](http://www.nytimes.com/2009/03/01/movies/01films.html?_r=0) said the New York Times in February 2009. The article quotes Martin Kaplan of the University of Southern California saying, \"People want to forget their troubles, and they want to be with other people.\" When holidays and expensive treats are unaffordable, movies provide welcome entertainment and relief.\n",
    "\n",
    "In 2009, another reason for high box office receipts was the movie Avatar and its 3D release. Not only was Avatar the \\#1 movie of 2009, it is also by some calculations the second highest grossing movie of all time, as we will see later."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "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>Year</th>\n",
       "      <th>Total Gross</th>\n",
       "      <th>Number of Movies</th>\n",
       "      <th>#1 Movie</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2009</td>\n",
       "      <td>10595.5</td>\n",
       "      <td>521</td>\n",
       "      <td>Avatar</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Year  Total Gross  Number of Movies #1 Movie\n",
       "6  2009      10595.5               521   Avatar"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "century_21[century_21['Year'] == 2009]"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
