Instructor Note: This notebook's code has been organized into cells differently than the snippets presented in the book. In a notebook, all the code that affects the visualization's appearance must appear in the same cell. Any code that modifies that appearance would have to re-display the visualization. For this reason, snippet numbers in this notebook do not match with the snippet numbers in the book.



ipython --matplotlib
Note: %matplotlib inline is an IPython magic that enables Matplotlib-based graphics to be displayed directly in the notebook. We've separated by two blank lines the snippets that were combined into a single cell.
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import random
import seaborn as sns
matplotlib.pyplot contains the Matplotlib library’s graphing capabilities that we use. This module typically is imported with the name plt. unique that we’ll use to summarize the die rolls. The numpy module typically is imported as np. random contains Python’s random-number generation functions.seaborn contains the Seaborn library’s graphing capabilities we use. This module typically is imported with the name sns. rolls = [random.randrange(1, 7) for i in range(600)]
unique function expects an ndarray argument and returns an ndarray. ndarray for better performance. return_counts=True tells unique to count each unique value’s number of occurrencesunique returns a tuple of two one-dimensional ndarrays containing the sorted unique values and their corresponding frequencies, respectively. values, frequencies = np.unique(rolls, return_counts=True)
title = f'Rolling a Six-Sided Die {len(rolls):,} Times'
sns.set_style('whitegrid') # default is white with no grid
# create and display the bar plot
axes = sns.barplot(x=values, y=frequencies, palette='bright')
# set the title of the plot
axes.set_title(title)
# label the axes
axes.set(xlabel='Die Value', ylabel='Frequency')
# scale the y-axis to add room for text above bars
axes.set_ylim(top=max(frequencies) * 1.10)
# create and display the text for each bar
for bar, frequency in zip(axes.patches, frequencies):
text_x = bar.get_x() + bar.get_width() / 2.0
text_y = bar.get_height()
text = f'{frequency:,}\n{frequency / len(rolls):.3%}'
axes.text(text_x, text_y, text,
fontsize=11, ha='center', va='bottom')
# plt.cla()
# We placed this code in a comment because it was meant for use
# in an interactive IPython session in which we clear the window,
# then display a new graph in it. In a notebook, we can simply
# display a new graph inline.
When you execute the next cell, the notebook will add another cell below it containing the code in Snippet 5. You should then change 600 to 60000.
%recall 5
rolls = [random.randrange(1, 7) for i in range(600)]
When you execute the next cell, the notebook will add another cell below it containing the code in Snippets 6-7. Executing that cell will produce a new graph.
%recall 6-7
values, frequencies = np.unique(rolls, return_counts=True)
title = f'Rolling a Six-Sided Die {len(rolls):,} Times'
sns.set_style('whitegrid') # default is white with no grid
# create and display the bar plot
axes = sns.barplot(x=values, y=frequencies, palette='bright')
# set the title of the plot
axes.set_title(title)
# label the axes
axes.set(xlabel='Die Value', ylabel='Frequency')
# scale the y-axis to add room for text above bars
axes.set_ylim(top=max(frequencies) * 1.10)
# create and display the text for each bar
for bar, frequency in zip(axes.patches, frequencies):
text_x = bar.get_x() + bar.get_width() / 2.0
text_y = bar.get_height()
text = f'{frequency:,}\n{frequency / len(rolls):.3%}'
axes.text(text_x, text_y, text,
fontsize=11, ha='center', va='bottom')
%save RollDie.py 1-7
# plt.cla()
# We placed this code in a comment because it was meant for use
# in an interactive IPython session in which we clear the window,
# then display a new graph in it. In a notebook, we can simply
# display a new graph inline.
RollDie.py file you saved above. We added comments and a two modifications so you can run the script with an argument that specifies the number of die rolls, as in:
ipython RollDieWithArg.py 600
sys module enables a script to receive command-line arguments that are passed into the program.
sys module’s argv list contains the arguments. show function, which displays the window containing the graph:plt.show()
run RollDieWithArg.py 6000
©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.