{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a39a8d3f-4a75-4e2e-aef7-5e7e8aafab3e",
   "metadata": {},
   "source": [
    "### <center>San Jose State University<br>Department of Applied Data Science<br><br>**DATA 200<br>Computational Programming for Data Analytics**<br><br>Spring 2024<br>Instructor: Ron Mak</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d5c9201-9336-40ba-82c4-30941d9b8ce4",
   "metadata": {},
   "source": [
    "# Subplots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cf44740-7e59-421f-bcd8-75513bd2d1ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import Series\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5309100a-3d57-49d4-92e6-791046d4c988",
   "metadata": {},
   "source": [
    "# Example: Stock Prices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "778330d5-7136-43c8-a490-2f324b8b12e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "google    = pd.read_csv('GOOGL_data.csv')\n",
    "facebook  = pd.read_csv('FB_data.csv')\n",
    "apple     = pd.read_csv('AAPL_data.csv')\n",
    "amazon    = pd.read_csv('AMZN_data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ddb11dc-eab3-4b85-a6c7-ffa93930e382",
   "metadata": {},
   "outputs": [],
   "source": [
    "apple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04b8c4e5-b2df-46a4-ba48-3a1b72521412",
   "metadata": {},
   "outputs": [],
   "source": [
    "stocks = ['Google', 'Facebook', 'Apple', 'Amazon']\n",
    "\n",
    "closing_prices = [google.close, facebook.close, \n",
    "                  apple.close,  amazon.close]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d96ae339-c8dc-468d-92c6-48e18bc8691d",
   "metadata": {},
   "source": [
    "#### We want to display several graphs in a grid. Recall that an `Axes` object is a single plot or subplot.\n",
    "#### Call `plt.subplots(nrows, ncols)` to create a `Figure` object and a set of subplots, where\n",
    "- #### *nrows* is the number of rows in the grid\n",
    "- #### *ncols* is the number of columns in the grid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "873eb039-a2fd-4ed9-a221-a02c44965e15",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2, 2)\n",
    "axes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d57df080-3eac-4f3a-8c50-e87e24625f05",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Make a 1-d array copy of the Axes object.\n",
    "axes = axes.ravel()\n",
    "axes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d95f32ba-aab6-47c0-a39b-af9bf94bc2f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i, ax in enumerate(axes):\n",
    "    ax.plot(closing_prices[i])\n",
    "    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dfceadc-4435-4d29-94b6-26f33089aa53",
   "metadata": {},
   "source": [
    "#### An equivalent way to specify subplots is to call `plt.subplots(nrows, ncols, index)`, where *index* starts with 1. The call `plt.subplots(2, 2, 1)` can also be made as `plt.subplots(211)`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4846b88-a775-46d3-a474-a2e74d261edd",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    plt.subplot(2, 2, i + 1)\n",
    "    plt.plot(closing_prices[i])\n",
    "        \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75be56fb-aa90-451f-9ae8-de8f9401aaf0",
   "metadata": {},
   "source": [
    "#### We can compare the stock prices better if the graphs all shared the same x and y axes. Include the keyword arguments `sharex=True` and  `sharey=True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37646014-2c04-4849-a223-3a5450f7f9c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)\n",
    "axes = axes.ravel()\n",
    "\n",
    "for i, ax in enumerate(axes):\n",
    "    ax.plot(closing_prices[i])\n",
    "        \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab89fb9d-ea9d-4450-a06a-173bf88af720",
   "metadata": {},
   "source": [
    "#### Label each subplot. Specify `plt.tight_layout()` to prevent the subplots from overlapping."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "47ddca60-a443-4073-bc1d-ede26aa556a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2, 2)\n",
    "axes = axes.ravel()\n",
    "\n",
    "for i, ax in enumerate(axes):\n",
    "    ax.plot(closing_prices[i])\n",
    "    ax.set_title(stocks[i])\n",
    "    \n",
    "plt.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bfb05d3-2fb4-4809-a680-e77be49cec06",
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f8b537e-d8e0-4a55-96ee-3b48097322d6",
   "metadata": {},
   "source": [
    "#### Adapted from ***Data Visualization with Python***, by Mario Döbler and Tim Großmann, Packt 2019, ISBN 978-1-78995-646-7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84372ab6-4173-48be-9b58-d34900c6d4f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Additional material (c) 2024 by Ronald Mak"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7e587ec-9018-47ec-9430-5eacfc7cb55b",
   "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": 5
}
