-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathplotting-the-ecdf.py
More file actions
54 lines (41 loc) · 1.73 KB
/
plotting-the-ecdf.py
File metadata and controls
54 lines (41 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'''
Plotting the ECDF
100xp
You will now use your ecdf() function to compute the ECDF for the petal lengths of
Anderson's Iris versicolor flowers. You will then plot the ECDF. Recall that your ecdf()
function returns two arrays so you will need to unpack them. An example of such unpacking
is x, y = foo(data), for some function foo().
Instructions
-Use ecdf() to compute the ECDF of versicolor_petal_length. Unpack the output into x_vers and y_vers.
-Plot the ECDF as dots. Remember to include marker = '.' and linestyle = 'none' in addition to x_vers and y_vers as arguments inside plt.plot().
-Set the margins of the plot with plt.margins() so that no data points are cut off. Use a 2% margin.
-Label the axes. You can label the y-axis 'ECDF'.
-Show your plot.
'''
import numpy as np
import matplotlib.pyplot as plt
versicolor_petal_length = np.array([ 4.7, 4.5, 4.9, 4. , 4.6, 4.5, 4.7, 3.3, 4.6, 3.9, 3.5,
4.2, 4. , 4.7, 3.6, 4.4, 4.5, 4.1, 4.5, 3.9, 4.8, 4. ,
4.9, 4.7, 4.3, 4.4, 4.8, 5. , 4.5, 3.5, 3.8, 3.7, 3.9,
5.1, 4.5, 4.5, 4.7, 4.4, 4.1, 4. , 4.4, 4.6, 4. , 3.3,
4.2, 4.2, 4.2, 4.3, 3. , 4.1])
def ecdf(data):
"""Compute ECDF for a one-dimensional array of measurements."""
# Number of data points: n
n = len(data)
# x-data for the ECDF: x
x = np.sort(data)
# y-data for the ECDF: y
y = np.arange(1, n+1) / n
return x, y
# Compute ECDF for versicolor data: x_vers, y_vers
x_vers, y_vers = ecdf(versicolor_petal_length)
# Generate plot
_ = plt.plot(x_vers, y_vers, marker = '.', linestyle = 'none')
# Make the margins nice
plt.margins(0.02)
# Label the axes
_ = plt.xlabel('length')
_ = plt.ylabel('ECDF')
# Display the plot
plt.show()