联合变量分布的散点图
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 = "+")