如何结合两个seaborn图? [英] How to combine two seaborn plots?

查看:85
本文介绍了如何结合两个seaborn图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

替换 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) 查看参数似乎没有一种简单的方法可以实现这一点:

 参数----------x,y:字符串或向量数据或``数据''中的变量名称.数据:数据帧,可选当 x 和 y 是变量名时的 DataFrame.种类:{分散"|"reg" |居住" |"kde" |十六进制"},可选要绘制的情节.stat_func : 可调用或无,可选用于计算有关该关系的统计量的函数注释情节.应该将 `x` 和 `y` 映射到单个值或一个(值,p)元组.如果您不想,请设置为无"注释剧情.颜色 : matplotlib 颜色,可选用于绘图元素的颜色.大小:数字,可选图的大小(将为正方形).比率:数字,可选关节轴大小与边缘轴高度的比率.空格:数字,可选关节轴和边缘轴之间的空间dropna:bool,可选如果为 True,则删除 ``x`` 和 ``y`` 中缺失的观察.{x, y}lim : 二元组,可选绘制前要设置的轴限制.{joint, margin, annot}_kws : dicts, 可选绘图组件的其他关键字参数.kwargs:键,值配对额外的关键字参数被传递给用于在关节轴上绘制图,并取代``joint_kws`` 字典.

或者有吗?

感谢您的任何建议!

解决方案

似乎您只是想将 regplot 绘制到 jointplot 的主轴上.

将 numpy 导入为 np将熊猫作为pd导入将 seaborn 作为 sns 导入导入matplotlib.pyplot作为pltsns.set(style="white")rs = np.random.RandomState(5)平均值 = [0, 0]cov = [(1, .5), (.5, 1)]x1, x2 = rs.multivariate_normal(mean, cov, 500).Tx1 = 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()

From the seaborn docs, the following snippet will produce the plot below:

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)

Replacing g = sns.jointplot(x1, x2, kind="kde", size=7, space=0) with g = sns.jointplot(x1, x2, kind="reg", size=7, space=0) will produce this:

But I would like to plot the density plot together with a regression line. Something like this:

Looking at the parameters using help(sns.jointplot) it doesn't seem like there is an easy way to achieve this:

 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.

Or is there?

Thank you for any suggestions!

解决方案

It seems you simply want to plot a regplot into the main axes of the 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()

这篇关于如何结合两个seaborn图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆