Skip to content

Commit a2ee71b

Browse files
committed
Add Lecture 9
1 parent 0749e20 commit a2ee71b

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed

res/.DS_Store

0 Bytes
Binary file not shown.

res/lecture-code/lecture-9.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env python3
2+
"""
3+
File: lecture-9.py
4+
------------------
5+
6+
Code from CS 41: Lecture 9 (Matplotlib).
7+
8+
Authors
9+
-------
10+
Parth Sarin (@psarin)
11+
12+
History
13+
-------
14+
3-1-2020 @psarin Created file
15+
"""
16+
import matplotlib.pyplot as plt
17+
import numpy as np
18+
19+
def simple_plot():
20+
"""
21+
Makes a simple matplotlib plot, which is the segment of y=x from x=1, ..., 4
22+
"""
23+
plt.plot([1, 2, 3, 4])
24+
plt.ylabel('some numbers')
25+
plt.show()
26+
27+
28+
def plot_customizations():
29+
"""
30+
Some simple customizations to the line plot.
31+
"""
32+
# Specify x and y coordinates to plot
33+
xs = [1, 2, 3, 4]
34+
ys = [1, 4, 9, 16]
35+
plt.plot(xs, ys)
36+
plt.show()
37+
38+
# Change the style of the plot
39+
plt.plot(xs, ys, 'ro')
40+
plt.show()
41+
42+
# Use numpy!
43+
t = np.arange(0., 5., 0.2)
44+
# Red dashes, blue squares and green triangles
45+
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
46+
plt.show()
47+
48+
49+
def multiple_subplots():
50+
"""
51+
Plot multiple plots in the same figure.
52+
"""
53+
def f(t):
54+
return np.exp(-t) * np.cos(2*np.pi*t)
55+
56+
t1 = np.arange(0.0, 5.0, 0.1)
57+
t2 = np.arange(0.0, 5.0, 0.02)
58+
59+
plt.figure()
60+
plt.subplot(211)
61+
plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
62+
63+
plt.subplot(212)
64+
plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
65+
plt.show()
66+
67+
68+
def histogram():
69+
"""
70+
Plot a histogram.
71+
"""
72+
# Fixing random state for reproducibility
73+
np.random.seed(20081203)
74+
75+
mu, sigma = 100, 15
76+
x = mu + sigma * np.random.randn(10000)
77+
78+
plt.hist(x, 50) # data, number of bins
79+
plt.show()
80+
81+
82+
def scatter_plot():
83+
"""
84+
Plot a scatter plot.
85+
"""
86+
# Fixing random state for reproducibility
87+
np.random.seed(20081203)
88+
89+
N = 50
90+
x = np.random.rand(N) # x-coordinates
91+
y = np.random.rand(N) # y-coordinates
92+
colors = np.random.rand(N) # color spectra values
93+
area = (30 * np.random.rand(N))**2 # radii for points
94+
alpha = 0.5 # transparency
95+
96+
plt.scatter(x, y, s=area, c=colors, alpha=alpha)
97+
plt.show()
98+
99+
100+
def main():
101+
simple_plot()
102+
plot_customizations()
103+
multiple_subplots()
104+
histogram()
105+
scatter_plot()
106+
107+
if __name__ == '__main__':
108+
main()

res/lectures.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"active": false,
7272
"code": "https://stanfordpython.com/res/lecture-code/Lecture5.zip"
7373
},
74-
{
74+
{
7575
"uid": "7ec7caac-ee4f-434c-9c13-d8f869d9590b",
7676
"title": "NumPy",
7777
"date": "2020-02-10",
@@ -83,7 +83,7 @@
8383
"active": false,
8484
"code": "#"
8585
},
86-
{
86+
{
8787
"uid": "86d9817e-e471-4f32-a79e-aa88b307c54b",
8888
"title": "Standard Library",
8989
"date": "2020-02-19",
@@ -95,7 +95,7 @@
9595
"active": false,
9696
"code": "https://stanfordpython.com/res/lecture-code/Lecture7.zip"
9797
},
98-
{
98+
{
9999
"uid": "161d41b6-b91f-4711-8805-53e827fa2a79",
100100
"title": "Machine Learning",
101101
"date": "2020-02-24",
@@ -104,7 +104,19 @@
104104
"video": "https://drive.google.com/open?id=1V_9xmICwer1U5WlxMIzoY9EO3_it425y",
105105
"video_note": "#",
106106
"visible": true,
107+
"active": false,
108+
"code": "https://stanfordpython.com/res/lecture-code/Lecture8.zip"
109+
},
110+
{
111+
"uid": "fbb1d737-46b3-4ca5-b2ee-339cf2edfcfb",
112+
"title": "Matplotlib",
113+
"date": "2020-03-02",
114+
"condensed": "https://drive.google.com/file/d/1xcndwb2Bf6dS_BFE1bygfa7bOnbqMWQ4/view?usp=sharing",
115+
"full": "https://drive.google.com/file/d/1cBMfE6I_-UEc9IdsTjiOJLnK562vHIjG/view?usp=sharing",
116+
"video": "#",
117+
"video_note": "#",
118+
"visible": true,
107119
"active": true,
108-
"code": "https://stanfordpython.com/res/lecture-code/Lecture9.zip"
120+
"code": "https://stanfordpython.com/res/lecture-code/lecture-9.py"
109121
}
110122
]

0 commit comments

Comments
 (0)