Python3 - seaborn: pairplot(),PairGrid(),fill,scatter,hist2d,map_diag/upper/lower,FacetGrid...有料

本博客是在Jupyter Notebooks上进行练习:
如果想知道如何创建Jupyter, 请看这里

使用的数据:

import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
print(iris.head())

结果如下:
在这里插入图片描述

# Plot pairwise relationships in a dataset
# By default, this function will create a grid of Axes such that each numeric variable in data will by shared across the y-axes 
#     across a single row and the x-axes across a single column. 
# The diagonal plots are treated differently: a univariate distribution plot is drawn to show the marginal distribution of the data in each column.
print(sns.pairplot(iris))

结果如下:
在这里插入图片描述
使用PairGrid():

print(sns.PairGrid(iris))

结果如下:
在这里插入图片描述
要想了解更多有关matplotlib.pyplot 的函数,请点击这里

使用PairGride.map()和scatter

# 使用PairGrid可以自定义图形
# PairGrid: Subplot grid for plotting pairwise relationships in a dataset.
# This object maps each variable in a dataset onto a column and row in a grid of multiple axes.
# Different axes-level plotting functions can be used to draw bivariate plots in the upper and lower triangles, and the marginal distribution of each
#    variable can be shown on the diagonal.
g = sns.PairGrid(iris)
# PairGrid.map() will draw a bivariate plot on every axes
# plt.scatter: A scatter plot of y vs x with varying marker size and/or color
g.map(plt.scatter)

结果如下:
在这里插入图片描述
使用fill:

# 使用PairGrid可以自定义图形
# PairGrid: Subplot grid for plotting pairwise relationships in a dataset.
# This object maps each variable in a dataset onto a column and row in a grid of multiple axes.
# Different axes-level plotting functions can be used to draw bivariate plots in the upper and lower triangles, and the marginal distribution of each
#    variable can be shown on the diagonal.
g = sns.PairGrid(iris)
# PairGrid.map() will draw a bivariate plot on every axes
# plt.fill: Plot filled polygons
g.map(plt.fill)

结果如下:
在这里插入图片描述
使用hist2d:

# 使用PairGrid可以自定义图形
# PairGrid: Subplot grid for plotting pairwise relationships in a dataset.
# This object maps each variable in a dataset onto a column and row in a grid of multiple axes.
# Different axes-level plotting functions can be used to draw bivariate plots in the upper and lower triangles, and the marginal distribution of each
#    variable can be shown on the diagonal.
g = sns.PairGrid(iris)
# PairGrid.map() will draw a bivariate plot on every axes
# plt.hist2d: Make a 2D histogram plot
g.map(plt.hist2d)

结果如下:
在这里插入图片描述
使用map_diag, map_upper, map_lower:

g = sns.PairGrid(iris)
# PairGrid.map_diag(): Plot with a univariate function on each diagonal subplot.
g.map_diag(sns.distplot)
# PairGrid.map_upper(): Plot with a bivariate function on the upper diagonal subplots.
g.map_upper(plt.scatter)
# PairGrid.map_lower(): Plot with a bivariate function on the lower diagonal subplots.
g.map_lower(sns.kdeplot)

结果如下:
在这里插入图片描述
有可能看不懂map_diag, map_upper, map_lower所代表什么意思,没事~
博主现在将它们拆开一个个展示,这样就很明了了。

使用map_diag():

g = sns.PairGrid(iris)
# PairGrid.map_diag(): Plot with a univariate function on each diagonal subplot.
g.map_diag(sns.distplot)

结果如下:
在这里插入图片描述
使用map_upper():

g = sns.PairGrid(iris)
# PairGrid.map_upper(): Plot with a bivariate function on the upper diagonal subplots.
g.map_upper(plt.scatter)

结果如下:
在这里插入图片描述
使用map_lower():

g = sns.PairGrid(iris)
# PairGrid.map_lower(): Plot with a bivariate function on the lower diagonal subplots.
g.map_lower(sns.kdeplot)

结果如下:
在这里插入图片描述


现在使用另外一个dataset:

tips = sns.load_dataset('tips')
print(tips.head())

结果如下:
在这里插入图片描述
使用FacetGrid():

# FacetGrid: Multi-plot grid for plotting conditional relationships
g = sns.FacetGrid(data=tips,col='time',row='smoker')
# To draw a plot on every facet, pass a function and the name of one or more columns in the dataframe to FacetGrid.map()
g.map(sns.distplot, 'total_bill')

结果如下:
在这里插入图片描述
使用sns.scatterplot:

# FacetGrid: Multi-plot grid for plotting conditional relationships
g = sns.FacetGrid(data=tips,col='time',row='smoker')
# To draw a plot on every facet, pass a function and the name of one or more columns in the dataframe to FacetGrid.map()
g.map(sns.scatterplot, 'total_bill', 'tip')

结果如下:
在这里插入图片描述
使用plt.scatter:

g = sns.FacetGrid(data=tips,col='time',row='smoker')
g.map(plt.scatter, 'total_bill','tip')

结果如下:
在这里插入图片描述
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
让我来详细解释每一句代码的含义: 1. `g = sns.PairGrid(pd.DataFrame(X_train_transformed, columns=column_names).iloc[:,:3].sample(600, random_state=RANDOM_SEED))` - `pd.DataFrame(X_train_transformed, columns=column_names)`:将经过特征转换的训练数据 `X_train_transformed` 转换为一个DataFrame,并指定列名为 `column_names`。 - `.iloc[:,:3]`:选择DataFrame中的前3列作为绘图的变量。 - `.sample(600, random_state=RANDOM_SEED)`:随机抽样600个样本,用于绘制PairGrid图。 - `sns.PairGrid()`:创建一个PairGrid对象,用于绘制多个变量之间的关系。 2. `plt.subplots_adjust(top=0.9)` - `plt.subplots_adjust()`:调整子图布局。 - `top=0.9`:将子图顶部距离整个图的顶部的距离设置为0.9。 3. `g.fig.suptitle('After:')` - `g.fig.suptitle()`:为PairGrid图设置总标题。 - `'After:'`:总标题的文本为 'After:'。 4. `g.map_diag(sns.kdeplot)` - `g.map_diag()`:对角线上的每个子图应用指定的绘图函数。 - `sns.kdeplot`:使用Seaborn库中的kdeplot函数绘制核密度估计图。 5. `g.map_offdiag(sns.kdeplot)` - `g.map_offdiag()`:对角线以外的每个子图应用指定的绘图函数。 - `sns.kdeplot`:使用Seaborn库中的kdeplot函数绘制核密度估计图。 通过这些代码,你创建了一个PairGrid对象 `g`,然后对其进行了配置和绘图。PairGrid图展示了经过特征转换后的训练数据的前3个变量之间的关系。对角线上的子图显示了每个变量的核密度估计图,而非对角线上的子图显示了两个变量之间的核密度估计图。总标题为 'After:'。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值