{ "cells": [ { "cell_type": "markdown", "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", "metadata": {}, "source": [ "# Line Graphs: Visualize Stock Trends" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### You are interested in investing in stocks, and so you downloaded the stock prices for the “big five”: Amazon, Google, Apple, Facebook, and Microsoft. Create a line plot to show stock trends." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Figure title\n", "#### Set the title for the figure. For example:\n", "``` Python\n", "plt.title('Graph Title')\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load datasets\n", "#### Use `pandas` to read the CSV data files." ] }, { "cell_type": "code", "execution_count": null, "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')\n", "microsoft = pd.read_csv('MSFT_data.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "google" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create the graph" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use `matplotlib` to create a line chart visualizing the closing prices for the past five years (whole data sequence) for all five companies. Add labels, titles, and a legend to make the visualization self-explanatory. Use `plt.grid()` to add a grid to your plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create figure\n", "plt.figure(figsize=(16, 8), dpi=300)\n", "\n", "# Plot data\n", "plt.plot('date', 'close', data=google, label='Google')\n", "plt.plot('date', 'close', data=facebook, label='Facebook')\n", "plt.plot('date', 'close', data=apple, label='Apple')\n", "plt.plot('date', 'close', data=amazon, label='Amazon')\n", "plt.plot('date', 'close', data=microsoft, label='Microsoft')\n", "\n", "# Specify the ticks for x- and y-axis.\n", "plt.xticks(np.arange(0, 1260, 40), rotation=70)\n", "plt.yticks(np.arange(0, 1450, 100))\n", "\n", "# Add the title and label for y-axis.\n", "plt.title('Stock trend', fontsize=16)\n", "plt.ylabel('Closing price in $', fontsize=14)\n", "\n", "# Add grid.\n", "plt.grid()\n", "\n", "# Add legend.\n", "plt.legend()\n", "\n", "# Show plot\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.close()" ] }, { "cell_type": "markdown", "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, "metadata": {}, "outputs": [], "source": [ "# Additional material (c) 2023 by Ronald Mak" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }