CS1-FULL-FALL2021
What’s next?
HW6 (due tomorrow)
Quiz Week 14 (this Friday)
Quiz Week 15 (next Friday)
Final – 12/16 – 4PM (Zoom cameras on!!!)
And what about HW7?
Not enough time to have a meaningful HW
But if you are worried about grades, you will be able
to re-submit your worse HW, but the grade of
resubmission you have be worth 80%
Remember the initial survey you done? Now, if you
really do it (you will have 0.2 in final grade – 1 point in
midterm)
Matplotlib
• Matplotlib is a library to build 2D graphs
• It is a very powerful library. https://matplotlib.org/
https://matplotlib.org/
Matplotlib
• To use it you need to import Matplotlib and Numpy
import matplotlib.pyplot as plt
import numpy as np
How to use Matplotlib
1 – A graph is a figure in the drawing area. You can
have multiple figures in a program
plt.figure()
default argument is empty, but you use a
number, a string, etc. as the input
How to use Matplotlib
2 – Plot the data using a plot type of your choice (line
graph, bar chart, histogram, pie chart, etc)
plt.plot(np.array([4,6,3,10,7]))
# use line graph here
How to use Matplotlib
3 – Add title, labels, tick marks, legends, etc.
plt.xlabel(‘input parameter (seconds)’ )
# label the x axis
How to use Matplotlib
4 – Display the plot either to an interactive window or
a save on a file
plt.show() # draw to a window
plt.savefig(‘myplot.png’) # save to a file
Matplotlib
• plt.axis() – axis.
• plt.subplot() – graph inside a graph
• plt.show()
• plt.clf() – clear the window
Matplotlib
• Some of the Matplotlib graphs we will see:
• Arbitrary points graph
• Function graphs
• Bar graphs
• Histograms
• Pie charts
Array generation functions
• There are some functions that can help to generate automatic arrays.
• arange(max): creates an array with valued
arange(10) = [0 1 2 3 4 5 6 7 8 9]
• arange(min, max, step): creates an array with valued
arange(2,10,2) = [2 4 6 8]
• linspace(min, max, n) creates an array with n values divided
equally between min and max (both are included)
linspace(2,50, 10) =
[ 2. 7.33333333 12.66666667 18. 23.33333333
28.66666667 34. 39.33333333 44.66666667 50. ]
Arbitrary points
• Data sets are required for the abscissa and the ordinate.
• A list (or array) for the abscissa.
• Another list (or array) for the ordinate.
• In addition, you can indicate the format of the points and the
line (colors, thickness, shape, etc.).
• The points can be plotted in a continuous line. The solid line
follows the order of the points. The line can intersect itself.
The values in the ordinate are specified in the
list’values’. On the abscissa the values are
self-generated, from 0 to 20
(there are21 values in the ‘values’ list)
We see that the points are
plotted in the order in which
their coordinates are in the
lists cx and cy. Therefore, the
curve can cross itself.