You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 KiB

<html> <head> </head>

___

Copyright by Pierian Data Inc. For more information, visit us at www.pieriandata.com

Grids

Imports

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

The Data

In [32]:
df = pd.read_csv('StudentsPerformance.csv')
In [33]:
df.head()
Out[33]:
gender race/ethnicity parental level of education lunch test preparation course math score reading score writing score
0 female group B bachelor's degree standard none 72 72 74
1 female group C some college standard completed 69 90 88
2 female group B master's degree standard none 90 95 93
3 male group A associate's degree free/reduced none 47 57 44
4 male group C some college standard none 76 78 75

catplot()

In [9]:
# Kind Options are: “point”, “bar”, “strip”, “swarm”, “box”, “violin”, or “boxen”
sns.catplot(x='gender',y='math score',data=df,kind='box')
Out[9]:
<seaborn.axisgrid.FacetGrid at 0x1e24e235b08>
In [11]:
sns.catplot(x='gender',y='math score',data=df,kind='box',row='lunch')
Out[11]:
<seaborn.axisgrid.FacetGrid at 0x1e24e37b2c8>
In [13]:
sns.catplot(x='gender',y='math score',data=df,kind='box',row='lunch',col='test preparation course')
Out[13]:
<seaborn.axisgrid.FacetGrid at 0x1e24e856388>

Pairgrid

Grid that pairplot is built on top of, allows for heavy customization of the pairplot seen earlier.

In [16]:
g = sns.PairGrid(df)
g = g.map_upper(sns.scatterplot)
g = g.map_diag(sns.kdeplot, lw=2)
g = g.map_lower(sns.kdeplot, colors="red")
In [24]:
g = sns.PairGrid(df, hue="gender", palette="viridis",hue_kws={"marker": ["o", "+"]})
g = g.map_upper(sns.scatterplot, linewidths=1, edgecolor="w", s=40)
g = g.map_diag(sns.distplot)
g = g.map_lower(sns.kdeplot)
g = g.add_legend();

# Safely ignore the warning, its telling you it didn't use the marker for kde plot
c:\users\marcial\anaconda3\envs\ml_master\lib\site-packages\seaborn\distributions.py:434: UserWarning: The following kwargs were not used by contour: 'marker'
  cset = contour_func(xx, yy, z, n_levels, **kwargs)

FacetGrid

In [4]:
sns.FacetGrid(data=df,col='gender',row='lunch')
Out[4]:
<seaborn.axisgrid.FacetGrid at 0x1e24be00748>
In [25]:
g = sns.FacetGrid(data=df,col='gender',row='lunch')
g = g.map(plt.scatter, "math score", "reading score", edgecolor="w")
g.add_legend()
Out[25]:
<seaborn.axisgrid.FacetGrid at 0x1e24f4fcb08>
In [26]:
# https://stackoverflow.com/questions/43669229/increase-space-between-rows-on-facetgrid-plot
In [29]:
g = sns.FacetGrid(data=df,col='gender',row='lunch')
g = g.map(plt.scatter, "math score", "reading score", edgecolor="w")
g.add_legend()

plt.subplots_adjust(hspace=0.4, wspace=1)


</html>