Seaborn.PairGrid class



The Seaborn.PairGrid is used to subplot grid for plotting pairwise relationships in a dataset. This object creates a grid of many axes with columns and rows for each variable in a dataset.

Bivariate plots can be drawn in the upper and lower triangles using a variety of axes-level charting tools, and the diagonal can display the marginal distribution of each variable. With pairplot, many common plots can be produced in a single line(). When you require greater versatility, use PairGrid.

Syntax

Following is the syntax of the seaborn.PairGrid −

class seaborn.PairGrid(**kwargs)

Parameters

Some of the parameters of the seaborn.PairGridare discussed below.

S.No Name and Description
1 Data

Takes dataframe where each column is a variable and each row is an observation.

2 hue

variables that specify portions of the data that will be displayed on certain grid facets. To regulate the order of levels for this variable, refer to the var order parameters.

3 Kind

Takes avlues from{‘scatter’, ‘kde’, ‘hist’, ‘reg’}and the kind of plot to make is determined.

4 Diag_kind

Takes values from{‘auto’, ‘hist’, ‘kde’, None} and the kind of plot for the diagonal subplots is determined if used. If ‘auto’, choose based on whether or not hue is used.

5 Height

Takes a scalar value and determines the height of the facet.

6 Aspect

Takes scalar value and Aspect ratio of each facet, so that aspect * height gives the width of each facet in inches.

7 corner

Takes Boolean value and If True, don’t add axes to the upper (off-diagonal) triangle of the grid, making this a “corner” plot.

8 hue_order

Takes lists as input and Order for the levels of the faceting variables is determined by this order.

Loading the seaborn library

Let us load the seaborn library and the dataset before moving on to the developing the plots. To load or import the seaborn library the following line of code can be used.

Import seaborn as sns

Loading the dataset

In this article, we will make use of the exercise dataset inbuilt in the seaborn library. the following command is used to load the dataset.

exercise=sns.load_dataset("exercise")

The below mentioned command is used to view the first 5 rows in the dataset. This enables us to understand what variables can be used to plot a graph.

exercise.head()

The below is the output for the above piece of code.

index,Unnamed: 0,id,diet,pulse,time,kind
0,0,1,low fat,85,1 min,rest
1,1,1,low fat,85,15 min,rest
2,2,1,low fat,88,30 min,rest
3,3,2,low fat,90,1 min,rest
4,4,2,low fat,92,15 min,rest

Now that we have loaded the dataset, we will explore a few examples.

Example 1

We will begin by calling the PairGrid and passing the dataframe to it. This will yield multiple plots as can be seen below.

import seaborn as sns
import matplotlib.pyplot as plt
exercise=sns.load_dataset("exercise")
exercise.head()
g = sns.PairGrid(exercise)
plt.show()

Output

The plot obtained is shown below −

seaborn pairgrid class

Example 2

In this example, we will pass a few more parameters along with the dataframe to the PairGrid. We are using the exercise dataset and we will plot graphs that are scatterplots in nature.

import seaborn as sns
import matplotlib.pyplot as plt
exercise=sns.load_dataset("exercise")
exercise.head()
g = sns.PairGrid(exercise)
g.map(sns.scatterplot)
plt.show()

Output

the plot obtained is shown below.

pairgrid class

Example 3

In this example, we will plot different kind sof plots in the facet. This can be done with the help of map_upper, map_lower and map_diag functions which are used to initialize the kind of plot that needs to be plotted in the grid.

Suppose, map_upper is used and scatterplot is the kind that is set, then the plots in the upper section of grid are scatter in kind and so on. In the below example, upper plots are scatter in kind, lower side plots are kernel density estimate(KDE) in kind and the remaining are empirical cumulative density function(ecdf) plots.

import seaborn as sns
import matplotlib.pyplot as plt
exercise=sns.load_dataset("exercise")
exercise.head()
g=sns.PairGrid(exercise)
g.map_upper(sns.scatterplot)
g.map_lower(sns.kdeplot)
g.map_diag(sns.ecdfplot)
plt.show()

Output

The output obtained is as follows −

pairgrid.jpg
seaborn_multi_plot_grids_introduction.htm
Advertisements