fa cto rp l o t a n d l m p l o t usi n g fa cetg ri d · d ata v i su a li zati o n w i th se a b...

32
Using FacetGrid, factorplot and lmplot DATA VISUALIZATION WITH SEABORN Chris Moftt Instructor

Upload: others

Post on 11-May-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Using FacetGrid,factorplot and lmplot

D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Chris Mof�ttInstructor

Page 2: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Page 3: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Tidy dataSeaborn's grid plots require data in "tidy format"

One observation per row of data

Page 4: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

FacetGridThe FacetGrid is foundational for many data aware grids

It allows the user to control how data is distributed across columns,

rows and hue

Once a FacetGrid is created, the plot type must be mapped to

the grid

Page 5: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

FacetGrid Categorical Example

g = sns.FacetGrid(df, col="HIGHDEG")

g.map(sns.boxplot, 'Tuition',

order=['1', '2', '3', '4'])

Page 6: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

factorplot()The factorplot is a simpler way to use a FacetGrid for

categorical data

Combines the facetting and mapping process into 1 function

sns.factorplot(x="Tuition", data=df,

col="HIGHDEG", kind='box')

Page 7: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

FacetGrid for regressionFacetGrid() can also be used for scatter or regression plots

g = sns.FacetGrid(df, col="HIGHDEG")

g.map(plt.scatter, 'Tuition', 'SAT_AVG_ALL')

Page 8: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

lmplotlmplot plots scatter and regression plots on a FacetGrid

sns.lmplot(data=df, x="Tuition", y="SAT_AVG_ALL",

col="HIGHDEG", fit_reg=False)

Page 9: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

lmplot with regression

sns.lmplot(data=df, x="Tuition", y="SAT_AVG_ALL",

col="HIGHDEG", row='REGION')

Page 10: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Let's practice!D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Page 11: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Using PairGrid andpairplot

D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Chris Mof�ttInstructor

Page 12: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Pairwise relationshipsPairGrid shows pairwise relationships between data elements

Page 13: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Creating a PairGridThe PairGrid follows similar API to FacetGrid

g = sns.PairGrid(df, vars=["Fair_Mrkt_Rent",

"Median_Income"])

g = g.map(plt.scatter)

Page 14: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Customizing the PairGrid diagonalsg = sns.PairGrid(df, vars=["Fair_Mrkt_Rent",

"Median_Income"])

g = g.map_diag(plt.hist)

g = g.map_offdiag(plt.scatter)

Page 15: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Pairplotpairplot is a shortcut for the PairGrid

sns.pairplot(df, vars=["Fair_Mrkt_Rent",

"Median_Income"], kind='reg',

diag_kind='hist')

Page 16: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Customizing a pairplotsns.pairplot(df.query('BEDRMS < 3'),

vars=["Fair_Mrkt_Rent",

"Median_Income", "UTILITY"],

hue='BEDRMS', palette='husl',

plot_kws={'alpha': 0.5})

Page 17: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Let's practice!D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Page 18: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Using JointGrid andjointplot

D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Chris Mof�ttInstructor

Page 19: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

JointGrid() Overview

Page 20: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Basic JointGrid

g = sns.JointGrid(data=df, x="Tuition",

y="ADM_RATE_ALL")

g.plot(sns.regplot, sns.distplot)

Page 21: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Advanced JointGridg = sns.JointGrid(data=df, x="Tuition",

y="ADM_RATE_ALL")

g = g.plot_joint(sns.kdeplot)

g = g.plot_marginals(sns.kdeplot, shade=True)

g = g.annotate(stats.pearsonr)

Page 22: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

jointplot()

sns.jointplot(data=df, x="Tuition",

y="ADM_RATE_ALL", kind='hex')

Page 23: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Customizing a jointplot

g = (sns.jointplot(x="Tuition",

y="ADM_RATE_ALL", kind='scatter',

xlim=(0, 25000),

marginal_kws=dict(

bins=15,rug=True),

data=df.query('UG < 2500 &

Ownership == "Public"'))

.plot_joint(sns.kdeplot))

Page 24: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Customizing a jointplot

Page 25: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Let's practice!D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Page 26: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Selecting SeabornPlots

D ATA V I S U A L I Z AT I O N W I T H S E A B O R N

Chris Mof�ttInstructor

Page 27: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Page 28: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Univariate Distribution Analysisdistplot() is the best place to start for this analysis

rugplot() and kdeplot() can be useful alternatives

Page 29: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Regression Analysislmplot() performs regression analysis and supports facetting

Page 30: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

Categorical PlotsExplore data with the categorical plots and facet with

Page 31: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

DATA VISUALIZATION WITH SEABORN

pairplot() and jointplot()Perform regression analysis with lmplot

Analyze distributions with distplot

Page 32: fa cto rp l o t a n d l m p l o t Usi n g Fa cetG ri d · D ATA V I SU A LI ZATI O N W I TH SE A B O R N fa ctorpl ot( ) Th e factorplot is a s imp ler way t o u s e a FacetGrid fo

Thank You!D ATA V I S U A L I Z AT I O N W I T H S E A B O R N