-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplots.py
508 lines (453 loc) · 19.9 KB
/
plots.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from seaborn import palettes
from seaborn.utils import ci
import funcs
import utils
# import dependencies
import pandas as pd
from pandas.core.frame import DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
import re
import wordcloud
def plot_qa(df_null):
"""Plot Count of Nulls of columns with nulls.
The plot is done for columns with nulls in Dataframe.
Parameters
----------
df : pd.Dataframe
Dataframe to check and plot for nulls.
Returns
-------
Seaborn.barplot
"""
null_columns = ['Feature', 'DataType', 'CountOfNonNulls', 'CountOfNulls',\
'PercentOfNullsInColumn', 'PercentOfNullsInData']
df_null_cols = df_null.columns
# check if columns match
mem_cols = all(col in null_columns for col in df_null_cols)
if mem_cols:
if df_null['CountOfNulls'].sum() == 0:
print("There are zero nulls in the DataFrame.")
print("No plot to display!")
else:
null_df = df_null.loc[df_null['CountOfNulls'] > 0]
null_df.reset_index(drop = True, inplace = True)
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 10)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
bar = sns.barplot(y = 'PercentOfNullsInColumn', x = 'Feature' , data = null_df, ci = False)
for i in range(len(null_df)):
bar.text(i, null_df['PercentOfNullsInColumn'][i] + 0.25, str(round(null_df['PercentOfNullsInColumn'][i], 2)),
fontdict= dict(color = 'blue', fontsize = 10, horizontalalignment = 'center'))
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('Percentage of Nulls in Column')
plt.show()
else:
raise utils.UnexpectedDataFrame(df_null)
def plot_qa_mtpltlib(df_null):
"""Plot Count of Nulls of columns with nulls.
The plot is done for columns with nulls in Dataframe.
Parameters
----------
df : pd.Dataframe
Dataframe to check and plot for nulls.
Returns
-------
matplotlib.pyplot.barplot
"""
null_columns = ['Feature', 'DataType', 'CountOfNonNulls', 'CountOfNulls',\
'PercentOfNullsInColumn', 'PercentOfNullsInData']
df_null_cols = df_null.columns
# check if columns match
mem_cols = all(col in null_columns for col in df_null_cols)
if mem_cols:
if df_null['CountOfNulls'].sum() == 0:
print("There are zero nulls in the DataFrame.")
print("No plot to display!")
else:
null_df = df_null.loc[df_null['CountOfNulls'] > 0]
null_df.reset_index(drop = True, inplace = True)
plt.figure(figsize=(15, 8))
plt.bar('Feature', 'CountOfNulls', data = df_null, color = 'orange', width = 0.9, align = 'center', edgecolor = 'blue')
# i = 1.0
# j = 2000
# for i in range(len(null_df)):
# plt.annotate(null_df['PercentOfNullsInColumn'][i], (-0.1 + i, null_df['PercentOfNullsInColumn'][i] + j))
plt.xticks(rotation = 90)
plt.xlabel("Columns")
plt.ylabel("Percentage")
plt.title("Count of Nulls in Column")
plt.show()
else:
raise utils.UnexpectedDataFrame(df_null)
def plot_unique_vals_count(df):
"""Plot count of unique values per column.
The plot is done for unique values of all columns.
Parameters
----------
df : Dataframe
Dataframe to check for unique values per column.
Returns
-------
seaborn.barplot
"""
unique_vals = funcs.unique_vals_counts(df)
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 12)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
bar = sns.barplot(y = 'count', x = 'column' , data = unique_vals)
for i in range(len(unique_vals)):
bar.text(i, unique_vals['count'][i] + 0.25, str(unique_vals['count'][i]),
fontdict= dict(color = 'blue', fontsize = 10, horizontalalignment = 'center'))
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('Count of Unique Values per Column')
plt.show()
def plot_unique_vals_column(df, col, normalize = False):
"""Plot value counts in a column.
Value counts are calculated for a single column and plotted.
Parameters
----------
df : DataFrame
Dataframe containing column to check for unique values.
col : str
Name of column to check for unique values
normalized : bool, optional
If true this function normalizes the counts.
(Default value = False)
Returns
-------
seaborn.barplot
"""
if normalize:
unique_col_vals = funcs.unique_vals_column(df, col, normalize = True)
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 12)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.barplot(x = 'percentOfTotal', y = col , data = unique_col_vals)
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('Percentage of Unique Values in {}'.format(col))
plt.show()
else:
unique_col_vals = funcs.unique_vals_column(df, col, normalize = False)
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 12)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.barplot(x = 'count', y = col , data = unique_col_vals)
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('Count of Unique Values in {}'.format(col))
plt.show()
def count_plot(df, col, **hue):
"""Plot value counts in a column.
Value counts are calculated for a single column and plotted.
Parameters
----------
df : DataFrame
Dataframe containing column to check for unique values.
col : str
Name of column to check for unique values.
**hue: dict
Keyword arguments.
Returns
-------
seaborn.countplot
"""
var = hue.get('var', None)
if isinstance(df, pd.DataFrame):
df_cols = df.columns.tolist()
if col in df_cols and not var:
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.countplot(y = df[col], order=df[col].value_counts(ascending=False).index, ax=ax)
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('Count of {}'.format(col))
plt.savefig('images/{}.png'.format(col))
plt.show()
elif col in df_cols and var:
if var in df_cols:
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.countplot(y = df[col], order=df[col].value_counts(ascending=False).index, hue = df[var], ax=ax)
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('{} vs {}'.format(col.title(), var.title()))
plt.savefig('images/{}v{}.png'.format(col, var))
plt.show()
else:
raise utils.InvalidColumn(var)
else:
raise utils.InvalidColumn(col)
else:
raise utils.InvalidDataFrame(df)
def bar_plot(df, col_x, col_y):
if isinstance(df, pd.DataFrame):
df_cols = df.columns.tolist()
if col_x in df_cols:
if col_y in df_cols:
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 12)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
bar = sns.barplot(x = col_x, y = col_y, data = df, ci = None, ax=ax)
# for i in range(len(df)):
# bar.text(i, df[col_y][i] + 0.25, str(round(df[col_y][i], 2)),
# fontdict= dict(color = 'blue', fontsize = 10, horizontalalignment = 'center'))
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('Rank of {} by {}'.format(col_y, col_x))
plt.savefig('images/{}_{}.png'.format(col_x, col_y))
plt.show()
else:
raise utils.InvalidColumn(col_y)
else:
raise utils.InvalidColumn(col_x)
else:
raise utils.InvalidDataFrame(df)
def pie_plot(df, col):
"""Plot pie plot of values in a column.
Percentage of values counts are calculated for a single column and plotted.
Parameters
----------
df : DataFrame
Dataframe containing column to check for unique values.
col : str
Name of column to plot.
Returns
-------
pandas.DataFrame.plot.pie
"""
if isinstance(df, pd.DataFrame):
df_cols = df.columns.tolist()
if col in df_cols:
pie_data = df[col].value_counts().reset_index()
pie_data.set_index('index', inplace=True)
if len(pie_data) > 5:
print("{} contains more than 5 values.".format(col))
print("Visualization best practices recommends using a barplot for variables with more than 5 unique values.")
elif len(pie_data) <= 5:
pie_data.plot.pie(y=col, autopct='%0.1f%%', figsize=(10, 10))
plt.savefig('images/{}.png'.format(col))
plt.title('{} Composition'.format(col.title()))
plt.show()
else:
raise utils.InvalidColumn(col)
else:
raise utils.InvalidDataFrame(df)
def hist_distribution(df, col, bins = 30, kde = False):
"""Plot distribution of values in a column.
Histogram with kde(kernel density estimate) of values in a numerical column are plotted.
Parameters
----------
df : DataFrame
Dataframe containing column to check for unique values.
col : str
Name of column to plot.
bins : integer, optional
Number of bins in distribution.
(Default value = 30)
kde : boolean, optional
If true, a kde(kernel density estimate) plot is included in histogram.
(Default value = False)
Returns
-------
seaborn.histplot
"""
if isinstance(df, pd.DataFrame):
df_cols = df.columns.tolist()
if col in df_cols:
if df[col].dtypes == 'int64' or df[col].dtypes == 'int32' or df[col].dtypes == 'float64':
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.histplot(df[col], bins=bins, kde=kde, ax=ax, color="orange")
plt.setp(ax.get_xticklabels(), rotation=90)
plt.title('Distribution of {}'.format(col.title()))
plt.savefig('images/{}_distribution.png'.format(col))
plt.show()
else:
raise utils.InvalidDataType(col)
else:
raise utils.InvalidColumn(col)
else:
raise utils.InvalidDataFrame(df)
def box_plot(df, col, y = None):
if isinstance(df, pd.DataFrame):
df_cols = df.columns.tolist()
if col in df_cols:
if not y:
if df[col].dtypes == 'int64' or df[col].dtypes == 'int32' or df[col].dtypes == 'float64':
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.boxplot(x = df[col], color = 'green')
plt.setp(ax.get_xticklabels(), rotation=0)
plt.title('Box Distribution of {}'.format(col.title()))
plt.savefig('images/{}_distribution.png'.format(col))
plt.show()
else:
raise utils.InvalidDataType(col)
if y:
if y in df_cols:
if df[y].dtypes == 'object':
fig, ax = plt.subplots()
# the size of A4 paper lanscape
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.boxplot(x = df[col], y = df[y])
plt.setp(ax.get_xticklabels(), rotation=0)
plt.title('{} Distribution by {}'.format(col.title(), y.title()))
plt.savefig('images/{}_{}_distribution.png'.format(col,y))
plt.show()
else:
raise utils.InvalidDataType(y)
else:
raise utils.InvalidColumn(y)
else:
raise utils.InvalidColumn(col)
def line_plot(df, x, y, hue = None):
if isinstance(df, pd.DataFrame):
df_cols = df.columns.tolist()
if x in df_cols:
if y in df_cols:
if hue:
if hue in df_cols:
fig, ax = plt.subplots()
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.lineplot(x = x, y = y, data = df, hue = hue, ci = False)
plt.setp(ax.get_xticklabels(), rotation = 90)
plt.title('Time Series of Price of BMW Used Car by {}'.format(hue.title()))
plt.savefig('images/tmseriesn.png')
plt.show()
else:
raise utils.InvalidColumn(hue)
elif not hue:
fig, ax = plt.subplots()
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.lineplot(x = x, y = y, data = df, ci = False)
plt.setp(ax.get_xticklabels(), rotation = 90)
plt.title('Time Series of Price of BMW used car')
plt.savefig('images/tmseriesn.png')
plt.show()
else:
raise utils.InvalidColumn(y)
else:
raise utils.InvalidColumn(x)
else:
raise utils.InvalidDataFrame(df)
def lineplot_by_unique_val(df, col, col_val, hue = None):
if isinstance(df, pd.DataFrame):
df_cols = df.columns.tolist()
if col in df_cols:
uniques = funcs.unique_vals_column(df, col, normalize = False)
if col_val in uniques[col].tolist():
df_col_val = df.loc[df[col] == col_val]
if hue:
if hue in df_cols:
fig, ax = plt.subplots()
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.lineplot(x = 'year', y = 'price', data = df_col_val, hue = hue, ci = False)
plt.setp(ax.get_xticklabels(), rotation = 90)
plt.title('Time Series of Price of {} {} {}'.format(col_val.title(), col.title(), hue.title()))
plt.savefig('images/{}_{}_tmseries.png'.format(col, col_val))
plt.show()
else:
raise utils.InvalidColumn(hue)
if not hue:
fig, ax = plt.subplots()
fig.set_size_inches(15, 8)
sns.set_context("poster", font_scale = .6, rc={"grid.linewidth": 0.6})
sns.lineplot(x = 'year', y = 'price', data = df_col_val, ci = False)
plt.setp(ax.get_xticklabels(), rotation = 90)
plt.title('Time Series of Price of {} {}'.format(col_val.title(), col.title()))
plt.savefig('images/{}_{}_tmseries.png')
plt.show()
else:
raise utils.InvalidColumn(col_val)
else:
raise utils.InvalidColumn(col)
else:
raise utils.InvalidColumn(df)
def corr_heatmap(df, **cols_to_drop):
"""Plot Correlation heatmap.
Parameters
----------
df : DataFrame
DataFrame with numerical values to make Correlation Heatmap
Returns
-------
seaborn.heatmap
"""
cols_drop = cols_to_drop.get('cols_drop', None)
if isinstance(df, pd.DataFrame):
df = df.copy()
df_cols = df.columns.tolist()
if not cols_drop:
fig, ax = plt.subplots()
fig.set_size_inches(15, 8)
mask = np.triu(np.ones_like(df.corr(), dtype = bool))
heatmap = sns.heatmap(df.corr(), mask = mask, vmin = -1, vmax = 1, annot =True, cmap = 'GnBu')
heatmap.set_title("Correlation Heatmap of BMW Sales", fontdict = {'fontsize': 16}, pad = 15)
plt.setp(ax.get_xticklabels(), rotation = 90)
plt.setp(ax.get_yticklabels(), rotation = 0)
# plt.savefig("images/dfcorr.png")
plt.show()
if cols_drop:
if isinstance(cols_drop, list):
if len(cols_drop) == 1:
if cols_drop[0] in df_cols:
df.drop(cols_drop[0], inplace=True, axis=1)
fig, ax = plt.subplots()
fig.set_size_inches(15, 8)
mask = np.triu(np.ones_like(df.corr(), dtype = bool))
heatmap = sns.heatmap(df.corr(), mask = mask, vmin = -1, vmax = 1, annot =True, cmap = 'Spectral')
heatmap.set_title("Correlation Heatmap of BMW Sales", fontdict = {'fontsize': 16}, pad = 15)
plt.setp(ax.get_xticklabels(), rotation = 90)
plt.setp(ax.get_yticklabels(), rotation = 0)
# plt.savefig("images/dfcorr.png")
plt.show()
else:
raise utils.InvalidColumn(cols_drop[0])
elif len(cols_drop) > 1:
col_mems = all(col in df_cols for col in cols_drop)
if col_mems:
df.drop(cols_drop, inplace=True, axis=1)
fig, ax = plt.subplots()
fig.set_size_inches(15, 8)
mask = np.triu(np.ones_like(df.corr(), dtype = bool))
heatmap = sns.heatmap(df.corr(), mask = mask, vmin = -1, vmax = 1, annot =True, cmap = 'PuOr')
heatmap.set_title("Correlation Heatmap of BMW Sales", fontdict = {'fontsize': 16}, pad = 15)
plt.setp(ax.get_xticklabels(), rotation = 90)
plt.setp(ax.get_yticklabels(), rotation = 0)
# plt.savefig("images/dfcorr.png")
plt.show()
else:
non_cols = []
for col in cols_drop:
if col not in df_cols:
raise utils.InvalidColumn(col)
else:
raise utils.InvalidDataStructure(cols_drop)
else:
raise utils.InvalidDataFrame(df)
def rank_feature(df, col, measure, n = None):
# group data by col and agg by measure
df_grp = df.groupby(col, as_index = False)[measure].mean()
sort_df = df_grp.sort_values(by = [measure], ascending = False)
if n:
if type(n) == int:
sort_df = sort_df.iloc[:n, :]
else:
raise utils.InvalidDataType(n)
return bar_plot(sort_df, measure, col)