41.md 5.6 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6
# seaborn.JointGrid

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

Y
Yet-sun 已提交
7
用于绘制具有边际单变量图的双变量图的网格。
W
init  
wizardforcel 已提交
8 9 10 11 12

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

Y
Yet-sun 已提交
13
设置子图的网格。
W
init  
wizardforcel 已提交
14

Y
Yet-sun 已提交
15
参数:`x, y`:字符串或向量
W
init  
wizardforcel 已提交
16

Y
Yet-sun 已提交
17
> 在 `data`中的数据或变量名
W
init  
wizardforcel 已提交
18

Y
Yet-sun 已提交
19
`data`:DataFrame, 可选
W
init  
wizardforcel 已提交
20

Y
Yet-sun 已提交
21
> 当 `x` and `y` 是变量名的时候为DataFrame。
W
init  
wizardforcel 已提交
22

Y
Yet-sun 已提交
23
`height`:数字
W
init  
wizardforcel 已提交
24

Y
Yet-sun 已提交
25
> 图中每一条边的大小(以英寸为单位)
W
init  
wizardforcel 已提交
26

Y
Yet-sun 已提交
27
`ratio`:数字
W
init  
wizardforcel 已提交
28

Y
Yet-sun 已提交
29
> 联合轴大小与边缘轴高度的比率。
W
init  
wizardforcel 已提交
30

Y
Yet-sun 已提交
31
`space`:数字,可选
W
init  
wizardforcel 已提交
32

Y
Yet-sun 已提交
33
> 联合轴和边缘轴之间的空间
W
init  
wizardforcel 已提交
34

Y
Yet-sun 已提交
35
`dropna`:bool, 可选
W
init  
wizardforcel 已提交
36

Y
Yet-sun 已提交
37
> 如果为True,则删除 <cite>x</cite> 和 <cite>y</cite>中缺少的观察结果。
W
init  
wizardforcel 已提交
38

Y
Yet-sun 已提交
39
`{x, y}lim`:二元组,可选
W
init  
wizardforcel 已提交
40

Y
Yet-sun 已提交
41
> 在绘图之前设置轴限制。
W
init  
wizardforcel 已提交
42

Y
Yet-sun 已提交
43
也可以看看
W
wizardforcel 已提交
44

Y
Yet-sun 已提交
45
用于绘制具有多种不同默认绘图类型的双变量图的高级界面。
W
init  
wizardforcel 已提交
46

Y
Yet-sun 已提交
47
例子:
W
init  
wizardforcel 已提交
48

Y
Yet-sun 已提交
49
初始化图形,但不在其上绘制任何图形:
W
init  
wizardforcel 已提交
50 51 52 53 54 55 56 57 58 59

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-1.png](img/a0e79dac9add2a97da1c95241a6122ab.jpg)

Y
Yet-sun 已提交
60
使用默认参数添加绘图:
W
init  
wizardforcel 已提交
61 62 63 64 65 66 67 68 69

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-2.png](img/f984c858bd63441ea9761d632cb76d2c.jpg)

Y
Yet-sun 已提交
70
分别绘制联合分布图和边缘直方图,这可以以更精细的级别控制其他参数:
W
init  
wizardforcel 已提交
71 72 73 74 75 76 77 78 79 80 81

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-3.png](img/3e159b4a38edb79ede76d93a55e2acb9.jpg)

Y
Yet-sun 已提交
82
分别绘制两个边缘直方图:
W
init  
wizardforcel 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-4.png](img/1db698012d05626321ac93ffb7668a2c.jpg)

Y
Yet-sun 已提交
98
添加注释,其中包含总结双变量关系的统计信息:
W
init  
wizardforcel 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-5.png](img/fa99a0a13450712a4f2b13d983b1e766.jpg)

Y
Yet-sun 已提交
112
使用自定义的函数和注释格式
W
init  
wizardforcel 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-6.png](img/a8307f7ba7809b63c523168fde9e9379.jpg)

Y
Yet-sun 已提交
127
移除联合轴和边缘轴之间的空间:
W
init  
wizardforcel 已提交
128 129 130 131 132 133 134 135 136 137

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-7.png](img/5beaabfceb79e2eef9563fc3044dd5f6.jpg)

Y
Yet-sun 已提交
138
绘制具有相对较大边缘轴的较小图:
W
init  
wizardforcel 已提交
139 140 141 142 143 144 145 146 147 148 149

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-8.png](img/bfc4c60af4e09992569375d51943de88.jpg)

Y
Yet-sun 已提交
150
设置轴的限制:
W
init  
wizardforcel 已提交
151 152 153 154 155 156 157 158 159 160 161

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

```

![http://seaborn.pydata.org/_images/seaborn-JointGrid-9.png](img/3a85305cd59104b4d9403deb570373cc.jpg)

Y
Yet-sun 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174
方法

[`__init__`](#seaborn.JointGrid.__init__ "seaborn.JointGrid.__init__")(x, y[, data, height, ratio, space, …]) | 设置子图的网格设置子图的网格。

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

[`plot`](seaborn.JointGrid.plot.html#seaborn.JointGrid.plot "seaborn.JointGrid.plot")(joint_func, marginal_func[, annot_func]) | 绘制完整绘图的快捷方式。

 [`plot_joint`](seaborn.JointGrid.plot_joint.html#seaborn.JointGrid.plot_joint "seaborn.JointGrid.plot_joint")(func, **kwargs) | 绘制 <cite>x</cite><cite>y</cite>的双变量图。

[`plot_marginals`](seaborn.JointGrid.plot_marginals.html#seaborn.JointGrid.plot_marginals "seaborn.JointGrid.plot_marginals")(func, **kwargs) | 分别绘制 <cite>x</cite><cite>y</cite> 的单变量图。

 `savefig`(*args, **kwargs) | 封装 figure.savefig 默认为紧边界框。
W
init  
wizardforcel 已提交
175

Y
Yet-sun 已提交
176
`set_axis_labels`([xlabel, ylabel]) |在双变量轴上设置轴标签。