6-6 散点图的衍生图形

联合变量分布的散点图

jointplot()函数可以创建一个多面板图形来展示两个变量之间的联合关系,并同时展示每个轴上单变量的分布情况。

seaborn.jointplot(

x, y : 绘图用数据,可以是数据框内的变量名
data = None : 数据框名称

kind = ‘scatter’ : 绘制的图形种类
{ “scatter” | “reg” | “resid” | “kde” | “hex” }
stat_func = : 需要计算的统计量
该参数在新版本中已经取消

图形格式:
color : matplotlib color
size = 6 : 图形大小(默认方形)
ratio = 5 : 联合图和边上的边际分布图宽度比例
space = 0.2 : 联合图和边际分布图中间的缝隙宽度

dropna = True : 是否删除缺失数据
{x, y}lim : two-tuples, x/y轴的刻度范围
{joint, marginal, annot}_kws : dicts, 各图形元素的属性设定
) 返回:JointGrid对象。

sns.scatterplot(x = "s3", y = "index1", data = ccss)

在这里插入图片描述

sns.jointplot(x = "s3", y = "index1", data = ccss)

在这里插入图片描述

sns.jointplot(x = "s3", y = "index1", data = ccss, kind = 'reg')

在这里插入图片描述

# 错误但不影响使用的调用方法
sns.jointplot(x = ccss.s3, y = ccss.index1, data = ccss)

Hexbin plot

对于大数据集,散点图将会过于密集,难以考察关联趋势。

hexbin图(也称向日葵图)可以将x、y变量按照六角形单元格为单位进行汇总,然后用颜色深浅展示单元格内的样本频数,这种图形对于相对大的数据集效果最好。

该图形可以通过matplotlib的plt.hexbin函数使用,也可以作为jointplot的一种类型参数使用

# 使用jointplot绘制hexbin图
sns.jointplot(x = "s3", y = "index1", data = ccss, kind = 'hex')

在这里插入图片描述

# 使用jointplot绘制hexbin图
plt.hexbin(x = ccss.s3, y = ccss.index1, gridsize = (40, 40))

在这里插入图片描述

等高线图

可以使用KDE估计来可视化双变量分布。在seaborn中,这种绘图以等高线图展示,并且可以作为jointplot()的一种类型参数使用。
在这里插入图片描述

除使用jointplot,kdeplot绘制外,新版seaborn还可以使用displot绘制等高线图。

sns.jointplot(x = "s3", y = "index1", data = ccss, kind = 'kde')

在这里插入图片描述

# 只绘制等高线图
sns.kdeplot(x = "s3", y = "index1", data = ccss)

在这里插入图片描述

# 让曲线过渡尽量平滑
sns.kdeplot(x = "s3", y = "index1", data = ccss, shade = True, n_levels = 10)

在这里插入图片描述

# 让曲线过渡尽量平滑
sns.kdeplot(x = "s3", y = "index1", data = ccss, shade = True, n_levels = 20)

在这里插入图片描述

# 让曲线过渡尽量平滑
sns.kdeplot(x = "s3", y = "index1", data = ccss, shade = True, n_levels = 40)

在这里插入图片描述

# 在图形边加绘数值条
sns.kdeplot(x = "s3", y = "index1", data = ccss, shade = True, 
            cbar = True, n_levels = 40)

在这里插入图片描述

jointplot()在绘制后返回JointGrid对象,可以使用它添加更多图层或调整可视化的其他方面:

seaborn.JointGrid对象的方法:

annotate(func[, template, stat, loc]) 添加指定的统计量作为注解。
plot(joint_func, marginal_func[, annot_func]) 绘制完整图形。
plot_joint(func, **kwargs) 绘制x和y的双变量图。
plot_marginals(func, **kwargs) 分别绘制x和y的单变量图。
savefig(*args, **kwargs) 保存图形。
set_axis_labels([xlabel, ylabel]) 设置轴标签。

g = sns.jointplot(x = "s3", y = "index1", data = ccss, kind = 'kde', 
                  n_levels = 40, color = "m")
# 在已有图形基础上加绘散点图
g.plot_joint(plt.scatter, c = "w", s = 30, linewidth = 1, marker = "+")

在这里插入图片描述

# 在一条语句中完成上述操作
sns.jointplot(x = "s3", y = "index1", data = ccss, kind = 'kde', 
              n_levels = 40, color = "m")\
    .plot_joint(plt.scatter, c = "w", s = 30, lw = 1, marker = "+")
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

juicy-hua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值