{ "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": [ "# Multiple Images" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot images in a grid." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib.image as mpimg\n", "\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load all four images from the `images` folder." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Load images\n", "img_filenames = os.listdir('images')\n", "imgs = [mpimg.imread(os.path.join('images', img_filename)) \n", " for img_filename in img_filenames]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualize the images in a 2x2 grid. Remove the axes and give each image a label." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create the subplots.\n", "fig, axes = plt.subplots(2, 2)\n", "fig.figsize = (6, 6)\n", "fig.dpi = 150\n", "\n", "axes = axes.ravel()\n", "\n", "# Specify the labels.\n", "labels = ['coast', 'beach', 'building', 'city at night']\n", "\n", "# Plot the images.\n", "for i in range(len(imgs)):\n", " axes[i].imshow(imgs[i])\n", " axes[i].set_xticks([])\n", " axes[i].set_yticks([])\n", " axes[i].set_xlabel(labels[i])" ] }, { "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 }