📜  Python – seaborn.PairGrid() 方法

📅  最后修改于: 2022-05-13 01:55:48.314000             🧑  作者: Mango

Python – seaborn.PairGrid() 方法

先决条件: Seaborn 编程基础

Seaborn 是一个基于 matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。 Seaborn 帮助解决了 Matplotlib 面临的两大问题;问题是什么?

  • 默认 Matplotlib 参数
  • 使用数据框

随着 Seaborn 对 Matplotlib 的补充和扩展,学习曲线非常缓慢。如果您了解 Matplotlib,那么您已经完成了 Seaborn 的一半。

seaborn.PairGrid() :

  • 用于在数据集中绘制成对关系的子图网格。
  • 此类将数据集中的每个变量映射到多轴网格中的列和行。可以使用不同的轴级绘图函数在上下三角形中绘制双变量图,并且可以在对角线上显示每个变量的边际分布。
  • 它还可以使用色调参数表示附加级别的条件化,以不同颜色绘制不同的数据子集。这使用颜色来解析第三维上的元素,但仅在彼此之上绘制子集,并且不会像接受色调的轴级函数那样为特定可视化定制色调参数。
seaborn.PairGrid( data, \*\*kwargs)

Seaborn.PairGrid 使用许多参数作为输入,下面以表格的形式描述其中的主要参数:

Arguments      Description                                                                                                                                Value
dataTidy (long-form) dataframe where each column is a variable and each row is an observation.DataFrame
hueVariable in “data“ to map plot aspects to different colors.string (variable name), optional
paletteSet of colors for mapping the “hue“ variable. If a dict, keys should be values  in the “hue“ variable.dict or seaborn color palette
vars Variables within “data“ to use, otherwise use every column with a numeric datatype.list of variable names, optional
dropnaDrop missing values from the data before plotting.boolean, optional

下面是上述方法的实现:

示例 1:

Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading dataset
df = seaborn.load_dataset('tips')
  
# PairGrid object with hue
graph = seaborn.PairGrid(df, hue ='day')
# type of graph for diagonal
graph = graph.map_diag(plt.hist)
# type of graph for non-diagonal
graph = graph.map_offdiag(plt.scatter)
# to add legends
graph = graph.add_legend()
# to show
plt.show()
# This code is contributed by Deepanshu Rusatgi.


Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading dataset
df = seaborn.load_dataset('tips')
  
# PairGrid object with hue
graph = seaborn.PairGrid(df)
# type of graph for non-diagonal(upper part)
graph = graph.map_upper(sns.scatterplot)
# type of graph for non-diagonal(lower part)
graph = graph.map_lower(sns.kdeplot)
# type of graph for diagonal
graph = graph.map_diag(sns.kdeplot, lw = 2)
# to show
plt.show()
# This code is contributed by Deepanshu Rusatgi.


输出 :

示例 2:

Python3

# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading dataset
df = seaborn.load_dataset('tips')
  
# PairGrid object with hue
graph = seaborn.PairGrid(df)
# type of graph for non-diagonal(upper part)
graph = graph.map_upper(sns.scatterplot)
# type of graph for non-diagonal(lower part)
graph = graph.map_lower(sns.kdeplot)
# type of graph for diagonal
graph = graph.map_diag(sns.kdeplot, lw = 2)
# to show
plt.show()
# This code is contributed by Deepanshu Rusatgi.

输出: