ENG 202: Computers and Engineering Object Oriented Programming in PYTHON
ENG 202: Computers and Engineering Object Oriented Programming in PYTHON
• 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.
Function Description
Function Description
• 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.
plt.clf()
plt.figure(“CubExp”); plt.clf()
plt.plot(x, yCubic); plt.plot(x, yExp)
plt.plot(“Cubic V.S. Exponential 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
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”)
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”)
• Syntax:
plt.subplot(xyz)
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
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”)
Very
Useful
Tutorial
https://www.tutorialspoint.com/matplotlib/index.htm
• 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?
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?
• 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.
• Notice there are now only three different colors and styles.
• Red is all $500, Blue is all $700 and Green is all $900.
• The four dashed lines are much better than the slant of the four solid lines.
• Objective realized: visualize and use visualization to guide computation.