How to add center align text it in each subplot graph in seaborn?
Last Updated :
11 Jun, 2021
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.
Functions Used:
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)
Parameter:
- 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:
Python3
# Import Library
import seaborn as sns
# style must be one of white, dark,
# whitegrid, darkgrid (Optional)
sns.set_style("darkgrid")
# Loading default data of seaborn
exercise = sns.load_dataset("exercise")
g = sns.FacetGrid(exercise, row="diet",
col="time", margin_titles = True)
g.map(sns.regplot, "id", "pulse", color = ".3")
# Set Title for each subplot
col_order=['Deltaic', 'Plains','Hummock',
'Swale', 'Sand Dunes', 'Mountain']
# embedding center-text with its title
# using loop.
for txt, title in zip(g.axes.flat, col_order):
txt.set_title(title)
# add text
txt.text(10, 120,'Geeksforgeeks', fontsize = 12)
Output:
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:
Python3
# import Library
import seaborn as sns
import pandas as pd
# style must be one of white, dark,
# whitegrid, darkgrid (Optional)
sns.set_style("darkgrid")
# Loading default data of seaborn
exercise = sns.load_dataset("exercise")
exercise_kind = exercise.kind.value_counts().index
g = sns.FacetGrid(exercise, row="kind",
row_order=exercise_kind,
height=1.7, aspect=4,)
g.map(sns.kdeplot, "id")
# Set Title
col_order=['Deltaic Plains','Hummock and Swale',
'Sand Dunes']
# embedding center-text with its title
# at each iteration
for txt, title in zip(g.axes.flat, col_order):
txt.set_title(title)
# add text
txt.text(10.58, 0.04,'Geeksforgeeks', fontsize = 11)
Output:
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:
Python3
# Import Library
import seaborn as sns
import pandas as pd
# style must be one of white,
# dark, whitegrid, darkgrid
sns.set_style("darkgrid")
# Loading default data of seaborn
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, row = "sex",
col = "smoker",
margin_titles = True)
g.map(sns.lineplot, "total_bill", 'tip')
# Set Title for each subplot
col_order = ['Deltaic Plains','Hummock and Swale',
'Sand Dunes', 'Mountain']
# embedding center-text with its
# title at each iteration
for txt, title in zip(g.axes.flat, col_order):
txt.set_title(title)
# add text
txt.text(15, 6,'Geeksforgeeks', fontsize = 12)
Output:
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:
Python3
# import Library
import seaborn as sns
import pandas as pd
# style must be one of white, dark,
# whitegrid, darkgrid
sns.set_style("darkgrid")
# Loading default data of seaborn
exercise = sns.load_dataset("exercise")
g = sns.FacetGrid(exercise, col="time",
height=4, aspect=.5)
g.map(sns.barplot, "diet", "pulse",
order=["no fat", "low fat"])
# Set Title for each subplot
col_order=['Deltaic Plains','Hummock and Swale',
'Sand Dunes']
# embedding center-text with its title
# at each iteration
for txt, title in zip(g.axes.flat, col_order):
txt.set_title(title)
# add text
txt.text(-0.2, 60,'Geeksforgeeks', fontsize = 12)
Output:
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
TCP/IP Model The TCP/IP model (Transmission Control Protocol/Internet Protocol) is a four-layer networking framework that enables reliable communication between devices over interconnected networks. It provides a standardized set of protocols for transmitting data across interconnected networks, ensuring efficie
7 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read