Card
Images with Matplotlib¶Card
imageshttps://commons.wikimedia.org/wiki/Category:SVG_English_pattern_playing_cards
ch10
examples folder’s card_images
subfolderfrom deck import DeckOfCards
deck_of_cards = DeckOfCards()
inline
required only in Jupyter%matplotlib inline
Path
for Each Image¶pathlib
module’s Path
class to construct the full path to each image on our systemPath
method joinpath
appends the subfolder containing the card images to the path for the current folder (.
)from pathlib import Path
path = Path('.').joinpath('card_images')
matplotlib.image
to load the imagesimport matplotlib.pyplot as plt
import matplotlib.image as mpimg
Figure
and Axes
Objects¶NOTE: In Jupyter, all code that modifies the on-screen presentation of a Figure
must be in one cell, so we combined several cells below
The first statement in the following cell uses Matplotlib function subplots
to create a Figure
object in which we’ll display the images as 52 subplots with four rows (nrows
) and 13 columns (ncols
)
Figure
and an array of the subplots’ Axes
objectsfigure, axes_list = plt.subplots(nrows=4, ncols=13)
# added next two statements to increase figure size in notebook
figure.set_figwidth(16)
figure.set_figheight(9)
for axes in axes_list.ravel():
axes.get_xaxis().set_visible(False)
axes.get_yaxis().set_visible(False)
image_name = deck_of_cards.deal_card().image_name
img = mpimg.imread(str(path.joinpath(image_name).resolve()))
axes.imshow(img)
figure.tight_layout()
Axes
Objects and Display the Images¶Axes
objects in axes_list
ravel
provides a one-dimensional view of a multidimensional arrayAxes
object,Card
and get its image_name
matplotlib.image
module’s imread
function to load the imageAxes
method imshow
to display the current image in the current subplotFigure
object’s tight_layout
method removes most of the extra white space in the windowdeck_of_cards.shuffle()
# added this statement to create a separate figure in the notebook
figure, axes_list = plt.subplots(nrows=4, ncols=13)
# added next two statements to increase figure size in notebook
figure.set_figwidth(16)
figure.set_figheight(9)
for axes in axes_list.ravel():
axes.get_xaxis().set_visible(False)
axes.get_yaxis().set_visible(False)
image_name = deck_of_cards.deal_card().image_name
img = mpimg.imread(str(path.joinpath(image_name).resolve()))
axes.imshow(img)
# added this statement for execution in the notebook
figure.tight_layout()
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.