本博客是在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')
结果如下:
如果觉得不错,就点赞或者关注或者留言~~
谢谢~ ~