Seaborn.FacetGrid() 方法



当您想分别在数据集的子集中可视化变量的分布或多个变量之间的关系时,Seaborn.FacetGrid() 方法非常有用。Row、Col 和 Hue 是可以用来绘制 FacetGrid 的三个可能的维度。前两个明显对应于生成的轴数组。

使用 hue 参数,它以不同的颜色描绘几个数据子集,它还可以表达第三个变量的级别。与接收 hue 的轴级别函数相比,这只是将子集绘制在一起,并且不会为特定显示定制 hue 参数。它使用颜色来解析第三维上的组件。

语法

以下是 seaborn.FacetGrid() 方法的语法:

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)

参数

下面讨论 seaborn.FacetGrid() 方法的一些参数。

序号 名称和描述
1 数据

接收数据框,其中每一列是一个变量,每一行是一个观测值。

2 Row, col, hue

指定将在特定网格面上显示的数据部分的变量。要控制此变量的级别顺序,请参考 var order 参数。

3 Col_wrap

接收整数作为输入,并在此宽度处“换行”列变量,以使列面跨越多行。

4 Share{x,y}

接收布尔值或 col、row 作为值,如果为真,则各个面将在列上共享 y 轴和/或在行上共享 x 轴。

5 Height

接收标量值,并确定面的高度。

6 Aspect

接收标量值,每个面的纵横比,因此 aspect * height 给出每个面的宽度(以英寸为单位)。

7 Despine

接收布尔值,并从图中删除顶部和右侧的脊柱。

8 {row,col,hue}_order

接收列表作为输入,并由此顺序确定分面变量级别的顺序。

在继续绘制图表之前,让我们加载 seaborn 库和数据集。

加载 seaborn 库

要加载或导入 seaborn 库,可以使用以下代码行。

Import seaborn as sns

加载数据集

在本文中,我们将使用 seaborn 库中内置的泰坦尼克号数据集。使用以下命令加载数据集。

titanic=sns.load_dataset("titanic")

以下命令用于查看数据集中前 5 行。这使我们能够了解可以使用哪些变量来绘制图表。

titanic.head()

以下是上述代码段的输出。

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

现在我们已经加载了数据集,我们将探索一些示例。

示例 1

在此示例中,我们将看到如何通过将数据集以及 col 和 row 参数传递给 FacetGrid() 方法来生成简单的分面网格。

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()

输出

获得的输出如下:

Seaborn FacetGrid method

示例 2

为了在每个面上绘制图表,请将函数和数据框中一个或多个列的名称传递给 FacetGrid.map()。在此示例中,我们将执行相同的操作。我们使用泰坦尼克号数据集,并将散点图绘制在所有面上。

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()

可以使用上述代码行来实现所解释的场景,并给出以下获得的输出。

输出

Seaborn FacetGrid

示例 3

现在,我们将使用 hsitplot() 方法在所有面上绘制直方图,并将 add_legend 用于所有添加到显示图表的图例。这可以使用以下代码行来完成。首先,我们将绘制各个面并在这些面上绘制histplot。然后,我们将图例添加到各个面。为此,facetgrid 将存储在一个名为 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()

输出

获得的输出如下:

FacetGrid

示例 4

我们将了解 python 中 savefig() 方法的用法。这可以用来将获得的任何绘图保存为图像以供您个人使用。您的绘图必须像上面的示例一样存储在 grid 等变量中,然后我们将对其应用 savefig() 方法并获得图像。savefig() 方法接受一个参数,即保存绘图的文件名。在下面的代码中,绘图被保存为“facetplot.png”。

import seaborn as sns
import matplotlib.pyplot as plt
titanic=sns.load_dataset("titanic")
titanic.head()
grid.savefig("facetplot.png")
plt.show()
seaborn_multi_plot_grids_introduction.htm
广告
© . All rights reserved.