34.    Seaborn

March 11, 2019
home

Contents

description

overview

When you do a statistical analysis, define what you want to see, get data and process them, finally, choose a plotting type to plotting. Once you get the plotting, you can start to analyze. This overview is for choosing the visualization plotting type.

03. CSV for Seaborn data

    import pandas as pd

    tips = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv")
    
    print(tips.head(2))
    print('type of tips = ' + str(type(tips)))
''' 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 '''

04. figure-level functions vs axes-level functions

4.1 figure-level functions for one plot

        tips = sns.load_dataset("tips")
        g = sns.relplot(x="total_bill", y="tip", data=tips)

        print(' type of tips = ' + str(type(tips)))
        print(' type of the method returns ' + str(type(g)))

        plt.show()
            

4.2 axes-level functions for one plot

        import seaborn as sns
        import matplotlib.pyplot as plt
        tips = sns.load_dataset("tips")
        sns.set()
        ax = sns.scatterplot(x="total_bill", y="tip", data=tips)
            

4.3 figure-level functions for many subplots

4.4 axes-level functions for may subplots

    ...
    f, axes = plt.subplots(1, 2, sharey=True, figsize=(6, 4)) # one row, 2 columns

    sns.boxplot(x="day", y="tip", data=tips, ax=axes[0])                         #plot 1
    sns.scatterplot(x="total_bill", y="tip", hue="day", data=tips, ax=axes[1])   #plot 2
    
    plt.legend()                         
    plt.show()
            

11a. x-y relationshiprelationship, relplot, scatter

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    tips = sns.load_dataset("tips")
    sns.set(style="ticks")
            
    g = sns.relplot(x="total_bill", y="tip", hue="day", data=tips, height=4)
    print('type of g = ' + str(type(g)))  
    
    plt.legend()
    plt.show()
            

11b. x-y relationship, continuity, relplot, line

    fmri = sns.load_dataset("fmri")
    sns.relplot(x="timepoint", y="signal", kind="line", data=fmri)
            

12. distribution, distplot, histogram

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
n = np.random.normal(size=100)
ax = sns.distplot(n)
plt.show()
            

13a. linear regression model

    ---  Using regression linear model 1
    g = sns.lmplot(x="total_bill", y="tip", data=tips)

    ---  Using regression linear model 2
    ax = sns.regplot(x="total_bill", y="tip", data=tips)
            

13b. non-linear regression model

    anscombe = sns.load_dataset("anscombe")
    ...
    sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"), 
           order = 2,           nonlinear
           #order = 1,          linear        
           #order = 3,          nonlinear     
           #order = 5,          nonlinear     
           ci=None)
            

14a. categorical-data, catplot, scatter

    g = sns.catplot(x="day", y="total_bill", data=tips)
            

14b. categorical-data, catplot, box

    g = sns.catplot(x="day", y="total_bill", kind="box", data=tips)
            

21. sns.FacetGrid and plt.hist

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    tips = sns.load_dataset('tips')       
    
    sns.set()
    g = sns.FacetGrid(tips, col="time", row="smoker")
    g = g.map(plt.hist, "total_bill")  
    
    plt.show()
            

22. sns.FacetGrid and plt.scatter

    ...
    g = sns.FacetGrid(tips, col="time", row="smoker")
    g.map(plt.scatter, "total_bill", "tip")
    ...
            

23. sns.FacetGrid and parameter hue

    ...
    g = sns.FacetGrid(tips, col="time",  hue="smoker")    
    g.map(plt.scatter, "total_bill", "tip")
    g.add_legend()                                    
    
    plt.legend()                                         
    plt.show()
            

24. sns.FacetGrid and plotting size

    ...
    g = sns.FacetGrid(tips, col="day", height=4, aspect=.75)   
    g.map(plt.hist, "total_bill")
    
    plt.show()
            

25. sns.FacetGrid and col_wrap

        ...        
        att = sns.load_dataset('attention') 
        sns.set()  
        
        g = sns.FacetGrid(att, col="subject", col_wrap=5, height=1.5)
        g = g.map(plt.plot, "solutions", "score", marker=".")
        ...
            

26. sns.PairGrid and plt.scatter

  
        ...  
        iris = sns.load_dataset('iris') 
        sns.set()  
        g = sns.PairGrid(iris)
        g.map(plt.scatter)
        ...
            

27. sns.JointGrid and sns.regplot, sns.distplot

JointPlot demo 1 JointPlot demo 2
  
    g = sns.JointGrid(x="total_bill", y="tip", data=tips)
    g.plot(sns.regplot, sns.distplot)