-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_visualization.py
190 lines (127 loc) · 3.81 KB
/
4_visualization.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# VISUALIZATION IN PYTHON
# Matplotlib is the most famous package
# matplotlib.pyplot - diverse plotting functions
import matplotlib.pyplot as plt
# plt.plot() - takes arguments that describe the data to be plotted
# plt.show() - displays plot to screen
# Plotting with pyplot
import matplotlib.pyplot as plt
plt.plot(months, prices)
plt.show()
# Customizing the Plot
import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red')
plt.show()
import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')
plt.show()
# Adding Labels and Titles
import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')
plt.show()
# Add Labels
plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')
# Show Plot
plt.show()
# Adding Additional Lines
import matplotlib.pyplot as plt
plt.plot(months, prices, color = 'red', linestyle = '--')
# adding additional line
plt.plot(months, prices_new, color = 'green', linestyle = '--')
plt.xlabel('Months')
plt.ylabel('Consumer Price Indexes, $')
plt.title('Average Monthly Consumer Price Indexes')
plt.show()
# Scatterplots
import matplotlib.pyplot as plt
plt.scatter(x = months, y = prices, color = 'red')
plt.show()
######################################
# EXERCISE
# Importing matplotlib and pyplot
# Import matplotlib.pyplot with the alias plt
import matplotlib.pyplot as plt
# Plot the price of stock over time
plt.plot(days, prices, color="red", linestyle="--")
# Display the plot
plt.show()
# Adding axis labels and titles
import matplotlib.pyplot as plt
# Plot price as a function of time
plt.plot(days, prices, color="red", linestyle="--")
# Add x and y labels
plt.xlabel('Days')
plt.ylabel('Prices, $')
# Add plot title
plt.title('Company Stock Prices Over Time')
# Show plot
plt.show()
# Multiple lines on the same plot
# Plot two lines of varying colors
plt.plot(days, prices1, color='red')
plt.plot(days, prices2, color='green')
# Add labels
plt.xlabel('Days')
plt.ylabel('Prices, $')
plt.title('Stock Prices Over Time')
plt.show()
# Scatterplots
# Import pyplot as plt
import matplotlib.pyplot as plt
# Plot price as a function of time
plt.scatter(days, prices, color='green', s=0.1)
# Show plot
plt.show()
######################################
# HISTOGRAMS
# Common graph used to display data as they tell the distribution of the data
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=3)
plt.show()
# When there are more bins, the number of obs in each bin decreases
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6)
plt.show()
# Normalizing histogram data
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1)
plt.show()
# Layering histograms on a plot
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1)
plt.hist(x=prices_new, bins=6, normed=1)
plt.show()
# Alpha: Changing the transparency of histograms
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5)
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5)
plt.show()
# Adding a legend
import matplotlib.pyplot as plt
plt.hist(x=prices, bins=6, normed=1, alpha=0.5, label='Prices 1')
plt.hist(x=prices_new, bins=6, normed=1, alpha=0.5, label='Prices New')
plt.show()
######################################
# EXERCISES
## Is data normally distributed?
# Plot histogram
plt.hist(prices, bins=100)
# Display plot
plt.show()
## Comparing two histograms
# Plot histogram of stocks_A
plt.hist(stock_A, bins=100, alpha=0.4)
# Plot histogram of stocks_B
plt.hist(stock_B, bins=100, alpha=0.4)
# Display plot
plt.show()
## Adding a legend
# Plot stock_A and stock_B histograms
plt.hist(stock_A, bins=100, alpha=0.4, label='Stock A')
plt.hist(stock_B, bins=100, alpha=0.4, label='Stock B')
# Add the legend
plt.legend()
# Display the plot
plt.show()