【问题标题】:How to combine two seaborn plots?如何结合两个seaborn地块?
【发布时间】:2018-08-03 11:57:33
【问题描述】:

seaborn docs,下面的 sn-p 将产生下面的图:

import numpy as np
import pandas as pd
import seaborn as sns
sns.set(style="white")

# Generate a random correlated bivariate dataset
rs = np.random.RandomState(5)
mean = [0, 0]
cov = [(1, .5), (.5, 1)]
x1, x2 = rs.multivariate_normal(mean, cov, 500).T
x1 = pd.Series(x1, name="$X_1$")
x2 = pd.Series(x2, name="$X_2$")

# Show the joint distribution using kernel density estimation
g = sns.jointplot(x1, x2, kind="kde", size=7, space=0)

g = sns.jointplot(x1, x2, kind="kde", size=7, space=0) 替换为 g = sns.jointplot(x1, x2, kind="reg", size=7, space=0) 会产生这个:

但我想将密度图与回归线一起绘制。像这样的:

使用help(sns.jointplot查看参数似乎没有一种简单的方法可以实现这一点:

 Parameters
----------
x, y : strings or vectors
    Data or names of variables in ``data``.
data : DataFrame, optional
    DataFrame when ``x`` and ``y`` are variable names.
kind : { "scatter" | "reg" | "resid" | "kde" | "hex" }, optional
    Kind of plot to draw.
stat_func : callable or None, optional
    Function used to calculate a statistic about the relationship and
    annotate the plot. Should map `x` and `y` either to a single value
    or to a (value, p) tuple. Set to ``None`` if you don't want to
    annotate the plot.
color : matplotlib color, optional
    Color used for the plot elements.
size : numeric, optional
    Size of the figure (it will be square).
ratio : numeric, optional
    Ratio of joint axes size to marginal axes height.
space : numeric, optional
    Space between the joint and marginal axes
dropna : bool, optional
    If True, remove observations that are missing from ``x`` and ``y``.
{x, y}lim : two-tuples, optional
    Axis limits to set before plotting.
{joint, marginal, annot}_kws : dicts, optional
    Additional keyword arguments for the plot components.
kwargs : key, value pairings
    Additional keyword arguments are passed to the function used to
    draw the plot on the joint Axes, superseding items in the
    ``joint_kws`` dictionary.

还有吗?

感谢您的任何建议!

【问题讨论】:

    标签: python matplotlib regression seaborn


    【解决方案1】:

    您似乎只是想将regplot 绘制到jointplot 的主轴中。

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(style="white")
    
    rs = np.random.RandomState(5)
    mean = [0, 0]
    cov = [(1, .5), (.5, 1)]
    x1, x2 = rs.multivariate_normal(mean, cov, 500).T
    x1 = pd.Series(x1, name="$X_1$")
    x2 = pd.Series(x2, name="$X_2$")
    
    g = sns.jointplot(x1, x2, kind="kde", size=7, space=0)
    sns.regplot(x1,x2, scatter=False, ax=g.ax_joint)
    plt.show()
    

    【讨论】:

    • 谢谢!正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2019-12-23
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多