- Seaborn Tutorial
- Seaborn - Home
- Seaborn - Introduction
- Seaborn - Environment Setup
- Importing Datasets and Libraries
- Seaborn - Figure Aesthetic
- Seaborn- Color Palette
- Seaborn - Histogram
- Seaborn - Kernel Density Estimates
- Visualizing Pairwise Relationship
- Seaborn - Plotting Categorical Data
- Distribution of Observations
- Seaborn - Statistical Estimation
- Seaborn - Plotting Wide Form Data
- Multi Panel Categorical Plots
- Seaborn - Linear Relationships
- Seaborn - Facet Grid
- Seaborn - Pair Grid
- Function Reference
- Seaborn - Function Reference
- Seaborn Useful Resources
- Seaborn - Quick Guide
- Seaborn - Useful Resources
- Seaborn - Discussion
Seaborn.FacetGrid() method
The Seaborn.FacetGrid() method is useful when you want to visualize the distribution of a variable or the relationship between multiple variables separately within subsets of your dataset. Row, Col, and Hue are the three possible dimensions that can be used to draw a FacetGrid. The first two clearly correspond to the array of axes that is produced.
With the hue parameter, which depicts several data subsets in various colors, it can also express levels of a third variable. In contrast to axes-level functions that receive hue, this just draws subsets on top of one another and does not tailor the hue parameter for the particular display. It uses color to resolve components on a third dimension.
Syntax
Following is the syntax of the seaborn.FacetGrid() method −
class seaborn.FacetGrid(**kwargs) Multi-plot grid for plotting conditional relationships. __init__(self, data, *, row=None, col=None, hue=None, col_wrap=None, sharex=True, sharey=True, height=3, aspect=1, palette=None, row_order=None, col_order=None, hue_order=None, hue_kws=None, dropna=False, legend_out=True, despine=True, margin_titles=False, xlim=None, ylim=None, subplot_kws=None, gridspec_kws=None, size=None)
Parameters
Some of the parameters of the seaborn.FacetGrid() method are discussed below.
S.No | Name and Description |
---|---|
1 | Data Takes dataframe where each column is a variable and each row is an observation. |
2 | Row,col,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 | Col_wrap Takes int as input and To make the column facets span several rows, "wrap" the column variable at this width. |
4 | Share{x,y} Takes Boolean valjue or col, row as values and If true, the facets will share y axes across columns and/or x axes across rows. |
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 | Despine Takes Boolean value and Removes the top and right spines from the plots. |
8 | {row,col,hue}_order Takes lists as input and Order for the levels of the faceting variables is determined by this order. |
let us load the seaborn library and the dataset before moving on to the developing the plots.
Loading the seaborn library
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 Titanic dataset inbuilt in the seaborn library. the following command is used to load the dataset.
titanic=sns.load_dataset("titanic")
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.
titanic.head()
the below is the output for the above piece of code.
index,survived,pclass,sex,age,sibsp,parch,fare,embarked,class,who,adult_male,deck,embark_town,alive,alone 0,0,3,male,22.0,1,0,7.25,S,Third,man,true,NaN,Southampton,no,false 1,1,1,female,38.0,1,0,71.2833,C,First,woman,false,C,Cherbourg,yes,false 2,1,3,female,26.0,0,0,7.925,S,Third,woman,false,NaN,Southampton,yes,true
Now that we have loaded the dataset, we will explore a few examples.
Example 1
In this example, we will see how to produce a simple facet grid by passing the dataset, and col and row parameters to the FacetGrid() method.
import seaborn as sns import matplotlib.pyplot as plt titanic=sns.load_dataset("titanic") titanic.head() sns.FacetGrid(titanic, col="sex", row="class") plt.show()
Output
The output obtained is as follows −
Example 2
In order to draw a plot on every facet, pass a function and the name of one or more columns in the data frame to FacetGrid.map(). In this example, we will do the same. We are using the titanic dataset and we will plot scatterplots in all the facets.
import seaborn as sns import matplotlib.pyplot as plt titanic=sns.load_dataset("titanic") titanic.head() grid = sns.FacetGrid(titanic, col="sex", row="class") grid.map(sns.scatterplot, "age", "fare") plt.show()
the above lines of code can be used to implement the scenario explained and the output obtained is given below.
Output
Example 3
Now we will plot a histogram using the hsitplot() method in all the facets and also use the add_legend to all the legend to the plots displayed. This can be done using the below lines of code. Firstly, we will draw facets and draw histplot in those facets. Then we will add the legend to the facet. In order to do this the facetgrid will be stored in a variable called grid.
import seaborn as sns import matplotlib.pyplot as plt titanic=sns.load_dataset("titanic") titanic.head() grid = sns.FacetGrid(titanic, col="class", hue="sex") grid.map_dataframe(sns.histplot, x="age", y="fare") grid.add_legend() plt.show()
Output
The output obtained is as follows −
Example 4
We will understand the use of savefig() method in python. This can be used to save any plot obtained as an image for your personal use. Your plot must be saved in a variable such as grid just like in the above example, then we will apply the savefig() method on it and obtain the image. The savefig() method takes an argument which is the name of the file to save the plot as. In the below code, the plot is being saved as “facetplot.png”.
import seaborn as sns import matplotlib.pyplot as plt titanic=sns.load_dataset("titanic") titanic.head() grid.savefig("facetplot.png") plt.show()
To Continue Learning Please Login
Login with Google