import matplotlib.pyplot as plt
= [1, 4, 9, 16, 25]
squares
# Create a figure and axis
= plt.subplots()
fig, ax
# Plot the squares with a blue line
=3, marker='o', linestyle='--')
ax.plot(squares, linewidth
# Customize the plot
'Square Numbers')
ax.set_title('Index')
ax.set_xlabel('Value')
ax.set_ylabel(True)
ax.grid(
# Show the plot
plt.show()
Learning outcomes
- Generate data sets and create visualizations
- Create simple plots with Matplotlib and use a scatter plot to explore random walks
- Create a histogram with Plotly and use a histogram to explore the results of rolling dice of different sizes
Plotting tools used
- Matplotlib- mathematical plotting library
- Plotly- visualizations which work with digital devices.
Plotting a line graph
Correcting the plot
import matplotlib.pyplot as plt
= [1,2,3,4,5] #adding this would fix it
input_values = [1, 4, 9, 16, 25]
squares
# Create a figure and axis
= plt.subplots()
fig, ax
# Plot the squares with a blue line
=3, marker='o', linestyle='--')
ax.plot(input_values, squares, linewidth
# Customize the plot
'Square Numbers')
ax.set_title('Index')
ax.set_xlabel('Value')
ax.set_ylabel(True)
ax.grid(
# Show the plot
plt.show()
Using built-in Styles
import matplotlib.pyplot as plt
plt.style.available
['Solarize_Light2',
'_classic_test_patch',
'_mpl-gallery',
'_mpl-gallery-nogrid',
'bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn-v0_8',
'seaborn-v0_8-bright',
'seaborn-v0_8-colorblind',
'seaborn-v0_8-dark',
'seaborn-v0_8-dark-palette',
'seaborn-v0_8-darkgrid',
'seaborn-v0_8-deep',
'seaborn-v0_8-muted',
'seaborn-v0_8-notebook',
'seaborn-v0_8-paper',
'seaborn-v0_8-pastel',
'seaborn-v0_8-poster',
'seaborn-v0_8-talk',
'seaborn-v0_8-ticks',
'seaborn-v0_8-white',
'seaborn-v0_8-whitegrid',
'tableau-colorblind10']
# using style
import matplotlib.pyplot as plt
= [1,2,3,4,5] #adding this would fix it
input_values = [1, 4, 9, 16, 25]
squares
#use style
'fast')
plt.style.use(
# Create a figure and axis
= plt.subplots()
fig, ax
# Plot the squares with a blue line
=3, marker='o', linestyle='--')
ax.plot(input_values, squares, linewidth
# Customize the plot
'Square Numbers')
ax.set_title('Index')
ax.set_xlabel('Value')
ax.set_ylabel(True)
ax.grid(
# Show the plot
plt.show()
Plotting and Styling Individual Points with scatter()
# using style
import matplotlib.pyplot as plt
= [1,2,3,4,5] #adding this would fix it
input_values = [1, 4, 9, 16, 25]
squares
#use style
'fast')
plt.style.use(
# Create a figure and axis
= plt.subplots()
fig, ax
# Plot the squares with a blue line
2,4,s=200)
ax.scatter(
# Customize the plot
'Square Numbers')
ax.set_title('Index')
ax.set_xlabel('Value')
ax.set_ylabel(True)
ax.grid(
# Show the plot
plt.show()
Caluculating data automatically
= range(1,1001)
x_values = [x**2 for x in x_values]
y_values
'fast')
plt.style.use(= plt.subplots()
fig, ax = 10)
ax.scatter(x_values, y_values, s
# Customize the plot
'Square Numbers')
ax.set_title('Index')
ax.set_xlabel('Value')
ax.set_ylabel(False)
ax.grid(
#Set the range for each axis
0, 1100, 0, 1100000])
ax.axis([
plt.show()
Using a Colormap
import matplotlib.pyplot as plt
= range(1, 1000)
x_values = [x**2 for x in x_values]
y_values = plt.subplots()
fig, ax = 10)
ax.scatter(x_values, y_values, s
= y_values, cmap= plt.cm.Blues, s=10)
ax.scatter(x_values, y_values, c
# Customize the plot
'Square Numbers')
ax.set_title('Index')
ax.set_xlabel('Value')
ax.set_ylabel(False)
ax.grid(
plt.show()
Saving the plots automatically
'squares_plot.png', bbox_inches= 'tight') #second argument trims extra white space plt.savefig(
<Figure size 640x480 with 0 Axes>
Example
1. plot for first five cubic numbers.
2. plot for first 5000 cubic numbers.
import matplotlib.pyplot as plt
# Function to calculate the cube of a number
def cube(x):
return x**3
# Generate the first five cubic numbers
= [cube(x) for x in range(1, 6)]
first_five_cubic
# Generate the first 5000 cubic numbers
= [cube(x) for x in range(1, 5001)]
first_5000_cubic
# Plot the first five cubic numbers
1)
plt.figure(range(1, 6), first_five_cubic, marker='o', linestyle='-', color='b')
plt.plot("First Five Cubic Numbers")
plt.title("Number")
plt.xlabel("Cubic Value")
plt.ylabel(
# Plot the first 5000 cubic numbers
2)
plt.figure(range(1, 5001), first_5000_cubic, color='r')
plt.plot("First 5000 Cubic Numbers")
plt.title("Number")
plt.xlabel("Cubic Value")
plt.ylabel(
# Show the plots
plt.show()
Random walks (creating and plotting)
Creating
import random
class RandomWalk:
def __init__(self, num_points=5000):
self.num_points = num_points
self.x_values = [0]
self.y_values = [0]
def fill_walk(self):
while len(self.x_values) < self.num_points:
= random.choice([-1, 1]) * random.choice([0, 1, 2, 3, 4])
x_step = random.choice([-1, 1]) * random.choice([0, 1, 2, 3, 4])
y_step
if x_step == 0 and y_step == 0:
continue
= self.x_values[-1] + x_step
x = self.y_values[-1] + y_step
y
self.x_values.append(x)
self.y_values.append(y)
Plotting
= RandomWalk()
rw
rw.fill_walk()
'fast')
plt.style.use(= plt.subplots()
fig, ax
=15)
ax.scatter(rw.x_values, rw.y_values, s plt.show()
Generating Multiple Random Walks
# just by wrapping the above code in a while loop
while True:
= RandomWalk()
rw
rw.fill_walk()
'fast')
plt.style.use(= plt.subplots()
fig, ax
=15)
ax.scatter(rw.x_values, rw.y_values, s
plt.show()
= input("Make another walk? (y/n): ")
keep_running if keep_running == 'n':
break
Make another walk? (y/n): y
Make another walk? (y/n): n
Styling the walk
- after generating the list using range() function, we stored them in point_numbers()
- then passing the point_numbers to c argument, we used
colormap - finally, pass edgecolors = ‘none’ to get rid of black outline.
while True:
= RandomWalk()
rw
rw.fill_walk()
'fast')
plt.style.use(= plt.subplots()
fig, ax = range(rw.num_points) # added here to style
point_numbers
= point_numbers, cmap= plt.cm.Blues, edgecolors= 'none', s=15)
ax.scatter(rw.x_values, rw.y_values, c
plt.show()
= input("Make another walk? (y/n): ")
keep_running if keep_running == 'n':
break
Make another walk? (y/n): y
Make another walk? (y/n): n
Plotting the starting and ending points
- to see where the walk begins and where it ends (we add first and last points)
while True:
= RandomWalk()
rw
rw.fill_walk()
'fast')
plt.style.use(= plt.subplots()
fig, ax
= range(rw.num_points) # added here to style
point_numbers
= point_numbers, cmap= plt.cm.Blues, edgecolors= 'none', s=15)
ax.scatter(rw.x_values, rw.y_values, c
plt.show()
# Emphasize the first and last points.
0, 0, c='green', edgecolors='none', s=100)
ax.scatter(-1], rw.y_values[-1], c='red', edgecolors='none',
ax.scatter(rw.x_values[=100)
s
= input("Make another walk? (y/n): ")
keep_running if keep_running == 'n':
break
Removing the Axes
while True:
= RandomWalk()
rw
rw.fill_walk()
'fast')
plt.style.use(= plt.subplots()
fig, ax
= range(rw.num_points) # added here to style
point_numbers
= point_numbers, cmap= plt.cm.Blues, edgecolors= 'none', s=15)
ax.scatter(rw.x_values, rw.y_values, c
plt.show()
# Remove the axes..
False)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(
= input("Make another walk? (y/n): ")
keep_running if keep_running == 'n':
break
Make another walk? (y/n): n
Altering the size to fit screen
while True:
= RandomWalk(50_000)
rw
rw.fill_walk()
'fast')
plt.style.use(= plt.subplots(figsize=(15,9), dpi=128) #here size and if pixels are know too!
fig, ax
= range(rw.num_points) # added here to style
point_numbers
= point_numbers, cmap= plt.cm.Blues, edgecolors= 'none', s=15)
ax.scatter(rw.x_values, rw.y_values, c
plt.show()
# Remove the axes..
False)
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(
= input("Make another walk? (y/n): ")
keep_running if keep_running == 'n':
break
Make another walk? (y/n): n
Rolling dice with Plotly
from random import randint
class Die:
"defining method"
def __init__(self, num_sides=6):
self.num_sides = num_sides
def roll(self):
return randint(1, self.num_sides)
= Die()
die
# make some rolls and store results in the list
= []
results for roll_num in range(100):
= die.roll()
result
results.append(result)
print(results)
[2, 6, 3, 3, 5, 6, 2, 2, 4, 1, 3, 4, 3, 1, 5, 2, 3, 5, 1, 6, 3, 1, 2, 6, 1, 1, 3, 4, 3, 2, 1, 6, 3, 2, 6, 3, 2, 2, 3, 4, 5, 5, 1, 3, 6, 3, 5, 5, 1, 3, 3, 6, 3, 4, 2, 1, 2, 1, 5, 3, 5, 6, 3, 4, 5, 3, 6, 3, 6, 3, 3, 2, 2, 4, 2, 6, 1, 6, 3, 2, 1, 6, 1, 4, 3, 2, 5, 5, 5, 1, 3, 5, 4, 2, 3, 5, 6, 5, 3, 3]
Analyzing the results
= []
frequencies for value in range(1, die.num_sides+1):
= results.count(value)
frequency
frequencies.append(frequency)
print(frequencies)
[20, 11, 24, 15, 17, 13]
# printing frequencies for 1000 rolls
for roll_num in range(1000):
= die.roll()
result
results.append(result)
= []
frequenciesfor value in range(1, die.num_sides+1):
= results.count(value)
frequency
frequencies.append(frequency)
print(frequencies)
[351, 314, 348, 355, 366, 366]
Histogram
from plotly.graph_objs import Bar, Layout
from plotly import offline
= list(range(1, die.num_sides+1))
x_values = [Bar(x=x_values, y=frequencies)]
data
= {'title': 'Result'}
x_axis_config = {'title': 'Frequency of Result'}
y_axis_config
= Layout(title='Results of rolling 1000 times',
my_layout = x_axis_config, yaxis= y_axis_config)
xaxis
'data': data, 'layout': my_layout}, filename = 'd6.html') offline.plot({
'd6.html'
Rolling two die
from plotly.graph_objs import Bar, Layout
from plotly import offline
# creating
= Die()
die_1 = Die()
die_2
= []
results_2for roll_num in range(1000):
= die_1.roll() + die_2.roll()
result
results_2.append(result)
# analyzing
= []
frequencies_2 = die_1.num_sides + die_2.num_sides #here aswell
max_result for value in range(2, max_result+1):
= results_2.count(value)
frequency
frequencies_2.append(frequency)
# Visualizing
= list(range(2, max_result+1)) #changed here
x_values = [Bar(x= x_values, y = frequencies_2)]
data
= {'title': 'Result', 'dtick' : 1} #changed here compared to one die
x_axis_config = {'title': 'Frequency of Result'}
y_axis_config
= Layout(title='Results of rolling two D6 dies 1000 times',
my_layout = x_axis_config, yaxis= y_axis_config)
xaxis
'data': data, 'layout': my_layout}, filename = 'd6_d6.html') offline.plot({
'd6_d6.html'
Rolling two die of different sizes
from plotly.graph_objs import Bar, Layout
from plotly import offline
# creating
= Die()
die_1 = Die(10) #change here
die_2
= []
results_2for roll_num in range(1000):
= die_1.roll() + die_2.roll()
result
results_2.append(result)
# analyzing
= []
frequencies_2 = die_1.num_sides + die_2.num_sides #here aswell
max_result for value in range(2, max_result+1):
= results_2.count(value)
frequency
frequencies_2.append(frequency)
# Visualizing
= list(range(2, max_result+1)) #changed here
x_values = [Bar(x= x_values, y = frequencies_2)]
data
= {'title': 'Result', 'dtick' : 1} #changed here compared to one die
x_axis_config = {'title': 'Frequency of Result'}
y_axis_config
= Layout(title='Results of rolling two D6 dies 1000 times',
my_layout = x_axis_config, yaxis= y_axis_config)
xaxis
'data': data, 'layout': my_layout}, filename = 'd6_d10.html') offline.plot({
'd6_d10.html'
Rolling three dice
from plotly.graph_objs import Bar, Layout
from plotly import offline
# Creating
= Die()
die_1 = Die()
die_2 = Die() #change here
die_3
= []
results_3for roll_num in range(1000):
= die_1.roll() + die_2.roll() + die_3.roll() #die added
result
results_3.append(result)
# Analyzing
= []
frequencies_3 = die_1.num_sides + die_2.num_sides + die_3.num_sides #here aswell
max_result for value in range(2, max_result+1):
= results_3.count(value)
frequency
frequencies_3.append(frequency)
# Visualizing
= list(range(3, max_result+1)) #range changed
x_values = [Bar(x= x_values, y = frequencies_3)]
data
= {'title': 'Result', 'dtick' : 1} #changed here compared to one die
x_axis_config = {'title': 'Frequency of Result'}
y_axis_config
= Layout(title='Results of rolling three D6 dies 1000 times',
my_layout = x_axis_config, yaxis= y_axis_config)
xaxis
'data': data, 'layout': my_layout}, filename = 'd6_d6_d6.html') offline.plot({
'd6_d6_d6.html'