seaborn.JointGrid

译者:Yet-sun

  1. class seaborn.JointGrid(x, y, data=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, size=None)

用于绘制具有边际单变量图的双变量图的网格。

  1. __init__(x, y, data=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, size=None)

设置子图的网格。

参数:x, y:字符串或向量

data中的数据或变量名

data:DataFrame, 可选

x and y 是变量名的时候为 DataFrame。

height:数字

图中每一条边的大小(以英寸为单位)

ratio:数字

联合轴大小与边缘轴高度的比率。

space:数字,可选

联合轴和边缘轴之间的空间

dropna:bool, 可选

如果为 True,则删除 xy中缺少的观察结果。

{x, y}lim:二元组,可选

在绘图之前设置轴限制。

也可以看看

用于绘制具有多种不同默认绘图类型的双变量图的高级界面。

例子:

初始化图形,但不在其上绘制任何图形:

  1. >>> import seaborn as sns; sns.set(style="ticks", color_codes=True)
  2. >>> tips = sns.load_dataset("tips")
  3. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips)

http://seaborn.pydata.org/_images/seaborn-JointGrid-1.png

使用默认参数添加绘图:

  1. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips)
  2. >>> g = g.plot(sns.regplot, sns.distplot)

http://seaborn.pydata.org/_images/seaborn-JointGrid-2.png

分别绘制联合分布图和边缘直方图,这可以以更精细的级别控制其他参数:

  1. >>> import matplotlib.pyplot as plt
  2. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips)
  3. >>> g = g.plot_joint(plt.scatter, color=".5", edgecolor="white")
  4. >>> g = g.plot_marginals(sns.distplot, kde=False, color=".5")

http://seaborn.pydata.org/_images/seaborn-JointGrid-3.png

分别绘制两个边缘直方图:

  1. >>> import numpy as np
  2. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips)
  3. >>> g = g.plot_joint(plt.scatter, color="m", edgecolor="white")
  4. >>> _ = g.ax_marg_x.hist(tips["total_bill"], color="b", alpha=.6,
  5. ... bins=np.arange(0, 60, 5))
  6. >>> _ = g.ax_marg_y.hist(tips["tip"], color="r", alpha=.6,
  7. ... orientation="horizontal",
  8. ... bins=np.arange(0, 12, 1))

http://seaborn.pydata.org/_images/seaborn-JointGrid-4.png

添加注释,其中包含总结双变量关系的统计信息:

  1. >>> from scipy import stats
  2. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips)
  3. >>> g = g.plot_joint(plt.scatter,
  4. ... color="g", s=40, edgecolor="white")
  5. >>> g = g.plot_marginals(sns.distplot, kde=False, color="g")
  6. >>> g = g.annotate(stats.pearsonr)

http://seaborn.pydata.org/_images/seaborn-JointGrid-5.png

使用自定义的函数和注释格式

  1. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips)
  2. >>> g = g.plot_joint(plt.scatter,
  3. ... color="g", s=40, edgecolor="white")
  4. >>> g = g.plot_marginals(sns.distplot, kde=False, color="g")
  5. >>> rsquare = lambda a, b: stats.pearsonr(a, b)[0] ** 2
  6. >>> g = g.annotate(rsquare, template="{stat}: {val:.2f}",
  7. ... stat="$R^2$", loc="upper left", fontsize=12)

http://seaborn.pydata.org/_images/seaborn-JointGrid-6.png

移除联合轴和边缘轴之间的空间:

  1. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips, space=0)
  2. >>> g = g.plot_joint(sns.kdeplot, cmap="Blues_d")
  3. >>> g = g.plot_marginals(sns.kdeplot, shade=True)

http://seaborn.pydata.org/_images/seaborn-JointGrid-7.png

绘制具有相对较大边缘轴的较小图:

  1. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips,
  2. ... height=5, ratio=2)
  3. >>> g = g.plot_joint(sns.kdeplot, cmap="Reds_d")
  4. >>> g = g.plot_marginals(sns.kdeplot, color="r", shade=True)

http://seaborn.pydata.org/_images/seaborn-JointGrid-8.png

设置轴的限制:

  1. >>> g = sns.JointGrid(x="total_bill", y="tip", data=tips,
  2. ... xlim=(0, 50), ylim=(0, 8))
  3. >>> g = g.plot_joint(sns.kdeplot, cmap="Purples_d")
  4. >>> g = g.plot_marginals(sns.kdeplot, color="m", shade=True)

http://seaborn.pydata.org/_images/seaborn-JointGrid-9.png

方法

__init__(x, y[, data, height, ratio, space, …]) | 设置子图的网格设置子图的网格。

annotate(func[, template, stat, loc]) | 用关于关系的统计数据来标注绘图。

plot(joint_func, marginal_func[, annot_func]) | 绘制完整绘图的快捷方式。

plot_joint(func, **kwargs) | 绘制 xy的双变量图。

plot_marginals(func, **kwargs) | 分别绘制 xy 的单变量图。

savefig(args, *kwargs) | 封装 figure.savefig 默认为紧边界框。

set_axis_labels([xlabel, ylabel]) |在双变量轴上设置轴标签。