如何在seaborn的每个子图中添加居中对齐文本?
在本文中,我们将看到如何使用 seaborn 在每个子图上方的中心添加文本。将标题居中是表示数据可变性的好方法。它可以应用于图形以提供有关所呈现数据的附加信息层。
使用的功能:
FacetGrid :它是一种基于函数绘制网格的通用方法。它帮助我们可视化变量的分布以及多个变量之间的关系。 FacetGrid 对象使用数据框作为输入,并使用塑造网格的列、行、维度的变量名称,语法如下:
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 方法: map() 方法广泛用于对数据序列应用函数或操作。在将特定函数应用于 iterable 的所有元素并返回后,它将函数应用于作为输入给出的迭代器的所有项目
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.
文本方法:此函数用于在数据坐标中的位置 x, y 的轴上添加文本。
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.
下面是上述方法的实现:
示例 1:这里我们通过调用 sns.regplot 绘制 regplot 图,该方法用于绘制数据和线性回归模型。
在这里,我们有一个图形,其中我们在图形的内部某个区域添加了注释,我们在 x=10 和 y=120 的位置添加了文本,字体大小为 12。请在下面找到我的代码:
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)
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)
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)
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)
输出:
示例 2:在此示例中,我们通过调用 sns.kdeplot 绘制 kdeplot,它将数据值的概率分布表示为绘制曲线下的面积。在这里,我们有一个图表,其中我们在某个区域的图表内部添加了注释,这里我们在 x=10.58 和 y=0.04 的位置添加了文本,字体大小为 11。请在下面找到我的代码:
蟒蛇3
# 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)
输出:
示例 3:在此示例中,我们正在绘制线图,sns.lineplot 是通常用于识别一段时间内趋势的图表。在这里,我们有一个图表,其中我们在某个区域的图表内部添加了注释,我们在 x=15 和 y=6 的位置添加了文本,字体大小为 12。请在下面找到我的代码:
蟒蛇3
# 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)
输出:
示例 4:在本示例中,我们将通过调用 sns.barplot 来绘制 Barplot。它是图形中 x 和 y 数值和分类数据集变量的可视化,以查找它们之间的关系。在这里,我们在位置 x= -0.2 和 y=60 处添加文本,字体大小为 12。请在下面找到我的代码:
蟒蛇3
# 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)
输出: