seaborn(2)---画分类图/分布图/回归图/矩阵图

二.分类图

1. 分类散点图

(1)散点图striplot(kind='strip')

方法1:

seaborn.stripplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=True, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)

方法2:catplot的kind默认=striplot

sns.catplot(x="sepal_length", y="species", data=iris)

  

(2)带分布的散点图swarmplot(kind='swarm'

方法1:

seaborn.swarmplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)

方法2:

 sns.catplot(x="sepal_length", y="species", kind="swarm", data=iris)   

 

2. 分类分布图

(1)箱线图boxplot(kind='box'

方法1:

seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)

方法2:

sns.catplot(x="sepal_length", y="species", data=iris)

  

(2)小提琴图violinplot(kind='violin'

方法1:

seaborn.violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)

方法2:

sns.catplot(x="sepal_length", y="species", kind="violin", data=iris)

 

(3)boxenplot(kind='boxen')

方法1:

seaborn.boxenplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, k_depth='proportion', linewidth=None, scale='exponential', outlier_prop=None, ax=None, **kwargs)

方法2:

sns.catplot(x="species", y="sepal_length", kind="boxen", data=iris)

  

3. 分类估计图

(1)pointplot(kind='point')

方法1:

seaborn.pointplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, markers='o', linestyles='-', dodge=False, join=True, scale=1, orient=None, color=None, palette=None, errwidth=None, capsize=None, ax=None, **kwargs)

方法2:

sns.catplot(x="sepal_length", y="species", kind="point", data=iris)

  

(2)直方图barplot(kind='bar'

方法1:

seaborn.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)

方法2:

sns.catplot(x="sepal_length", y="species", kind="bar", data=iris)

  

(3)计数的直方图countplot(kind='count'

方法1:

seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)

方法2:

sns.catplot(x="species", kind="count", data=iris)

  

三.分布图

包括单变量核密度曲线,直方图,双变量多变量的联合直方图,和密度图

1.单分布

(1)distpot

方法:

seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, fit=None, hist_kws=None, kde_kws=None, rug_kws=None, fit_kws=None, color=None, vertical=False, norm_hist=False, axlabel=None, label=None, ax=None)
  • seaborn.distplot

  • 设置 kde=False 则可以只绘制直方图,或者 hist=False 只绘制核密度估计图

举例:

sns.distplot(iris["sepal_length"])

  

(2)kdeplot

方法:

seaborn.kdeplot(data, data2=None, shade=False, vertical=False, kernel='gau', bw='scott', gridsize=100, cut=3, clip=None, legend=True, cumulative=False, shade_lowest=True, cbar=False, cbar_ax=None, cbar_kws=None, ax=None, **kwargs)
  • kdeplot 可以专门用于绘制核密度估计图,其效果和 distplot(hist=False) 一致,但 kdeplot 拥有更多的自定义设置
  • seaborn.kdeplot

举例:

sns.kdeplot(iris["sepal_length"])

  

2.双分布

(1)jointplot二元变量分布图

方法:

seaborn.jointplot(x, y, data=None, kind='scatter', stat_func=None, color=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, joint_kws=None, marginal_kws=None, annot_kws=None, **kwargs)
  • seaborn.jointplot

  • jointplot 并不是一个 Figure-level 接口,但其支持 kind= 参数指定绘制出不同样式的分布图。例如,绘制出核密度估计对比图 kind = 'kde'。
  • kind='hex'绘制六边形计数图
  • kind='reg'绘制回归拟合图

举例:

例如,我们探寻 sepal_length 和 sepal_width 二元特征变量之间的关系。

sns.jointplot(x="sepal_length", y="sepal_width", data=iris)

  

(2)pairpot

支持将数据集中的特征变量两两对比绘图,默认情况下,对角线上是单变量分布图,而其他是二元变量分布图

方法:

seaborn.pairplot(data, hue=None, hue_order=None, palette=None, vars=None, x_vars=None, y_vars=None, kind='scatter', diag_kind='auto', markers=None, height=2.5, aspect=1, dropna=True, plot_kws=None, diag_kws=None, grid_kws=None, size=None)

举例:

sns.pairplot(iris, hue="species")

  

(3)rugplot

 方法:

seaborn.rugplot(a, height=0.05, axis='x', ax=None, **kwargs)

  

 

四.回归图

回归图只要探讨两连续数值变量的变化趋势情况,绘制x-y的散点图和回归曲线。

1.lmplot

方法:

seaborn.lmplot(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, height=5, aspect=1, markers='o', sharex=True, sharey=True, hue_order=None, col_order=None, row_order=None, legend=True, legend_out=True, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None, size=None)
  • seaborn.lmplot
  • lmplot 同样是用于绘制回归图,但 lmplot 支持引入第三维度进行对比,例如我们设置 hue="species"

举例:

sns.lmplot(x="sepal_length", y="sepal_width", hue="species", data=iris)

  

2.regplot

方法:用线性回归模型对数据做拟合

seaborn.regplot(x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)
  • seaborn.regplot
  • regplot 绘制回归图时,只需要指定自变量和因变量即可,regplot 会自动完成线性回归拟合。

举例:

sns.regplot(x="sepal_length", y="sepal_width", data=iris)

  

3.residplot

方法:展示线性回归模型拟合后各点对应的残值

seaborn.residplot(x, y, data=None, lowess=False, x_partial=None, y_partial=None, order=1, robust=False, dropna=True, label=None, color=None, scatter_kws=None, line_kws=None, ax=None)

  

 举例:可以对以年为单位的地震记录作线性回归拟合。以下两张图分别对应一阶线性回归拟合、拟合后残值分布情况图。

plt.figure(figsize=(12,6))
plt.subplot(121) 
sns.regplot(x="Year", y="ID",
data=temp,order=1) # default by 1plt.ylabel(' ')
plt.title('Regression fit of earthquake records by year,order = 1')
 
plt.subplot(122)
sns.residplot(x="Year", y="ID",
data=temp)
plt.ylabel(' ')
plt.title('Residual plot when using a simplt regression
model,order=1')
plt.show()

  

 

 

五.矩阵图

1.热力图heatmap

方法:用颜色矩阵去显示数据在两个维度下的度量值

seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, **kwargs)
  • 热力图在某些场景下非常实用,例如绘制出变量相关性系数热力图。
  • seaborn.heatmap

举例:

import numpy as np

sns.heatmap(np.random.rand(10, 10))

  

2.聚类图clustermap

方法:

seaborn.clustermap(data, pivot_kws=None, method='average', metric='euclidean', z_score=None, standard_scale=None, figsize=None, cbar_kws=None, row_cluster=True, col_cluster=True, row_linkage=None, col_linkage=None, row_colors=None, col_colors=None, mask=None, **kwargs)
  • 除此之外,clustermap 支持绘制层次聚类结构图。如下所示,我们先去掉原数据集中最后一个目标列,传入特征数据即可。当然,你需要对层次聚类有所了解,否则很难看明白图像多表述的含义。
  • seaborn.clustermap

举例:

iris.pop("species")
sns.clustermap(iris)

  

六.时间序列图

1.时间序列图tsplot

用时间维度序列去展现数据的不确定性

2.网格时序图plot_ts_d , plot_ts_m

 

七.多绘图网格

1.小平面网格

1.1 FaceGrid

1.2 FacetGrid.map

1.3 FacetGrid.map_dataframe

 

2.配对网格

2.1 PairGrid

2.2 PairGrid.map

2.3 PairGrid.map_diag

2.4 PairGrid.map_offdiag

2.5 PairGrid.map_lower

2.6 PairGrid.map_upper

 

3.联合网格

3.1 JointGrid

3.2 JointGrid.plot

3.3 JointGrid.plot_joint

3.4 JointGrid.plot_marginals

 

 

 

参考文献:

【1】Python学习笔记——数据分析之Seaborn绘图

【2】5种方法教你用Python玩转histogram直方图

【3】机器学习开放课程:二、使用Python可视化数据

【4】python可视化进阶---seaborn1.7 分类数据可视化 - 统计图 barplot() / countplot() / pointplot()

【5】seaborn官网

【6】Seaborn 数据可视化基础教程

转载于:https://www.cnblogs.com/nxf-rabbit75/p/11157229.html

首先需要安装必要的库,包括pandas、matplotlib、seaborn和scikit-learn。然后可以使用以下代码获取tips数据集: ```python import pandas as pd import seaborn as sns tips = sns.load_dataset("tips") ``` 接下来可以绘制不同类型的表: 1. 散点 散点可以用于展示不同变量之间的关系,例如总消费金额和小费金额之间的关系: ```python sns.scatterplot(data=tips, x="total_bill", y="tip") ``` 2. 直方 直方可以用于展示数值数据的分布情况,例如小费金额的分布: ```python sns.histplot(data=tips, x="tip") ``` 3. 箱线 箱线可以用于展示数值数据的分布情况和离群值情况,例如不同时间和性别的小费金额分布: ```python sns.boxplot(data=tips, x="time", y="tip", hue="sex") ``` 4. 散点矩阵 散点矩阵可以用于展示多个变量之间的关系,例如总消费金额、小费金额和就餐人数之间的关系: ```python sns.pairplot(data=tips, vars=["total_bill", "tip", "size"]) ``` 5. 小提琴 小提琴可以用于展示数值数据的分布情况和离群值情况,且比箱线更能展示密度分布,例如不同时间和性别的小费金额分布: ```python sns.violinplot(data=tips, x="time", y="tip", hue="sex", split=True) ``` 6. 回归 回归可以用于展示两个数值变量之间的关系,并可以拟合线性回归模型,例如总消费金额和小费金额之间的关系: ```python sns.regplot(data=tips, x="total_bill", y="tip") ``` 7. 关系类图 关系类图可以用于展示多个变量之间的关系,例如总消费金额、小费金额、就餐时间和性别之间的关系: ```python sns.relplot(data=tips, x="total_bill", y="tip", hue="time", style="sex") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值