How to add center align text it in each subplot graph in seaborn?
In this article, we are going to see how to add text in the center above each subplot using seaborn. Centering a title is a great way to represent the variability in your data. It can be applied to graphs to provide an additional layer of information on the presented data.
FacetGrid: It is a general way of plotting a grid based on a function. It helps us in visualizing the distribution of variables as well as the relationship between multiple variables. FacetGrid object uses the data frame as Input and the names of the variables that shape the column, row, dimensions of the grid, the syntax is given below:
Syntax: seaborn.FacetGrid( data, \*\*kwargs)
- data: Tidy data frame where each column is a variable and each row is an observation.
- \*\*kwargs: It uses many arguments as input such as i.e. row, col, hue, palette, etc.
Map method: The map() method is vastly used to apply a function or operation on a sequence of data. It applies a function on all the items of an iterator given as input after applying a specific function to all the elements of iterable and return
Syntax: map(function, iterable)
- function Required: The function to execute for each item
- iterable Required: A collection of row-column, sequence, or an iterator object.
Text method: This function is used to add text to the axes at location x, y in data coordinates.
Syntax: text(x, y, text, fontsize = int )
- x, y: The position to place the text.
- text: “your text”.
- fontsize: size of text in integer form.
Below is the implementation of the above method:
Example 1: Here we are plotting a regplot graph by calling sns.regplot, This method is used to plot data and a linear regression model.
Here we have a graph in which we have added an annotation to the inner part of the graph at a certain area where we’re adding our text at positions x=10 and y=120 with fontsize=12. Please find my code below:
Example 2: In this example, we are plotting kdeplot by calling sns.kdeplot, which represents the probability distribution of the data values as the area under the plotted curve. Here we have a graph in which we have added an annotation to the inner part of the graph at a certain area, here we’re adding our text at positions x=10.58 and y=0.04 with fontsize=11. Please find my code below:
Example 3: In this example, we are plotting line plot, sns.lineplot is charts that are normally used to identify trends over a period of time. Here we have a graph in which we have added an annotation to the inner part of the graph at a certain area here we’re adding our text at positions x=15 and y=6 with fontsize=12. Please find my code below:
Example 4: In this example, we will plot Barplot by calling sns.barplot. It is a visualization of x and y numeric and categorical dataset variables in a graph to find the relationship between them. Here we add our text at positions x= -0.2 and y=60 with fontsize=12. Please find my code below:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course