Python – seaborn.pairplot() 方法
先决条件: Seaborn 编程基础
Seaborn 是一个基于 matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。 Seaborn 帮助解决了 Matplotlib 面临的两大问题;问题是什么?
- 默认 Matplotlib 参数
- 使用数据框
随着 Seaborn 对 Matplotlib 的补充和扩展,学习曲线非常缓慢。如果您了解 Matplotlib,那么您已经完成了 Seaborn 的一半。
seaborn.pairplot() :
要在数据集中绘制多个成对二元分布,可以使用 pairplot()函数。这将 DataFrame 中变量 (n, 2) 组合的关系显示为图矩阵,对角线图是单变量图。
seaborn.pairplot( data, \*\*kwargs )
Seaborn.pairplot 使用许多参数作为输入,其中主要以表格形式描述如下:Arguments Description Value data Tidy (long-form) dataframe where each column is a variable and each row is an observation. DataFrame hue Variable in “data“ to map plot aspects to different colors. string (variable name), optional palette Set of colors for mapping the “hue“ variable. If a dict, keys should be values in the “hue“ variable. vars : list of variable names, optional dict or seaborn color palette {x, y}_vars Variables within “data“ to use separately for the rows and columns of the figure; i.e. to make a non-square plot. lists of variable names, optional dropna Drop missing values from the data before plotting. boolean, optional
下面是上述方法的实现:
示例 1:
Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
############# Main Section ############
# loading dataset using seaborn
df = seaborn.load_dataset('tips')
# pairplot with hue sex
seaborn.pairplot(df, hue ='sex')
# to show
plt.show()
# This code is contributed by Deepanshu Rustagi.
Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
############# Main Section ############
# loading dataset using seaborn
df = seaborn.load_dataset('tips')
# pairplot with hue day
seaborn.pairplot(df, hue ='day')
# to show
plt.show()
# This code is contributed by Deepanshu Rustagi.
输出 :
示例 2:
Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
############# Main Section ############
# loading dataset using seaborn
df = seaborn.load_dataset('tips')
# pairplot with hue day
seaborn.pairplot(df, hue ='day')
# to show
plt.show()
# This code is contributed by Deepanshu Rustagi.