-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14 - Intermediate Data Visualization with Seaborn
649 lines (505 loc) · 14.7 KB
/
14 - Intermediate Data Visualization with Seaborn
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//^//^//^//^//^//^//^//^//^ Seaborn Introduction ^\\^\\^\\^\\^\\^\\^\\^\\^\\
Reading a csv file
--------------------------------------------------
# import all modules
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
# Read in the DataFrame
df = pd.read_csv(grant_file)
Comparing a histogram and displot
--------------------------------------------------
# Display pandas histogram
df['Award_Amount'].plot.hist()
plt.show()
# Clear out the pandas histogram
plt.clf()
---------
# Display a Seaborn distplot
sns.displot(df['Award_Amount'])
plt.show()
# Clear the distplot
plt.clf()
Plot a histogram
--------------------------------------------------
# Create a distplot
sns.displot(df['Award_Amount'],
kde=False,
bins=20)
# Display the plot
plt.show()
Rug plot and kde shading
--------------------------------------------------
# Create a distplot of the Award Amount
sns.displot(df['Award_Amount'],kind = 'kde', fill = True,
rug=True)
# Plot the results
plt.show()
Create a regression plot
--------------------------------------------------
# Create a regression plot of premiums vs. insurance_losses
sns.regplot(x="insurance_losses", y="premiums", data=df)
# Display the plot
plt.show()
---------
# Create an lmplot of premiums vs. insurance_losses
sns.lmplot(x="insurance_losses", y="premiums", data=df)
# Display the second plot
plt.show()
Plotting multiple variables
--------------------------------------------------
# Create a regression plot using hue
sns.lmplot(data=df,
x="insurance_losses",
y="premiums",
hue="Region")
# Show the results
plt.show()
Facetting multiple regressions
--------------------------------------------------
# Create a regression plot with multiple rows
sns.lmplot(data=df,
x="insurance_losses",
y="premiums",
hue="Region",row='Region')
# Show the plot
plt.show()
//^//^//^//^//^//^//^//^//^ Customizing Seaborn Plots ^\\^\\^\\^\\^\\^\\^\\^\\^\\
Setting the default style
--------------------------------------------------
# Plot the pandas histogram
df['fmr_2'].plot.hist()
plt.show()
plt.clf()
# Set the default seaborn style
sns.set()
# Plot the pandas histogram again
df['fmr_2'].plot.hist()
plt.show()
plt.clf()
Comparing styles
--------------------------------------------------
sns.displot(df['fmr_2'])
sns.set_style('dark')
plt.show()
plt.clf()
---------
sns.displot(df['fmr_2'])
sns.set_style('whitegrid')
plt.show()
plt.clf()
Removing spines
--------------------------------------------------
# Set the style to white
sns.set_style('white')
# Create a regression plot
sns.lmplot(data=df,
x='pop2010',
y='fmr_2')
# Remove the spines
sns.despine()
# Show the plot and clear the figure
plt.show()
plt.clf()
Matplotlib color codes
--------------------------------------------------
# Set style, enable color code, and create a magenta displot
sns.set(color_codes = True)
sns.displot(df['fmr_3'], color='m')
# Show the plot
plt.show()
Using default palettes
--------------------------------------------------
# Loop through differences between bright and colorblind palettes
for p in ['bright', 'colorblind']:
sns.set_palette(p)
sns.displot(df['fmr_3'])
plt.show()
# Clear the plots
plt.clf()
Creating Custom Palettes
--------------------------------------------------
sns.palplot(sns.color_palette("Purples", 8))
plt.show()
---------
sns.palplot(sns.color_palette('husl', 10))
plt.show()
---------
sns.palplot(sns.color_palette('coolwarm', 6))
plt.show()
Using matplotlib axes
--------------------------------------------------
# Create a figure and axes
fig, ax = plt.subplots()
# Plot the distribution of data
sns.histplot(df['fmr_3'], ax=ax)
# Create a more descriptive x axis label
ax.set(xlabel="3 Bedroom Fair Market Rent")
# Show the plot
plt.show()
Additional plot customizations
--------------------------------------------------
# Create a figure and axes
fig, ax = plt.subplots()
# Plot the distribution of 1 bedroom rents
sns.histplot(df['fmr_1'], ax=ax)
# Modify the properties of the plot
ax.set(xlabel="1 Bedroom Fair Market Rent",
xlim=(100,1500),
label="US Rent")
# Display the plot
plt.show()
Adding annotations
--------------------------------------------------
# Create a figure and axes. Then plot the data
fig, ax = plt.subplots()
sns.histplot(df['fmr_1'], ax=ax)
# Customize the labels and limits
ax.set(xlabel="1 Bedroom Fair Market Rent", xlim=(100,1500), title="US Rent")
# Add vertical lines for the median and mean
ax.axvline(x=df['fmr_1'].median(), color='m', label='Median', linestyle='--', linewidth=2)
ax.axvline(x=df['fmr_1'].mean(), color='b', label='Mean', linestyle='-', linewidth=2)
# Show the legend and plot the data
ax.legend()
plt.show()
Multiple plots
--------------------------------------------------
# Create a plot with 1 row and 2 columns that share the y axis label
fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, sharey=True)
# Plot the distribution of 1 bedroom apartments on ax0
sns.histplot(df['fmr_1'], ax=ax0)
ax0.set(xlabel="1 Bedroom Fair Market Rent", xlim=(100,1500))
# Plot the distribution of 2 bedroom apartments on ax1
sns.histplot(df['fmr_2'], ax=ax1)
ax1.set(xlabel="2 Bedroom Fair Market Rent", xlim=(100,1500))
# Display the plot
plt.show()
//^//^//^//^//^//^//^//^//^ Additional Plot Types ^\\^\\^\\^\\^\\^\\^\\^\\^\\
stripplot() and swarmplot()
--------------------------------------------------
# Create the stripplot
sns.stripplot(data=df,
x='Award_Amount',
y='Model Selected',
jitter=True)
plt.show()
---------
# Create and display a swarmplot with hue set to the Region
sns.swarmplot(data=df,
x='Award_Amount',
y='Model Selected',
hue='Region')
plt.show()
boxplots, violinplots and boxenplots
--------------------------------------------------
# Create a boxplot
sns.boxplot(data=df,
x='Award_Amount',
y='Model Selected')
plt.show()
plt.clf()
---------
# Create a violinplot with the husl palette
sns.violinplot(data=df,
x='Award_Amount',
y='Model Selected',
palette='husl')
plt.show()
plt.clf()
---------
# Create a boxenplot with the Paired palette and the Region column as the hue
sns.boxenplot(data=df,
x='Award_Amount',
y='Model Selected',
palette='Paired',
hue='Region')
plt.show()
plt.clf()
barplots, pointplots and countplots
--------------------------------------------------
# Show a countplot with the number of models used with each region a different color
sns.countplot(data=df,
y="Model Selected",
hue="Region")
plt.show()
plt.clf()
---------
# Create a pointplot and include the capsize in order to show caps on the error bars
sns.pointplot(data=df,
y='Award_Amount',
x='Model Selected',
capsize=.1)
plt.show()
plt.clf()
---------
# Create a barplot with each Region shown as a different color
sns.barplot(data=df,
y='Award_Amount',
x='Model Selected',
hue='Region')
plt.show()
plt.clf()
Regression and residual plots
--------------------------------------------------
# Display a regression plot for Tuition
sns.regplot(data=df,
y='Tuition',
x='SAT_AVG_ALL',
marker='^',
color='g')
plt.show()
plt.clf()
---------
# Display the residual plot
sns.residplot(data=df,
y='Tuition',
x='SAT_AVG_ALL',
color='g')
plt.show()
plt.clf()
Regression plot parameters
--------------------------------------------------
# Plot a regression plot of Tuition and the Percentage of Pell Grants
sns.regplot(data=df,
y='Tuition',
x='PCTPELL')
plt.show()
plt.clf()
---------
# Create another plot that estimates the tuition by PCTPELL
sns.regplot(data=df,
y='Tuition',
x='PCTPELL',
x_bins=5)
plt.show()
plt.clf()
---------
# The final plot should include a line using a 2nd order polynomial
sns.regplot(data=df,
y='Tuition',
x='PCTPELL',
x_bins=5,
order=2)
plt.show()
plt.clf()
Binning data
--------------------------------------------------
# Create a scatter plot by disabling the regression line
sns.regplot(data=df,
y='Tuition',
x='UG',
fit_reg=False)
plt.show()
plt.clf()
---------
# Create a scatter plot and bin the data into 5 bins
sns.regplot(data=df,
y='Tuition',
x='UG',
x_bins=5)
plt.show()
plt.clf()
---------
# Create a regplot and bin the data into 8 bins
sns.regplot(data=df,
y='Tuition',
x='UG',
x_bins=8)
plt.show()
plt.clf()
Creating heatmaps
--------------------------------------------------
# Create a crosstab table of the data
pd_crosstab = pd.crosstab(df["Group"], df["YEAR"])
print(pd_crosstab)
# Plot a heatmap of the table
sns.heatmap(pd_crosstab)
# Rotate tick marks for visibility
plt.yticks(rotation=0)
plt.xticks(rotation=90)
plt.show()
Customizing heatmaps
--------------------------------------------------
# Create the crosstab DataFrame
pd_crosstab = pd.crosstab(df["Group"], df["YEAR"])
# Plot a heatmap of the table with no color bar and using the BuGn palette
sns.heatmap(pd_crosstab, cbar=False, cmap="BuGn", linewidths=0.3)
# Rotate tick marks for visibility
plt.yticks(rotation=0)
plt.xticks(rotation=90)
#Show the plot
plt.show()
plt.clf()
//^//^//^//^//^//^//^//^//^ Creating Plots on Data Aware Grids ^\\^\\^\\^\\^\\^\\^\\^\\^\\
Building a FacetGrid
--------------------------------------------------
# Create FacetGrid with Degree_Type and specify the order of the rows using row_order
g2 = sns.FacetGrid(df,
col="Degree_Type",row = 'Degree_Type',
row_order=['Graduate', 'Bachelors', 'Associates', 'Certificate'])
# Map a pointplot of SAT_AVG_ALL onto the grid
g2.map(sns.pointplot, 'SAT_AVG_ALL')
# Show the plot
plt.show()
plt.clf()
Using a catplot
--------------------------------------------------
# Create a factor plot that contains boxplots of Tuition values
sns.catplot(data=df,
x='Tuition',
kind='box',
row='Degree_Type')
plt.show()
plt.clf()
---------
# Create a facetted pointplot of Average SAT_AVG_ALL scores facetted by Degree Type
sns.catplot(data=df,
x='SAT_AVG_ALL',
kind='point',
row='Degree_Type',
row_order=['Graduate', 'Bachelors', 'Associates', 'Certificate'])
plt.show()
plt.clf()
Using a lmplot
--------------------------------------------------
# Create a FacetGrid varying by column and columns ordered with the degree_order variable
g = sns.FacetGrid(df, col="Degree_Type", col_order=degree_ord)
# Map a scatter plot of Undergrad Population compared to PCTPELL
g.map(plt.scatter, 'UG', 'PCTPELL')
plt.show()
plt.clf()
---------
# Re-create the previous plot as an lmplot
sns.lmplot(data=df,
x='UG',
y='PCTPELL',
col="Degree_Type",
col_order=degree_ord)
plt.show()
plt.clf()
---------
# Create an lmplot that has a column for Ownership, a row for Degree_Type and hue based on the WOMENONLY column
sns.lmplot(data=df,
x='SAT_AVG_ALL',
y='Tuition',
col="Ownership",
row='Degree_Type',
row_order=['Graduate', 'Bachelors'],
hue='WOMENONLY',
col_order=inst_ord)
plt.show()
plt.clf()
Building a PairGrid
--------------------------------------------------
# Create a PairGrid with a scatter plot for fatal_collisions and premiums
g = sns.PairGrid(df, vars=["fatal_collisions", "premiums"])
g2 = g.map(sns.scatterplot)
plt.show()
plt.clf()
---------
# Create the same PairGrid but map a histogram on the diag
g = sns.PairGrid(df, vars=["fatal_collisions", "premiums"])
g2 = g.map_diag(sns.histplot)
g3 = g2.map_offdiag(sns.scatterplot)
plt.show()
plt.clf()
Using a pairplot
--------------------------------------------------
# Create a pairwise plot of the variables using a scatter plot
sns.pairplot(data=df,
vars=["fatal_collisions", "premiums"],
kind='scatter')
plt.show()
plt.clf()
---------
# Plot the same data but use a different color palette and color code by Region
sns.pairplot(data=df,
vars=["fatal_collisions", "premiums"],
kind='scatter',
hue='Region',
palette='RdBu',
diag_kws={'alpha':.5})
plt.show()
plt.clf()
Additional pairplots
--------------------------------------------------
# Build a pairplot with different x and y variables
sns.pairplot(data=df,
x_vars=["fatal_collisions_speeding", "fatal_collisions_alc"],
y_vars=['premiums', 'insurance_losses'],
kind='scatter',
hue='Region',
palette='husl')
plt.show()
plt.clf()
---------
# plot relationships between insurance_losses and premiums
sns.pairplot(data=df,
vars=["insurance_losses", "premiums"],
kind ='reg',
palette='BrBG',
diag_kind = 'kde',
hue='Region')
plt.show()
plt.clf()
Building a JointGrid and jointplot
--------------------------------------------------
# Build a JointGrid comparing humidity and total_rentals
sns.set_style("whitegrid")
g = sns.JointGrid(x="hum",
y="total_rentals",
data=df,
xlim=(0.1, 1.0))
g.plot(sns.regplot, sns.histplot)
plt.show()
plt.clf()
---------
# Create a jointplot similar to the JointGrid
sns.jointplot(x="hum",
y="total_rentals",
kind='reg',
data=df)
plt.show()
plt.clf()
Jointplots and regression
--------------------------------------------------
# Plot temp vs. total_rentals as a regression plot
sns.jointplot(x="temp",
y="total_rentals",
kind='reg',
data=df,
order=2,
xlim=(0, 1))
plt.show()
plt.clf()
---------
# Plot a jointplot showing the residuals
sns.jointplot(x="temp",
y="total_rentals",
kind='resid',
data=df,
order=2)
plt.show()
plt.clf()
Complex jointplots
--------------------------------------------------
# Create a jointplot of temp vs. casual riders
# Include a kdeplot over the scatter plot
g = sns.jointplot(x="temp",
y="casual",
kind='scatter',
data=df,
marginal_kws=dict(bins=10))
g.plot_joint(sns.kdeplot)
plt.show()
plt.clf()
---------
# Replicate the above plot but only for registered riders
g = sns.jointplot(x="temp",
y="registered",
kind='scatter',
data=df,
marginal_kws=dict(bins=10))
g.plot_joint(sns.kdeplot)
plt.show()
plt.clf()