{
 "cells": [
  {
   "cell_type": "markdown",
   "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",
   "metadata": {},
   "source": [
    "## 10.6.4 Displaying Card Images with Matplotlib "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deck import DeckOfCards"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deck_of_cards = DeckOfCards()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Enable Matplotlib in IPython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create the Base `Path` for Each Image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = Path('.').joinpath('card_images')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import the Matplotlib Features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.image as mpimg"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create the `Figure` and `Axes` Objects, Configure the `Axes` Objects and Display the Images and Maximize the Image Sizes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4 rows of 13 columns each.\n",
    "figure, axes_list = plt.subplots(nrows=4, ncols=13)\n",
    "\n",
    "# Increase the total figure size in the notebook.\n",
    "figure.set_figwidth(16)\n",
    "figure.set_figheight(9)\n",
    "\n",
    "# Display all the images.\n",
    "for axes in axes_list.ravel():\n",
    "    axes.get_xaxis().set_visible(False)\n",
    "    axes.get_yaxis().set_visible(False)\n",
    "    image_name = deck_of_cards.deal_card().image_name\n",
    "    img = mpimg.imread(str(path.joinpath(image_name).resolve()))\n",
    "    axes.imshow(img)\n",
    "\n",
    "figure.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Shuffle and Re-Deal the Deck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deck_of_cards.shuffle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a separate figure in the notebook.\n",
    "figure, axes_list = plt.subplots(nrows=4, ncols=13)\n",
    "\n",
    "# Increase the total figure size in the notebook.\n",
    "figure.set_figwidth(16)\n",
    "figure.set_figheight(9)\n",
    "\n",
    "# Display all the images after shuffling.\n",
    "for axes in axes_list.ravel():\n",
    "    axes.get_xaxis().set_visible(False)\n",
    "    axes.get_yaxis().set_visible(False)\n",
    "    image_name = deck_of_cards.deal_card().image_name\n",
    "    img = mpimg.imread(str(path.joinpath(image_name).resolve()))\n",
    "    axes.imshow(img)\n",
    "    \n",
    "figure.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deck_of_cards.shuffle()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a separate figure in the notebook.\n",
    "# This time, 2 rows of 5 columns each.\n",
    "figure, axes_list = plt.subplots(nrows=2, ncols=5)\n",
    "\n",
    "# Increase the total figure size in the notebook.\n",
    "figure.set_figwidth(16)\n",
    "figure.set_figheight(9)\n",
    "\n",
    "# Display 10 images after shuffling.\n",
    "for axes in axes_list.ravel():\n",
    "     axes.get_xaxis().set_visible(False)\n",
    "     axes.get_yaxis().set_visible(False)\n",
    "     image_name = deck_of_cards.deal_card().image_name\n",
    "     img = mpimg.imread(str(path.joinpath(image_name).resolve()))\n",
    "     axes.imshow(img)\n",
    "\n",
    "figure.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "##########################################################################\n",
    "# (C) Copyright 2019 by Deitel & Associates, Inc. and                    #\n",
    "# Pearson Education, Inc. All Rights Reserved.                           #\n",
    "#                                                                        #\n",
    "# DISCLAIMER: The authors and publisher of this book have used their     #\n",
    "# best efforts in preparing the book. These efforts include the          #\n",
    "# development, research, and testing of the theories and programs        #\n",
    "# to determine their effectiveness. The authors and publisher make       #\n",
    "# no warranty of any kind, expressed or implied, with regard to these    #\n",
    "# programs or to the documentation contained in these books. The authors #\n",
    "# and publisher shall not be liable in any event for incidental or       #\n",
    "# consequential damages in connection with, or arising out of, the       #\n",
    "# furnishing, performance, or use of these programs.                     #\n",
    "##########################################################################\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Additional material (C) Copyright 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
}
