{ "cells": [ { "cell_type": "markdown", "id": "5b26900a-e5e2-48ae-83d2-56a90e0772f8", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "id": "423dfbdd-b5ec-4359-a470-546cd68da52c", "metadata": {}, "source": [ "# GridSpec\n", "#### A `matplotlib.gridspec.GridSpec(nrows, ncols)` object specifies the grid coordinates of a subplot." ] }, { "cell_type": "code", "execution_count": null, "id": "b67a1674-597c-4937-be58-ffc8041630c6", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from pandas import Series\n", "\n", "import numpy as np\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "id": "3b530d87-f38c-41a0-877b-148b5775be2e", "metadata": {}, "source": [ "# Example: Stock Prices" ] }, { "cell_type": "code", "execution_count": null, "id": "310d55bf-1e2f-4a36-912e-a81698d2d5bf", "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": "4cf20487-f17a-4a38-929e-0b6ffebaf019", "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": "800d4fe8-d14a-43f1-a3e4-cf69b4897b60", "metadata": {}, "source": [ "#### Create a 3 by 4 `GridSpec` object." ] }, { "cell_type": "code", "execution_count": null, "id": "d23488f0-c213-4ceb-8bda-639aefacb311", "metadata": {}, "outputs": [], "source": [ "gs = matplotlib.gridspec.GridSpec(3, 4)\n", "gs" ] }, { "cell_type": "code", "execution_count": null, "id": "9cf1e2ca-5fe1-40b4-a016-e0be8b8bef18", "metadata": {}, "outputs": [], "source": [ "# The Apple subplot uses the leftmost \n", "# 3 by 3 block of grid cells.\n", "ax1 = plt.subplot(gs[:3, :3])\n", "ax1.plot(apple.close)\n", "ax1.set_title('Apple')\n", "\n", "# The Google subplot uses the\n", "# upper right grid cell.\n", "ax2 = plt.subplot(gs[0, 3])\n", "ax2.plot(google.close)\n", "ax2.set_title('Google')\n", "\n", "# The Facebook subplot uses the\n", "# middle grid cell on the right.\n", "ax3 = plt.subplot(gs[1, 3])\n", "ax3.plot(facebook.close)\n", "ax3.set_title('Facebook')\n", "\n", "# The Amazon subplot uses the\n", "# lower right grid cell\n", "ax4 = plt.subplot(gs[2, 3])\n", "ax4.plot(amazon.close)\n", "ax4.set_title('Amazon')\n", "\n", "plt.tight_layout()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "9c3419bb-6d17-4091-892a-9741486d10f1", "metadata": {}, "outputs": [], "source": [ "plt.close()" ] }, { "cell_type": "markdown", "id": "119a1669-a1d5-41fc-b9b3-7dc3536f1dda", "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": "e0318687-f07b-4f33-8f00-cf30b925d7a6", "metadata": {}, "outputs": [], "source": [ "# Additional material (c) 2024 by Ronald Mak" ] } ], "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 }