Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Seaborn graphing present
1. 6/17/19, 21)55seaborn_graphing_present
Page 1 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [1]: import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from IPython.display import display
import matplotlib
%matplotlib inline
In [2]: iris = sns.load_dataset('iris')
tips = sns.load_dataset('tips')
display(iris.head())
print(iris.shape)
display(tips.head())
print(tips.shape)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
(150, 5)
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
(244, 7)
2. 6/17/19, 21)55seaborn_graphing_present
Page 2 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [3]: # Boxplot
# sns.set(style="whitegrid")
# with matplotlib
grouped = {g:tips.loc[i,'total_bill'] for g , i in tips.groupby('day')
.groups.items()}
plt.boxplot(grouped.values(), labels=grouped.keys() )
# with pandas
ax = tips.boxplot(by='day', column='total_bill')
plt.show()
ax = sns.boxplot(x='day', y='total_bill', data=tips)
ax.set_title('Boxplot with Seaborn')
Out[3]: Text(0.5, 1.0, 'Boxplot with Seaborn')
3. 6/17/19, 21)55seaborn_graphing_present
Page 3 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [4]: # Stripplot
ax = sns.stripplot(x='day', y='total_bill', data=tips)
4. 6/17/19, 21)55seaborn_graphing_present
Page 4 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [5]: # Stripplot: Let's add a hue and split
ax = sns.stripplot(x='day', y='total_bill', hue='smoker', dodge=True,
data=tips)
In [6]: # Violinplot
ax = sns.violinplot(x="day", y="total_bill", data=tips)
/Users/apple/anaconda3/lib/python3.7/site-packages/scipy/stats/stats
.py:1713: FutureWarning: Using a non-tuple sequence for multidimensi
onal indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[s
eq]`. In the future this will be interpreted as an array index, `arr
[np.array(seq)]`, which will result either in an error or a differen
t result.
return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumva
l
5. 6/17/19, 21)55seaborn_graphing_present
Page 5 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [7]: # Violinplot
ax = sns.violinplot(x="day", y="total_bill", data=tips, hue="smoker")
In [8]: # Violinplot
ax = sns.violinplot(x="day", y="total_bill", data=tips, hue="smoker",
split=True, palette="bright")
6. 6/17/19, 21)55seaborn_graphing_present
Page 6 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [9]: # Violinplot
fig , ax = plt.subplots(figsize=(20,10))
ax = sns.violinplot(x="day", y="total_bill", data=tips, hue="smoker",
split=True, palette="bright",
inner="stick", bw=0.2, ax=ax
)
In [10]: ax = sns.swarmplot(x="day", y="total_bill", hue="smoker", data=tips, p
alette="Set2")
7. 6/17/19, 21)55seaborn_graphing_present
Page 7 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
A note on color palettes
Seaborn uses Color Brewer sets. This is a cool tool for finding a good (color blind/grayscale friendly).
http://colorbrewer2.org (http://colorbrewer2.org)
In [11]: # Combine a Violinplot and Swarmplot
fig , ax = plt.subplots(figsize=(15,10))
ax = sns.violinplot(x="day", y="total_bill", data=tips, inner=None)
ax = sns.swarmplot(x="day", y="total_bill", data=tips,
color="white", edgecolor="gray", ax=ax)
Distribution Plots
jointplot - two variables
pairplot - grid comparing two-way relationships
8. 6/17/19, 21)55seaborn_graphing_present
Page 8 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [12]: g = sns.jointplot(x="total_bill", y="tip", data=tips)
9. 6/17/19, 21)55seaborn_graphing_present
Page 9 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [13]: g = sns.jointplot("total_bill", "tip", data=tips, kind="reg")
10. 6/17/19, 21)55seaborn_graphing_present
Page 10 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [14]: # Distplot - makes histograms
grouped = {g:iris.loc[i,'petal_length'] for g , i in iris.groupby('spe
cies').groups.items()}
for g , v in grouped.items():
sns.distplot(v, label=g)
plt.legend()
Out[14]: <matplotlib.legend.Legend at 0x1a220d9320>
11. 6/17/19, 21)55seaborn_graphing_present
Page 11 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [15]: # pairplot - look at all pairwise relationships
g = sns.pairplot(iris)
12. 6/17/19, 21)55seaborn_graphing_present
Page 12 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [16]: # pairplot - look at all pairwise relationships
g = sns.pairplot(iris, hue='species', palette='husl')
13. 6/17/19, 21)55seaborn_graphing_present
Page 13 of 13http://localhost:8888/nbconvert/html/Desktop/UW/2019%20spring/CSE180/seaborn_graphing_present.ipynb?download=false
In [17]: # lmplot - plots regplots across multiple variables
g = sns.lmplot(x='sepal_length', y='sepal_width', hue='species', data=
iris, palette='Dark2')
In [ ]: