zoukankan      html  css  js  c++  java
  • sns.jointplot()双变量图,也就是双变量独自的分布图,以及这双变量的相关性图

    数据分析中常用做图的方式实现相关性分析,即X轴设置为变量A,Y轴设置为变量B,做散点图,由于散点图中点的叠加显示,往往还需要关注每个变量自身的分布情况,jointplot把描述变量的分布图和变量相关的散点图组合在一起,是相关性分析最常用的工具,图片上还能展示回归曲线,以及相关系数
    import statsmodels.api as sm
    import seaborn as sns
    sns.set(style="darkgrid")
    data = sm.datasets.ccard.load_pandas().data
    g = sns.jointplot('AVGEXP', 'AGE', data=data, kind="reg",
                     xlim=(0, 1000), ylim=(0, 50), color="m")

     下面我们看一下参数以及常见例子

    seaborn.jointplot(*, x=None, y=None, data=None, kind='scatter', color=None, height=6, ratio=5, space=0.2,
    dropna=False, xlim=None, ylim=None, marginal_ticks=False, joint_kws=None, marginal_kws=None,
    hue=None, palette=None, hue_order=None, hue_norm=None, **kwargs)

    kind:{ “scatter” | “kde” | “hist” | “hex” | “reg” | “resid” }

    主要了解一下这个kind参数,其余的可以参考官网:http://seaborn.pydata.org/generated/seaborn.jointplot.html?highlight=jointplot#seaborn.jointplot

    下面代码全部都是在seaborn0.11.0版本下实现,可以使用

    import seaborn as sns 
    sns.__version__  #检查模块版本

    如果不是,则需要升级版本

    pip install -U 模块名   # 这是 python2+ 版本的用法更新模块
    pip3 install -U 模块名   # 这是 python3+ 版本的用法更新模块

    在最简单的调用中,只有x和y默认的是散点图和直方图

    penguins = sns.load_dataset("penguins")
    sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm")

     增加hue变量将为散点图添加条件颜色,并kdeplot()在边沿轴上绘制单独的密度曲线(使用

    sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species")

    通过该kind参数可以使用几种不同的绘图方法设置kind="kde"将绘制双变量和单变量KDE

    sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", hue="species", kind="kde")

     设置kind="reg"以添加线性回归拟合(使用regplot())和单变量KDE曲线

    sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="reg")

     对于基于bin的关节分布可视化,还有两个选项。第一个使用kind="hist"histplot()在所有轴上使用

    sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="hist")

     或者,设置kind="hex"将使用matplotlib.axes.Axes.hexbin()六角形箱来计算双变量直方图

    sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", kind="hex")

     可以将其他关键字参数传递给基础图

    sns.jointplot(
        data=penguins, x="bill_length_mm", y="bill_depth_mm",
        marker="+", s=100, marginal_kws=dict(bins=25, fill=False),
    )

     使用JointGrid参数来控制图形的大小和布局

    sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm", height=5, ratio=2, marginal_ticks=True)

     要将更多层添加到绘图上,请使用返回JointGrid对象上的方法jointplot()

    g = sns.jointplot(data=penguins, x="bill_length_mm", y="bill_depth_mm")
    g.plot_joint(sns.kdeplot, color="r", zorder=0, levels=6)
    g.plot_marginals(sns.rugplot, color="r", height=-.15, clip_on=False)

     
  • 相关阅读:
    PAT乙级题库“傻瓜”题解之跟奥巴马一起编程
    PAT乙级题库“傻瓜”题解之划拳
    PAT乙级题库“傻瓜”题解之数素数
    PAT乙级题库“傻瓜”题解之编程团体赛
    PAT乙级题库“傻瓜”题解之判断题
    PAT乙级题库“傻瓜”题解之输出PATest
    有始有终,后会无期。
    今日德语大学习
    【day
    [day 3] Deutsch Studie
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/14095786.html
Copyright © 2011-2022 走看看