Python is a storehouse of numerous immensely powerful libraries and frameworks. Among them, is Seaborn, which is a dominant data visualization library, granting yet another reason for programmers to complete Python Certification. In this Python Seaborn Tutorial, you will be leaning all the knacks of data visualization using Seaborn.

Before moving on, let’s glance through all the topics of discussion in this article:

So let’s begin first by reasoning out the importance of Python Seaborn.

Why use Python Seaborn?

As mentioned earlier, the Python Seaborn library is used to ease the challenging task of data visualization and it’s based on Matplotlib. Seaborn allows the creation of statistical graphics through the following functionalities:

An API that is based on datasets allowing comparison between multiple variables

Supports multi-plot grids that in turn ease building complex visualizations

Univariate and bivariate visualizations available to compare between subsets of data

Availability of different color palettes to reveal various kinds of patterns

So, if you were wondering as to why use Seaborn when you already have Matplotlib, here is the answer to it.

Python Seaborn vs Matplotlib:

“If Matplotlib “tries to make easy things easy and hard things possible”, seaborn tries to make a well-defined set of hard things easy too” – Michael Waskom (Creator of Seaborn).

Factually, Matplotlib is good but Seaborn is better. There are basically two shortcomings of Matplotlib that Seaborn fixes:

Matplotlib can be personalized but it’s difficult to figure out what settings are required to make plots more attractive. On the other hand, Seaborn comes with numerous customized themes and high-level interfaces to solve this issue.

When working with Pandas, Matplotlib doesn’t serve well when it comes to dealing with DataFrames, while Seaborn functions actually work on DataFrames.

How to install Seaborn?

To install the Python Seaborn library, you can use the following commands based on the platform you use:

pip install seaborn

or

conda install seaborn

Once this is installed, just make sure to install the packages and libraries that seaborn is dependent on.

Installing Python Seaborn Dependencies:

To install these libraries, you can use the same commands as shown previously for Seaborn with their respective names. Once installed, they can be imported easily. Seaborn allows you to load any dataset from GIT using the load_dataset() function. You can also view all the available datasets using get_dataset_names() function as follows:EXAMPLE:

import seaborn as sns
sns.get_dataset_names()

This will return a list of all the available datasets. Now that you have set up your environment for working with seaborn, let’s move on further to see how to use it’s plotting functions in Python.

Seaborn Plotting Functions

Visualizing Statistical Relationships:

The process of understanding relationships between variables of a dataset and how these relationships, in turn, depend on other variables is known as statistical analysis. Let’s now take a deeper look at functions needed for this:

relplot():

This is a figure-level-function that makes use of two other axes functions for Visualizing Statistical Relationships which are:

scatterplot()

lineplot()

These functions can be specified using the ‘kind’ parameter of relplot(). In case this parameter is given, it takes the default one which is scatterplot(). Before you begin writing your code, make sure to import the required libraries as follows:

Please note that the style attribute is also customizable and can take any value such as darkgrid, ticks, etc which I will discuss later in the plot-aesthetics section. Let’s now take a look at a small example:

As you can see, in the above example I have not set the ‘kind’ parameter. Therefore it has returned the graph as the default scatterplot. You can specify any of the axes level function to change the graph as need be. Let’s take an example of this as well:

Once this is done, you can continue plotting univariate and bivariate distributions.

Plotting Univariate distributions:

To plot them, you can make use of distplot() function as follows:

EXAMPLE:

a = np.random.normal(loc=5,size=100,scale=2)
sns.distplot(a);

OUTPUT:

As you can see in the above example, we have plotted a graph for the variable a whose values are generated by the normal() function using distplot.

Plotting bivariate distributions:

This comes into picture when you have two random independent variables resulting in some probable event. The best function to plot these type of graphs is jointplot(). Let us now plot a bivariate graph using jointplot().

Now that you have understood the various functions in Python Seaborn, let’s move on to build structured multi-plot grids.

Multi-Plot Grids:

Python Seaborn allows you to plot multiple grids side-by-side. These are basically plots or graphs that are plotted using the same scale and axes to aid comparison between them. This, in turn, helps the programmer to differentiate quickly between the plots and obtain large amounts of information.

Consider the following example of facetgrid() function to plot these graphs.

The above output clearly shows the comparison between the tips that are given during Lunch and Dinner. You can also plot using PairGrid function when you have a pair of variables to compare. Consider the following example.

As you can see, the above output clearly compares between the year and the number of passengers in different ways.

Seaborn also allows customizations regarding aesthetics which is discussed further.

Plot-Aesthetics:

This segment of Python Seaborn tutorial deals with making our plots more attractive and delightful.

Python Seaborn Figure-Aesthetics:

The first function that I shall be discussing is set(). I have been using the ‘style’ parameter of this function before. This parameter basically deals with seaborn themes. At present, there are five of them available namely darkgrid, ticks, whitegrid, white and dark.

In the above output, you can notice the theme is changed to white. You can explore them further using the other themes as well. If you notice in the previous output, there are axes present all around the graph. However, this is also customizable using the despine() function. Look at the example below.

OUTPUT: Note the difference between the previous two outputs. However, there are many more options that you can explore for yourself.

Python Seaborn Color-Palettes:

Color is basically the feature that approaches the human eyes beyond any other feature. Seaborn allows you to play with colors using various functions such as color_palette(), hls_palette(), husl_palette(), etc. Take a look at the colors that are currently present within seaborn.