Seaborn

Seaborn is a library mostly used for statistical plotting in Python. It is built on top of Matplotlib and provides beautiful default styles and color palettes to make statistical plots more attractive.

seaborn

Seaborn is an amazing visualization library for statistical graphics plotting in Python. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is built on the top of matplotlib library and also closely integrated to the data structures from pandas.
Different categories of plot in Seaborn

Plots are basically used for visualizing the relationship between variables. Those variables can be either be completely numerical or a category like a group, class or division. Seaborn divides plot into the below categories –

Relational plots: This plot is used to understand the relation between two variables.

Categorical plots: This plot deals with categorical variables and how they can be visualized.

Distribution plots: This plot is used for examining univariate and bivariate distributions

Regression plots: The regression plots in seaborn are primarily intended to add a visual guide that helps to emphasize patterns in a dataset during exploratory data analyses.

Matrix plots: A matrix plot is an array of scatterplots.

Multi-plot grids: It is an useful approach is to draw multiple instances of the same plot on different subsets of the dataset.

  1. starter template
                                        pip install seaborn                            
  2. Dist plot
    Dist plot

    Seaborn dist plot is used to plot a histogram, with some other variations like kdeplot and rugplot.

                       # Plot a simple histogram and kde                
                             # with binsize determined automatically                   
                              sns.distplot(d, kde=True, color="m")                
  3. Line plot
    Line plot

    The line plot is one of the most basic plot in seaborn library. This plot is mainly used to visualize the data in form of some time series, i.e. in continuous manner.

                # Plot the responses for different
                            # events and regions
                            sns.lineplot(x="timepoint",y="signal",hue="region", style="event", data=fmri)
  4. Lmplot
    Lmplot

    The lmplot is another most basic plot. It shows a line representing a linear regression model along with data points on the 2D-space and x and y can be set as the horizontal and vertical labels respectively.

                # Show the results of a linear regression        
                            sns.lmplot(x="x", y="y", data=df)
  5. set style of background of plot
    sns.set(style="    ")                
  6. Swarmplot
    ax = sns.swarmplot(x='   ', y='    ', data=df )
  7. giving title to the plot
    plt.title('     ');
  8. function to show plot
    plt.show()
  9. Barplot
    Barplot
    A barplot is basically used to aggregate the categorical data according to some methods and by default it’s the mean. It can also be understood as a visualization of the group by action. To use this plot we choose a categorical column for the x-axis and a numerical column for the y-axis, and we see that it creates a plot taking a mean per categorical column.

    Syntax:
    barplot([x, y, hue, data, order, hue_order, …])
                        sns.barplot(x ='   ', y ='    ', data = df,  
                            palette ='plasma')
                    
  10. Countplot
    Countplot
    A countplot basically counts the categories and returns a count of their occurrences. It is one of the simplest plots provided by the seaborn library.

    Syntax:
    countplot([x, y, hue, data, order, …])
    sns.countplot(x ='sex', data = df)
  11. Boxplot
    Boxplot
    Box Plot is the visual representation of the depicting groups of numerical data through their quartiles. Boxplot is also used to detect the outlier in the data set.

    Syntax:
    boxplot([x, y, hue, data, order, hue_order, …])
                        sns.boxplot(x='     ', y='   ', data=df, hue='')
                    
  12. Violinplot
    Violinplot
    It is similar to the boxplot except that it provides a higher, more advanced visualization and uses the kernel density estimation to give a better description about the data distribution.

    Syntax:
    violinplot([x, y, hue, data, order, …])
                    sns.violinplot(x='', y='', data=df,hue='', split=True)
                
  13. Stripplot
    Stripplot
    It basically creates a scatter plot based on the category.

    Syntax:
    stripplot([x, y, hue, data, order, …])
                sns.stripplot(x='  ', y='   ', data=df,jitter=True, hue='  ', dodge=True)
            
  14. Seaborn Figure Stylest
    Seaborn Figure Styles
    This affects things like the color of the axes, whether a grid is enabled by default, and other aesthetic elements.

    The ways of styling themes are as follows:
    white
    dark
    whitegrid
    darkgrid
    ticks
            sns.set_style('white')
        
  15. Removing Axes Spines
    Removing Axes Spines
    The despine() is a function that removes the spines from the right and upper portion of the plot by default. sns.despine(left = True) helps remove the spine from the left.
        sns.despine()
    
  16. Size and aspect
    Size and aspect
    Non grid plot: The figure() is a matplotlib function used to plot the figures. The figsize is used to set the size of the figure.
    plt.figure(figsize =(12, 3))
    
  17. Scale and Context
    Scale and Context
    The set_context() allows us to override default parameters. This affects things like the size of the labels, lines, and other elements of the plot, but not the overall style.

    The context are:

    poster
    paper
    notebook
    talk
    sns.set_context('poster', font_scale = 2)
    
  18. color_palette() types
    color_palette() types −

    Syntax: seaborn.color_palette(palette=None, n_colors=None, desat=None)
    Parameters:
    palette: Name of palette or None to return current palette.
    n_colors: Number of colors in the palette.
    desat: Proportion to desaturate each color.
    Returns: list of RGB tuples or matplotlib.colors.Colormap

    types:

    • Qualitative
    • Sequential
    • Diverging

    A qualitative palette is used when the variable is categorical in nature, the color assigned to each group need to be distinct.
    Each possible value of the variable is assigned one color from a qualitative palette within a plot as shown in figure.

    In sequential palettes color moved sequentially from a lighter to a darker. When the variable assigned to be colored is numeric or has inherently ordered values, then it can be depicted with a sequential palette as shown in figure.

    When we work on mixed value like +ve and -ve(low and high values) then diverging palette is the best suit for visualization.

                        
                            current_palette = sns.color_palette()
                            sns.palplot(current_palette)
                            plt.show()
                        
                    

                        
                            current_palette = sns.color_palette()
                            sns.palplot(current_palette)
                            plt.show()
                        
                    

                        
                            current_palette = sns.color_palette()
                            sns.palplot(sns.color_palette("terrain_r", 7))
                            plt.show()