{ "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": [ "#### Module `matplotlib` contains Python's the most popular graphing library. It emulates the graphing capabilities of MATLAB, a commercial package of scientific and mathematical software. `matplotlib.pyplot` is a collection of plotting functions, usually imported as `plt`.\n", "#### A `matplotlib` plot is an object that contains multiple objects (the object-oriented *has-a* relationship) nested in a tree structure. \n", "#### The top visualization container is a `Figure` object. It is the outermost container which serves as the canvas upon which we can draw multiple plots." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating a simple visualization." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In this exercise, we will create our first simple plot using Matplotlib." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Import the necessary modules and enable plotting within a Jupyter notebook." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a figure" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Explicitly create a figure and set the dpi (dots per inch) to 200." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(dpi=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot data pairs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot the following (x, y) data points: (1, 1), (2, 3), (4, 4), (5, 3). Therefore, the x values are `[1, 2, 4, 5]` and the y values are `[1, 3, 4, 3]`. Show the data points as circles connected by line segments: `-o`. Show the plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.plot([1, 2, 4, 5], [1, 3, 4, 3], '-o')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Format strings" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In the previous example, the argument string `'-o'` is a ***format string*** of the form '*color marker line-type*' where each item is optional.\n", "- #### For *color*, see [Specifying colors](https://matplotlib.org/stable/tutorials/colors/colors.html) and [List of named colors](https://matplotlib.org/stable/gallery/color/named_colors.html)\n", "- #### For *marker*, see [matplotlib.markers](https://matplotlib.org/stable/api/markers_api.html)\n", "- #### For *line-type*, see [Line Plot Styles in Matplotlib](https://medium.com/swlh/line-plot-styles-in-matplotlib-c6ffd7a34d46)\n", "#### Therefore, `'-o'` specifies a solid line with filled circles as markers, and the default color is black." ] }, { "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) 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": 4 }