ENG 202: Computers and Engineering
Object Oriented Programming in PYTHON
LECTURE 12 – Data Visualization in PYTHON
Maurice J. KHABBAZ, Ph.D.
Visualizing Results
• Throughout so many upcoming engineering courses:
• One quantifies system performances using different metrics:
• Examples:
• Electrical Engineering:
• Voltage (current) across (through) a resistive element – Ohm’s Law:
𝑉𝑉 = 𝑅𝑅 × 𝐼𝐼
• 𝑉𝑉 ≜ Voltage ; 𝑅𝑅 ≜ Resistance ; 𝐼𝐼 ≜ Current intensity
• Mechanical Engineering:
• Energy used by a system to do useful work under fixed pressure, temperature and volume:
𝐺𝐺 = 𝑈𝑈 + 𝑝𝑝𝑝𝑝 − 𝑇𝑇𝑇𝑇
• 𝐺𝐺 ≜ Gibbs Free Energy ; 𝑈𝑈 ≜ Internal Energy ; 𝑝𝑝 ≜ Pressure ; 𝑇𝑇 ≜ Temperature ; 𝑆𝑆 ≜ Entropy
• Civil Engineering:
• In Traffic Flow Theory, the average speed of a vehicle navigating along a roadway segment is:
𝜌𝜌
𝑉𝑉 = 𝑉𝑉FF 1 −
𝜌𝜌max
• 𝑉𝑉𝐹𝐹𝐹𝐹 ≜ Free-Flow Speed ; 𝜌𝜌 ≜ Traffic Density ; 𝜌𝜌max ≜ maximum (jamming) traffic density.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 2
Visualizing Results
• Use graphs to provide an intuitive/visual sense of system behavior.
• This is yet another example on leveraging existing libraries
• Rather than writing procedures from scratch.
• PYTHON provides libraries for (among other topics):
• Graphing.
• Numerical Computation.
• Stochastic Computation.
• Explore the usage of existing library procedures for:
• Processing data.
• Exploring data.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 3
Using matplotlib
• Most popular PYTHON library for data visualization.
• Written in PYTHON and makes use of numpy.
• Cross-platform library for making 2D plots from data arrays.
• Provides Object-Oriented Application Programming Interface (OO-API):
• Helps embed plots in applications using Graphical User Interface (GUI) toolkits.
• Can be used in PYTHON and interactive PYTHON (iPython) shells (and others).
• Has a procedural interface called Pylab (designed to resemble MATLAB):
• matplotlib combined with numpy are the open-source equivalent of MATLAB.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 4
Using matplotlib
• matplotlib.pyplot is a collection of command style functions:
• Make matplotlib work like MATLAB.
• Each function:
• Creates a figure.
• Creates a plotting area in the figure.
• Plots some lines in the plotting area.
• Decorates the plot with labels, … etc.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 5
TYPE OF PLOT DESCRIPTION
bar Creates a bar plot
barh Creates a horizontal bar plot
boxplot Creates a box and a whisker plot
hist Plots a histogram
hist2d Plots a 2D histogram
pie Plots a pie chart
Types of Plots plot Plots lines and/or markers on axes
polar Creates a polar plot
scatter Creates a scatter plot of x VS y
stackplot Creates a stacked area plot
stem Creates a stem plot
step Creates a step plot
Friday, January 3, 2020
quiver Plots a 2D field of arrows
NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 6
Image Functions
Function Description
imread Read an image from a file into an array
imsave Save an array as an image file
imshow Display an image on the axes
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 7
Axis Functions
FUNCTION DESCRIPTION
axes Add axes to the figure
text Add text to the axes
title Set a title for the current axes
xlabel Set the x-axis label for the current axes
xlim Get or set the x limits of the current axes
xscale Set the scaling of the x-axis for the current axes
xticks Get or set the x limits of the current tick locations and labels
ylabel Set the y-axis label for the current axes
ylim Get or set the y limits for the current axes
yscale Set the scaling of the y-axis for the current axes
yticks Get or set the y limits of the current tick locations and labels
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 8
Figure Functions
Function Description
figtext Add text to figure
figure Creates a new figure
show Displays a figure
savefig Saves the current figure
close Closes a figure window
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 9
A Simple Plot
• First, need to import library into the computing environment:
import matplotlib as mpl
• Allows referencing any of the library’s functions as: mpl.<funcName>
• Provides access to existing set of graphing/plotting functions.
• This is just a simple example.
• Lots of additional information is available in matplotlib documentation.
• Will go over many other examples in details later in this lecture.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 10
A Simple Plot – Cont’d.
• Basic function to plot lists as x and y values:
• Other data structures are much more powerful.
• Example:
from matplotlib import pyplot as plt
x = []; y = [];
for v in range(31):
x.append(v);
y.append(v);
plt.plot(x, y) Sample Display
plt.show()
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 11
A Simple Example: Adding More Stuff
• Example:
from matplotlib import pyplot as plt
import numpy as np
x = []; yLinear = []; yQuad = [];
yCubic = []; yExp = []
for v in range(10):
x.append(v);
yLinear.append(v); yQuad.append(v**2)
yCubic.append(v**3); yExp.append((np.e)**v)
plt.plot(x, yLinear); plt.plot(x, yQuad)
plt.plot(x, yCubic); plt.plot(x, yExp)
plt.show()
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 12
A Simple Example: Adding More Stuff
Sample Display • Four graphs overlaid.
• Guess red curve is the exponential:
• Grows really much faster than the others.
• Green curve is most likely the cubic one.
• Brown curve is the quadratic one.
• Blue curve is the linear one.
• Brown and Blue curves merely visible:
• This is because of the scale of the y-axis:
for v in range(10):
• y-axis grows to 8000.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 13
Overlapping Displays
• Previous display unhelpful since only the biggest plot is clearly visible:
• Due to the difference in the scales of the different curves.
• Is it possible to graph each function separately?
• Yes, the function plt.figure() makes this possible.
• Syntax:
plt.figure(<arg>)
• Creates a new display with that name if one does not already exist.
• If a display with that name exists, it is reopened for further processing.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 14
Separating Plots
from matplotlib import pyplot as plt
import numpy as np
x = []; yLinear = []; yQuad = [];
yCubic = []; yExp = []
for v in range(10):
x.append(v);
yLinear.append(v); yQuad.append(v**2)
yCubic.append(v**3); yExp.append((np.e)**v)
plt.figure(“Linear”); plt.plot(x, yLinear)
plt.figure(“Quadratic”); plt.plot(x, yQuad)
plt.figure(“Cubic”); plt.plot(x, yCubic);
plt.figure(“Exponential”); plt.plot(x, yExp)
plt.show()
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 15
Separating Plots: Sample
Displays
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 16
Plotting: Adding More and More Stuff
• A professional plot must have well labelled axes:
• Can be done using the plt.xlabel() and plt.ylabel() functions.
• Example:
from matplotlib import pyplot as plt
import numpy as np
x = []; yLinear =
for v in range(10):
x.append(v);
yLinear.append(v);
plt.figure(“Linear”); plt.plot(x, yLinear)
plt.xlabel(“x Values”); plt.ylabel(“Linear Function”)
plt.show()
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 17
Adding
Labels to the
Axes
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 18
Plotting: Adding a Title to The Graph
• A professional plot must have well labelled axes:
• Can be done using the plt.xlabel() and plt.ylabel() functions.
• Example:
from matplotlib import pyplot as plt
import numpy as np
x = []; yLinear =
for v in range(10):
x.append(v);
yLinear.append(v);
plt.figure(“Linear”); plt.plot(x, yLinear)
plt.xlabel(“x Values”); plt.ylabel(“Linear Function”)
plt.title(“Plot of a Linear Function”)
plt.show()
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 19
Adding a
Title to The
Graph
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 20
Cleaning a Figure Window
• This means erasing a figure window with all its custom set properties:
• i.e. axes labels, titles, curve colors, etc.
• This can be done using the plt.clf() function.
• Example:
plt.clf()
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 21
Comparing Results
• Suppose it is required to compare different plots.
• The scales on the graphs are very different.
• Option 1: explicitly set limits on one or both axes.
• Option 2: plot multiple function on the same display.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 22
Option 1: Changing Limits of One or Both Axes
plt.figure(“Linear”); plt.clf()
plt.ylim(0, 1000)
plot(x, yLinear)
plt.title(“Plot of Linear Function”)
plt.figure(“Quadratic”); plt.clf()
plt.ylim(0, 1000)
plt.plot(x, yQuad)
plt.title(“Plot of Quadratic Function”)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 23
Option 1: Sample Display
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 24
Option 2: Overlaying Plots
plt.figure(“LinQuad”); plt.clf()
plt.plot(x, yLinear); plt.plot(x, yQuad)
plt.title(“Linear V.S. Quadratic Functions”)
plt.figure(“CubExp”); plt.clf()
plt.plot(x, yCubic); plt.plot(x, yExp)
plt.plot(“Cubic V.S. Exponential Functions”)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 25
Option 2: Sample Display
• Important message here:
• In comparing, one needs to think about what is
being compared to what.
• Python gives this nice way of visualization.
• Sometimes:
• Like to overlay things on the same window.
• Like to display things side by side.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 26
Plotting: Adding More Documentation
• A legend helps identifying which of overlaying plots is which.
• Example:
plt.figure(“LinQuad”); plt.clf()
plt.plot(x, yLinear, label = “Linear Function”)
plt.plot(x, yQuad, label = “Quadratic Function”)
plt.legend(loc = “upper right”)specify legend location in figure window
plt.title(“Linear V.S. Quadratic Functions”)
plt.figure(“CubExp”); plt.clf()
plt.plot(x, yCubic, label = “Cubic Function”)
plt.plot(x, yExp, label = “Exponential Function”)
plt.legend() not specifying location lets Python decide the best location
plt.plot(“Cubic V.S. Exponential Functions”)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 27
Sample Display With Legend
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 28
Plotting: Controlling Display Parameters
Requirement: control details of the displays themselves.
Examples:
Change color or style Change width Subdividing a figure window
of data sets. of lines or displays. into multiple subplots.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 29
Controlling Display Parameters – Cont’d.
plt.figure(“LinQuad”); plt.clf()
plt.plot(x, yLinear, “b-”, label = “Linear Function”)
plt.plot(x, yQuad, “ro”, label = “Quadratic Function”)
plt.legend(loc = “upper right”)
plt.title(“Linear V.S. Quadratic Functions”)
plt.figure(“CubExp”); plt.clf()
plt.plot(x, yCubic, “g^”, label = “Cubic Function”)
plt.plot(x, yExp, “r--”, label = “Exponential Function”)
plt.legend()
plt.plot(“Cubic V.S. Exponential Functions”)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 30
Changing Data Sample Display
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 31
Changing Line Width
plt.figure(“LinQuad”); plt.clf()
plt.plot(x, yLinear, “b-”, label = “Linear Function”, linewidth = 2.0)
plt.plot(x, yQuad, “ro”, label = “Quadratic Function”, linewidth = 3.0)
plt.legend(loc = “upper right”)
plt.title(“Linear V.S. Quadratic Functions”)
plt.figure(“CubExp”); plt.clf()
plt.plot(x, yCubic, “g^”, label = “Cubic Function”, linewidth = 4.0)
plt.plot(x, yExp, “r--”, label = “Exponential Function”, linewidth = 5.0)
plt.legend()
plt.plot(“Cubic V.S. Exponential Functions”)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 32
Changing Line Width Sample Display
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 33
Plotting: Using Subplots
• Subplotting ⇒ subdividing a figure window into a matrix of figures.
• Syntax:
plt.subplot(xyz)
• where x, y and z are integers designating the following:
• x ≜ number of rows.
• y ≜ number of columns.
• z ≜ number of the figure to be handled.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 34
Example: Using Subplots
plt.figure("LinQuad")
plt.clf()
plt.subplot(211); plt.ylim(0, 80); plt.ylabel("Linear Function")
plt.plot(x, yLinear, "b-", label = "Linear Function", linewidth = 2.0);
plt.subplot(212); plt.ylim(0,80)
plt.ylabel("Quadratic Function"); plt.xlabel("x Values")
plt.plot(x, yQuad, "r", label = "Quadratic Function", linewidth = 3.0)
plt.figure("CubExp")
plt.clf()
plt.subplot(121); plt.ylim(0, 8000);
plt.ylabel("Cubic Function"); plt.xlabel("x Values")
plt.plot(x, yCubic, "g--", label = "Cubic Function", linewidth = 4.0)
plt.subplot(122); plt.ylim(0, 8000)
plt.ylabel("Exponential Function"); plt.xlabel("x Values")
plt.plot(x, yExp, "r", label = "Exponential Function", linewidth = 5.0)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 35
Sample Display Using Subplots
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 36
Plotting: Changing Scales
plt.figure(“CubExp-Y-LogScale")
plt.clf()
plt.plot(x, yCubic, "g--", label = "Cubic Function", linewidth = 4.0)
plt.plot(x, yExp, "r", label = "Exponential Function", linewidth = 5.0)
plt.yscale(“log”)
plt.legend(); plt.title(“Cubic V.S. Exponential Functions (y-Log)”)
plt.xlabel(“x Values”)
plt.figure("CubExp-Y-Linear")
plt.clf()
plt.plot(x, yCubic, "g--", label = "Cubic Function", linewidth = 4.0)
plt.plot(x, yExp, "r", label = "Exponential Function", linewidth = 5.0)
plt.legend(); plt.title(“Cubic V.S. Exponential Function (y-Linear)”)
plt.xlabel(“x Values”)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 37
Sample Display with Changing Scales
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 38
Additional tutorial-oriented documentation is found at:
Very
Useful
Tutorial
https://www.tutorialspoint.com/matplotlib/index.htm
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 39
Practical Example: Analysis V.S. Contribution
• Objectives:
• Illustrate how plotting allows the proper data visualization.
• Show how the visualization of results helps in:
• Guiding computations.
• Determining the values of parameters to pass as inputs to perform computations.
• Enumerate things to explore within/throughout certain computations.
• Problem Statement:
• Imagine planning a retirement.
• Think about ensuring to have enough savings to allow retiring at a certain age:
• Intend to save an amount m each month.
• Expect to earn a percentage r of income on investments each month (i.e. interest)
• Have a look at how a retirement fund grows with compounding of interest:
• Given m and r, determine the time when ready for retirement.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 40
Practical Example: Computing Compound Interest
• Write a function called retire() that takes in three arguments:
• monthly ≜ the amount to put aside each month.
• rate ≜ the rate a which income grows as a result of investments.
• terms ≜ the period of time to do the computation over.
• The function should set up two lists, namely: base and savings
• These are going to be the x and y values.
• The monthly rate is equivalent to the yearly rate divided by twelve.
• Savings at current month are equivalent to:
• The latest accumulated savings to which is accumulated the monthly rate
• Plus the monthly amount to be put aside.
• Finally return the base and savings together in a tuple.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 41
Practical Example: Computing Compound Interest
def retire(monthly, rate, terms):
savings = [0]
base = [0]
monthlyRate = rate / 12.0
for i in range(terms):
base += [i]
savings += [savings[-1] * (1 + monthlyRate) + monthly]
return (base, savings)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 42
Practical Example: Display Results V.S. Months
• Write a function displayResults() that takes three arguments:
• monthlies ≜ a list of trial monthly amounts to put aside in [500; 1100].
• rate ≜ the yearly rate of income as before.
• terms ≜ the number of months to look over, also, as before.
• The function will then plot the different curves that correspond to
each and every trial monthly amount put aside.
• This will allow:
• The comparison of results.
• Determination of best plan to follow given: i) interest and ii) retirement time.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 43
Practical Example: Display Results V.S. Months
def displayResults(monthlies, rate, terms):
plt.figure(“retirementChart”)
plt.clf()
for monthly in monthlies:
(x, y) = retire(monthly, rate, terms)
plt.plot(x, y, label = “Monthly Amount: ” + str(monthly))
plt.legend(loc = “upper left”)
plt.xlabel(“Number of Months”); plt.ylabel(“Retirement Amount”);
plt.show()
# Main Program: # Do not forget from matplotlib import pyplot as plt
displayResults([500, 600, 700, 800, 900, 1000, 1100], 0.05, 40 * 12)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 44
Practical
Example:
Sample
Display
• Can see the impact of increasing monthly contribution
45 • Retirement amount ranges from $750K to $1.67M as monthly savings
ranges from $500 to $1100.
• Question: What is the effect of rate of growth of investments?
NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. Friday, January 3, 2020
Practical Example: Display Results V.S. Rate
• Modify displayResults() to take in a:
• Fixed monthly saving amount.
• List of different income rates.
• Do the plotting to visualize the variations of the retirement amount as
a result of varying the income rates.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 46
Practical Example: Display Results V.S. Rate
def displayResults(monthly, rates, terms):
plt.figure(“retirementChart”)
plt.clf()
for rate in rates:
(x, y) = retire(monthly, rate, terms)
plt.plot(x, y, label = “Monthly Amount: ” + str(monthly) +
“, Rate: ” + str(int(rate * 100)) + “%”)
plt.legend(loc = “upper left”)
plt.xlabel(“Number of Months”); plt.ylabel(“Retirement Amount”);
plt.show()
# Main Program: # Do not forget from matplotlib import pyplot as plt
displayResults(800, [0.03, 0.05, 0.07], 40 * 12)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 47
Practical
Example:
Sample
Display
• Varying growth of investment return rate spreads the graphs more rapidly.
48
• Aggressive growth (optimistic 7%) leads to better amounts upon retirement.
• Retirement amount ∈ [$600K; $2.1M] as rate grows in [3%; 7%].
• Question: What if change affects both monthly savings and rate together?
NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. Friday, January 3, 2020
Practical Example: Display Results V.S. Both
def displayResults(monthlies, rates, terms):
plt.figure(“retirementChart”)
plt.clf(); plt.xlim(30 * 12, 40 * 12)
for monthly in monthlies:
for rate in rates:
(x, y) = retire(monthly, rate, terms)
plt.plot(x, y, label = “Monthly Amount: ” + str(monthly) +
“, Rate: ” + str(int(rate * 100)) + “%”)
plt.legend(loc = “upper left”)
plt.xlabel(“Number of Months”); plt.ylabel(“Retirement Amount”);
plt.show()
# Main Program: # Do not forget from matplotlib import pyplot as plt
displayResults([500, 700, 900], [0.03, 0.05, 0.07], 40 * 12)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 49
Practical
Example:
Sample
Display
• Labels are kind of helpful in telling what is going on.
• Zooming in on just the last ten years is valuable as it spreads out curves.
50 •
•
A bit hard to figure out which of these graphs corresponds to which color.
Need to fix this to really see more appropriately the comparison.
• Objective: How to use visualization to help decide how to think about computation?
NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. Friday, January 3, 2020
Practical
Example:
Sample
Display
• Too much overlay of graphs that does not allow distinguishing among them.
51
• Question: How to separate those out into pieces that help think better?
• Maybe analyze them separately: be careful about visualizing different effects.
• Do that as follows on the next slide.
NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. Friday, January 3, 2020
Practical Example: Display Results V.S. Both
def displayResults(monthlies, rates, terms):
plt.figure(“retirementChart”)
plt.clf(); plt.xlim(30 * 12, 40 * 12)
monthLabels = [“r”, “b”, “g”, “k”]; rateLabels = [“-”, “o”, “--”]
for i in range(len(monthlies)):
monthly = monthlies[i]; monthLabel = monthLabels[i % len(monthLabels)]
for j in range(len(rates)):
rate = rates[j]; rateLabel = rateLabels[j % len(rateLabels)]
(x, y) = retire(monthly, rate, terms)
plt.plot(x, y, monthLabel + rateLabel, label = “Monthly Amount: ” + str(monthly) +
“, Rate: ” + str(int(rate * 100)) + “%”)
plt.legend(loc = “upper left”)
plt.xlabel(“Number of Months”); plt.ylabel(“Retirement Amount”);
plt.show()
# Main Program: # Do not forget from matplotlib import pyplot as plt
displayResults([500, 700, 900], [0.03, 0.05, 0.07], 40 * 12)
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 52
Practical
Example:
Sample
Display
• Notice there are now only three different colors and styles.
• Red is all $500, Blue is all $700 and Green is all $900.
53 • The color indicates very nicely the monthly savings amount.
• The style (i.e. solid line, circle, dashed line) indicates the return rate.
• There is still a bit overlay but now graphs are much easier to read.
NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. Friday, January 3, 2020
Practical
Example:
Sample
Display
• The four dashed lines are much better than the slant of the four solid lines.
• Objective realized: visualize and use visualization to guide computation.
54 • Start saving early for retirement to get wonderful compound interest.
• Much better than relying on getting 25% return each year.
• Do not invest in a Ponzi scheme as this is not a good idea.
NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. Friday, January 3, 2020
Summary
• Now easier to see grouping plots:
• Color encodes monthly contributions.
• Format/style encodes growth rate of investments.
• Interaction with plotting routines and computations allows data exploration:
• Change display range to zoom in on particular areas of interest.
• Change value sets and visualize effect – then guide new choice of values to explore.
• Change display parameters to highlight clustering of plots by parameter.
• There are still so many other examples about using plotting to visualize data.
• Example of using libraries:
• All what has been done throughout this lecture is usage of a set of functions.
• These functions are already present in matplotlib.
• That makes it possible to rely on somebody having written code that can be reused.
• Libraries are tools that will be used a lot as one moves on with own computation.
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 55
Friday, January 3, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 56