-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_vis.py
490 lines (364 loc) · 12 KB
/
data_vis.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
from matplotlib import pyplot as plt
from prettytable import PrettyTable
# * Seaborn dependencies
import pandas as pd
import numpy as np
import seaborn as sns
# * Settings
show_matplotlib = False
show_seaborn = True
if (show_matplotlib == True):
#?############
#* HISTOGRAM #
#?############
# values on the y axis
y = [14, 3, 6, 9]
# plotting histogram
plt.hist(y)
# showing plot
plt.show()
#?##########
#* BOX PLOT#
#?##########
# values on the y axis
y = [14, 3, 6, 9]
# plotting histogram
plt.boxplot(y)
# showing plot
plt.show()
#?###########
#* Bar Plot #
#?###########
# values on the x axis
x = [5, 2, 9, 4, 7]
# values on the y axis
y = [14, 3, 6, 9, 1]
# plotting bar
plt.bar(x, y)
# showing plot
plt.show()
#?############
#* Line Plot #
#?############
# values on the x axis
x = [5, 2, 9, 4, 7]
# values on the y axis
y = [14, 3, 6, 9, 1]
# plotting line
plt.plot(x, y)
# showing plot
plt.show()
#?############
#* Pie Chart #
#?############
# The labels are the names for the parts
label_set = '7', '6', '8+', '5-'
# we give each label a size
sizes = [20, 45, 15, 20]
# we use explode to show a particular slide
explode = (0, 0, 0, 0.1)
# subplots
fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=label_set,
autopct='%1.1f%%', shadow=True, startangle=90)
# make sure its a circle
ax1.axis('equal')
# showing plot
plt.show()
#?###############
#* Scatter Plot #
#?###############
# values on the x axis
x = [5, 2, 9, 4, 7, 6, 3, 1, 4, 11, 4, 4, 5]
# values on the y axis
y = [14, 3, 6, 9, 1, 3, 1, 2, 7, 8, 3, 4, 11]
# make the actual scatter plot
plt.scatter(x, y)
# showing plot
plt.show()
#?###################
#* Plot Customizing #
#?###################
# values on the x axis
x = [5, 2, 9, 4, 7, 6, 3, 1, 4, 11, 4, 4, 5]
# values on the y axis
y = [14, 3, 6, 9, 1, 3, 1, 2, 7, 8, 3, 4, 11]
# values on the x axis of second set
x2 = [14, 3, 6, 9, 1, 3, 1, 2, 7, 8, 3, 4, 11]
# values on the y axis of second set
y2 = [5, 2, 9, 4, 7, 6, 3, 1, 4, 11, 4, 4, 5]
# adjust the size of the plot
plt.figure(figsize=(12, 8))
# make the actual scatter plot
plt.scatter(x, y, label="first set")
# we do the scattering again to plot 2 different sets inside the same plot
# Note that we used alpha to chagne the opacity of the plot
plt.scatter(x2, y2, alpha=0.5, label="second set")
# adjust the x and y label (side text)
plt.xlabel('X coordinate')
plt.ylabel('Y coordinate')
# adjust the plot title
plt.title("2D map of x and y coordinates")
# add a legend #?(depends on label in .scatter)
plt.legend()
# showing plot
plt.show()
#?####################
#* Creating Subplots #
#?####################
# values on the x axis
x = [5, 2, 9, 4, 7, 6, 3, 1, 4, 11, 4, 4, 5]
# values on the y axis
y = [14, 3, 6, 9, 1, 3, 1, 2, 7, 8, 3, 4, 11]
# values on the x axis of second set
x2 = [14, 3, 6, 9, 1, 3, 1, 2, 7, 8, 3, 4, 11]
# values on the y axis of second set
y2 = [5, 2, 9, 4, 7, 6, 3, 1, 4, 11, 4, 4, 5]
# alternative way to chance the size of the plot
fig = plt.figure(figsize=(12, 8))
# we create a sublpot
ax1 = plt.subplot2grid((2, 2), (0, 0))
# make the actual scatter plot
ax1 = plt.scatter(x, y, label="first set")
# adjust the x and y label (side text)
ax1 = plt.xlabel('X coordinate')
ax1 = plt.ylabel('Y coordinate')
# adjust the plot title
ax1 = plt.title("2D map of x and y coordinates")
# we create a sublpot
ax2 = plt.subplot2grid((2, 2), (0, 1))
# make the actual scatter plot
ax2 = plt.scatter(x2, y2, label="second set")
# adjust the x and y label (side text)
ax2 = plt.xlabel('X coordinate')
ax2 = plt.ylabel('Y coordinate')
# adjust the plot title
ax2 = plt.title("2D map of x and y coordinates")
# make histogram for ax2
ax2 = plt.subplot2grid((2, 2), (1, 0), colspan=2)
ax2 = plt.hist(y2)
# showcase result
fig.tight_layout()
#?##############
#* prettytable #
#?##############
# specifying column names
mytable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
# adding rows
mytable.add_row(["Serra", "X", "B", "91.2%"])
mytable.add_row(["Penny", "B", "C", "61.2%"])
mytable.add_row(["Erik", "B", "B", "90.2%"])
mytable.add_row(["Josso", "X", "A+", "100%"])
mytable.add_row(["Luka", "X", "B", "88.1%"])
# output
print(mytable)
#?###################
#* testing comments #
#?###################
# the following section is for testing better comments
# TODO: Implement x and y
# ? This is a query
# * this is hightlighted, way better then the stupid plain default comments
# ! this is very important
if (show_seaborn == True):
#?###################
#* Seaborn data Vis #
#?###################
# set the style of sns
sns.set_style('darkgrid')
"""
* This are possible options to use for sns.set_style()
? darkgrid
? whitegrid
? dark
? white
? ticks
"""
# load the student database we use for this examples
df = pd.read_csv('data.csv')
df.head()
#?###############
#* Scatter plot #
#?###############
# do scatter with as input x and y coordinates
sns.scatterplot(x=df['math score'], y=df['reading score'])
# show the actual plot
plt.show()
"""
* We can implement this using multiple ways:
? plt.figure(figsize = (9,6))
? sns.scatterplot(x = 'math score', y = 'reading score',data = df)
"""
#?#######################
#* Scatter plot extended#
#?#######################
# set the plot figure size
plt.figure(figsize=(9, 6))
# plot data labels x and y, data df, differentiate in gender and use alpha for opacity.
sns.scatterplot(x='math score',
y='reading score',
hue='gender',
data=df,
alpha=0.8
)
# show the actual plot
plt.show()
#?############
#* Count Plot#
#?############
# set the plot figure size
plt.figure(figsize=(9, 6))
# plot countplot with x label and data df
sns.countplot(x='race/ethnicity',
data=df
)
# show the actual plot
plt.show()
#?###############
#* Distance Plot#
#?###############
# set the plot figure size
plt.figure(figsize=(9, 6))
# plot distance plot with x data df, and kde false (don't show kde of the distribution)
sns.distplot(x=df['math score'], kde=False)
# show the actual plot
plt.show()
#?##############
#* KDE plotting#
#?##############
# * KDE stands for Kernal Density Estimate, more information about it is available here:
# ? https://mathisonian.github.io/kde/
# set the plot figure size
plt.figure(figsize=(9, 6))
# Do a kernal density plot with data math score
sns.kdeplot(x=df['math score'])
# show the actual plot
plt.show()
#?#########################
#* Regression scatter plot#
#?#########################
# set the plot figure size
plt.figure(figsize=(9, 6))
# this creates a scatter plot with a regression line. the regression line tells about the relationship between two points
# x data = df(math score), y data = df(reading score), the scatter is set to pink, the line to red, we use somehow similar colours
sns.regplot(x=df['math score'],
y=df['reading score'],
scatter_kws={'color': 'pink'},
line_kws={'color': 'red'}
)
# show the actual plot
plt.show()
#?################################
#* Subset regresiion scatter plot#
#?################################
# do a plot with x data, y data, making a gender based differentiaton
# this method automaticly makes a regression line for every subset in the plot
sns.lmplot(x='math score',
y='reading score',
hue='gender',
data=df
)
# show the actual plot
plt.show()
#?#################
#* Paired plotting#
#?#################
# * This kind of plot is somewhat harder --> what does it do?
# ? It pairwise plots distrutions of a dataset with different methods
# ? It does this in numeric columns
# ? The input is the subsets of the data with columns you want to plot distributions in between
# We do a pairplot with 3 different inputs (should result in 9 outputs)
sns.pairplot(df[['math score',
'reading score',
'writing score']]
)
# show the actual plot
plt.show()
#?#######################################
#* univariate and bivariate distribution#
#?#######################################
# * This plot also pluts univariate and bivariate distributions at the top and side
# ? the distribution density for x and y if you want to put it different
# We do a jointplot with 2 different inputs from df
sns.jointplot(x='math score',
y='reading score',
data=df
)
# show the actual plot
plt.show()
#! This method works better when using hue
# ? This because this way it shows differences and overlaps
# We do a jointplot with 2 different inputs from df, differentiating on gender
sns.jointplot(x='math score',
y='reading score',
hue='gender',
data=df
)
# show the actual plot
plt.show()
#?#############
#* Boxplotting#
#?#############
# A boxplot is an excellent way to show data spread and data outliers of a dataset
# * Single Boxplot:
# We do a boxplot with as data math score from df
sns.boxplot(x='math score',
data=df
)
# Show the actual boxplot
plt.show()
# ? Multi Boxplot:
# We do a boxplot for all numerical collumns inside the dataset
sns.boxplot(data=df)
# Show the actual boxplot
plt.show()
#?################
#* Swarm Plotting#
#?################
# * This plots a scatterplots per category with its points being non overlapping
# ? Swarm plots form good complement to box or violin plots
# We set the figure size
plt.figure(figsize=(12, 8))
# We do a swarm plot with x label and y label, data df and an opacity of 0.8
sns.swarmplot(x='race/ethnicity',
y='math score',
data=df,
alpha=0.8
)
# Show the actual plot
plt.show()
#?##################
#* Heatmap Plotting#
#?##################
# * This plot plots a heatmap of the input data, this is most commonly used for correlation heatmaps
# ? annot=True enables displaying the value of the cells, setting it to false would disable this
# We do a heatmap plot with correlation of the input data, annot true and add an inferno bar to it
sns.heatmap(df.corr(),
annot=True,
cmap='inferno')
# Show the actual plot
plt.show()
#?###################
#* Colour adjustment#
#?###################
"""
*plt.figure(figsize = (12,8))
*
? colors = [
? '#F8D030',
? '#E0C068',
? '#EE99AC',
? '#C03028',
? '#F85888'
? ]
*
* sns.stripplot(x = 'race/ethnicity',
* y = 'math score',
* data = df,
* palette = colors
* )
*
* plt.show()
"""
#! For even more ways to plot data, make sure to check out the following url:
# * http://seaborn.pydata.org/examples/