19.md 9.1 KB
Newer Older
W
init  
wizardforcel 已提交
1 2
# seaborn.pointplot

W
credit  
wizardforcel 已提交
3 4
> 译者:[FindNorthStar](https://github.com/FindNorthStar)

W
init  
wizardforcel 已提交
5 6 7 8
```py
seaborn.pointplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, markers='o', linestyles='-', dodge=False, join=True, scale=1, orient=None, color=None, palette=None, errwidth=None, capsize=None, ax=None, **kwargs)
```

F
findns 已提交
9
通过绘制散点连线显示数据点的估计值和置信区间。
W
init  
wizardforcel 已提交
10

F
findns 已提交
11
点图代表散点图位置的数值变量的中心趋势估计,并使用误差线提供关于该估计的不确定性的一些指示。
W
init  
wizardforcel 已提交
12

F
findns 已提交
13
点图比条形图在聚焦一个或多个分类变量的不同级别之间的比较时更为有用。点图尤其善于表现交互作用:一个分类变量的层次之间的关系如何在第二个分类变量的层次之间变化。连接来自相同 `色调` 等级的每个点的线允许交互作用通过斜率的差异进行判断,这使得更容易对几组数据点或数据线的高度进行比较。
W
init  
wizardforcel 已提交
14

F
findns 已提交
15
重要的一点是点图仅显示平均值(或其他估计值),但在许多情况下,显示分类变量的每个级别的值的分布可能会带有更多信息。在这种情况下,其他绘图方法,例如箱型图或小提琴图可能更合适。
W
init  
wizardforcel 已提交
16

F
findns 已提交
17
输入数据可以通过多种格式传入,包括:
W
init  
wizardforcel 已提交
18

W
wizardforcel 已提交
19 20 21
*   格式为列表,numpy 数组或 pandas Series 对象的数据向量可以直接传递给`x``y``hue`参数。
*   对于长格式的 DataFrame,`x``y`,和`hue`参数会决定如何绘制数据。
*   对于宽格式的 DataFrame,每一列数值列都会被绘制。
F
findns 已提交
22
*   一个数组或向量的列表。
W
init  
wizardforcel 已提交
23

W
wizardforcel 已提交
24
在大多数情况下,可以使用 numpy 或 Python 对象,但更推荐使用 pandas 对象,因为与数据关联的列名/行名可以用于标注横轴/纵轴的名称。此外,您可以使用分类类型对变量进行分组以控制绘图元素的顺序。
W
init  
wizardforcel 已提交
25

F
findns 已提交
26
此函数始终将其中一个变量视为分类,并在相关轴上的序数位置(0,1,... n)处绘制数据,即使数据属于数值类型或日期类型也是如此。
W
init  
wizardforcel 已提交
27

F
findns 已提交
28
更多信息请参阅 [教程](../tutorial/categorical.html#categorical-tutorial)
W
init  
wizardforcel 已提交
29

F
findns 已提交
30
参数:`x, y, hue``data`或向量数据中的变量名称,可选
W
init  
wizardforcel 已提交
31

F
findns 已提交
32
> 用于绘制长格式数据的输入。查看样例以进一步理解。
W
init  
wizardforcel 已提交
33

F
findns 已提交
34
`data`:DataFrame,数组,数组列表,可选
W
init  
wizardforcel 已提交
35

F
findns 已提交
36
> 于绘图的数据集。如果`x`和`y`都缺失,那么数据将被视为宽格式。否则数据被视为长格式。
W
init  
wizardforcel 已提交
37

F
findns 已提交
38
`order, hue_order`:字符串列表,可选
W
init  
wizardforcel 已提交
39

W
wizardforcel 已提交
40
> 控制绘图的方向(垂直或水平)。这通常是从输入变量的 dtype 推断出来的,但是当“分类”变量为数值型或绘制宽格式数据时可用于指定绘图的方向。
W
init  
wizardforcel 已提交
41

F
findns 已提交
42
`estimator`:调用函数实现向量 -&gt; 标量的映射,可选
W
init  
wizardforcel 已提交
43

F
findns 已提交
44
> 在每个分箱内进行估计的统计函数。
W
init  
wizardforcel 已提交
45

F
findns 已提交
46
`ci`:float 或 “sd” 或 None,可选
W
init  
wizardforcel 已提交
47

F
findns 已提交
48
> 在估计值附近绘制置信区间的尺寸大小。如果是“sd”,则跳过引导阶段并绘制观察数据点的标准差。如果为 `None`,则不会执行引导过程,并且不会绘制误差块。
W
init  
wizardforcel 已提交
49

F
findns 已提交
50
`n_boot`:int,可选
W
init  
wizardforcel 已提交
51

F
findns 已提交
52
> 计算置信区间时使用的引导迭代次数。
W
init  
wizardforcel 已提交
53

W
wizardforcel 已提交
54
`units``data` 或 vector data 中变量的名称,可选
W
init  
wizardforcel 已提交
55

F
findns 已提交
56
> 采样单元的标识符,用于执行多级引导过程(计算置信区间等)并能够处理重复测量的设定。
W
init  
wizardforcel 已提交
57

F
findns 已提交
58
`markers`:字符串或字符串列表,可选
W
init  
wizardforcel 已提交
59

F
findns 已提交
60
> 用于每个`hue`色调的级别的标记。
W
init  
wizardforcel 已提交
61

F
findns 已提交
62
`linestyles`:字符串或字符串列表,可选
W
init  
wizardforcel 已提交
63

F
findns 已提交
64
> 用于每个`hue`色调的级别的线条风格。
W
init  
wizardforcel 已提交
65

W
wizardforcel 已提交
66
`dodge`:bool 或 float,可选
W
init  
wizardforcel 已提交
67

F
findns 已提交
68
> 用于沿着分类轴分离`hue`变量的每个级别数据点的数量。
W
init  
wizardforcel 已提交
69

F
findns 已提交
70
`join`:bool,可选
W
init  
wizardforcel 已提交
71

F
findns 已提交
72
> 如果为`True`,则在`hue`级别相同的点估计值之间绘制线条。
W
init  
wizardforcel 已提交
73

F
findns 已提交
74
`scale`:float,可选
W
init  
wizardforcel 已提交
75

F
findns 已提交
76
> 绘图元素的比例因子。
W
init  
wizardforcel 已提交
77

F
findns 已提交
78
`orient`:“v” &#124; “h”,可选
W
init  
wizardforcel 已提交
79

W
wizardforcel 已提交
80
> 控制绘图的方向(垂直或水平)。这通常是从输入变量的 dtype 推断出来的,但是当“分类”变量为数值型或绘制宽格式数据时可用于指定绘图的方向。
W
init  
wizardforcel 已提交
81

W
wizardforcel 已提交
82
`color`:matplotlib 颜色,可选
W
init  
wizardforcel 已提交
83

F
findns 已提交
84
> 所有元素的颜色,或渐变调色板的种子颜色。
W
init  
wizardforcel 已提交
85

F
findns 已提交
86
`palette`:调色板名称,列表或字典,可选
W
init  
wizardforcel 已提交
87

W
wizardforcel 已提交
88
> 用于`hue`变量的不同级别的颜色。可以从 [`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette") 得到一些解释,或者将色调级别映射到 matplotlib 颜色的字典。
W
init  
wizardforcel 已提交
89

F
findns 已提交
90
`errwidth`:float,可选
W
init  
wizardforcel 已提交
91

F
findns 已提交
92
> 误差线(和上下限指示线)的厚度。
W
init  
wizardforcel 已提交
93

F
findns 已提交
94
`capsize`:float,可选
W
init  
wizardforcel 已提交
95

F
findns 已提交
96
> 误差线“上下限指示线”的宽度。
W
init  
wizardforcel 已提交
97

W
wizardforcel 已提交
98
`ax`:matplotlib 轴,可选
W
init  
wizardforcel 已提交
99

W
wizardforcel 已提交
100
> 绘图时使用的 Axes 轴对象,否则使用当前 Axes 轴对象。
W
init  
wizardforcel 已提交
101

W
wizardforcel 已提交
102

W
wizardforcel 已提交
103
返回值:`ax`:matplotlib 轴
W
init  
wizardforcel 已提交
104

W
wizardforcel 已提交
105
> 返回 Axes 对轴象,并在其上绘制绘图。
W
init  
wizardforcel 已提交
106

W
wizardforcel 已提交
107

W
init  
wizardforcel 已提交
108

F
findns 已提交
109
亦可参见
W
init  
wizardforcel 已提交
110

F
findns 已提交
111
使用线条显示数据点的估计值和置信区间。将分类类别的绘图与该类结合使用:<cite>FacetGrid</cite>
W
init  
wizardforcel 已提交
112

F
findns 已提交
113
示例
W
init  
wizardforcel 已提交
114

F
findns 已提交
115
绘制一组按分类变量分组的纵向点图:
W
init  
wizardforcel 已提交
116 117 118 119 120 121 122 123 124 125 126

```py
>>> import seaborn as sns
>>> sns.set(style="darkgrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.pointplot(x="time", y="total_bill", data=tips)

```

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

F
findns 已提交
127
通过两个嵌套分组的变量绘制一组纵向的点图:
W
init  
wizardforcel 已提交
128 129 130 131 132 133 134 135 136

```py
>>> ax = sns.pointplot(x="time", y="total_bill", hue="smoker",
...                    data=tips)

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-2.png](img/864eda3b3c2fcc6b0bdb53c84c3dafcf.jpg)

F
findns 已提交
137
沿着分类轴分离不同色调级别的点:
W
init  
wizardforcel 已提交
138 139 140 141 142 143 144 145 146

```py
>>> ax = sns.pointplot(x="time", y="total_bill", hue="smoker",
...                    data=tips, dodge=True)

```

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

F
findns 已提交
147
根据色调级别使用不同的标记和线条样式:
W
init  
wizardforcel 已提交
148 149 150 151 152 153 154 155 156 157 158

```py
>>> ax = sns.pointplot(x="time", y="total_bill", hue="smoker",
...                    data=tips,
...                    markers=["o", "x"],
...                    linestyles=["-", "--"])

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-4.png](img/a97ae4389fc8bc946eb62e06c173b3e3.jpg)

F
findns 已提交
159
绘制一组横向的点图:
W
init  
wizardforcel 已提交
160 161 162 163 164 165 166 167

```py
>>> ax = sns.pointplot(x="tip", y="day", data=tips)

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-5.png](img/4217fbfe6aaba42c4d18a69c5b8c9fc4.jpg)

F
findns 已提交
168
不要绘制每个点的连接线:
W
init  
wizardforcel 已提交
169 170 171 172 173 174 175 176

```py
>>> ax = sns.pointplot(x="tip", y="day", data=tips, join=False)

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-6.png](img/96a33f32e85dcced62c9fc4ff063fe3d.jpg)

F
findns 已提交
177
对单层图使用不同的颜色:
W
init  
wizardforcel 已提交
178 179 180 181 182 183 184 185 186

```py
>>> ax = sns.pointplot("time", y="total_bill", data=tips,
...                    color="#bb3f3f")

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-7.png](img/1e3348f06e5cd7876d5bc530b04d3d93.jpg)

F
findns 已提交
187
为数据点使用不同的调色板:
W
init  
wizardforcel 已提交
188 189 190 191 192 193 194 195 196

```py
>>> ax = sns.pointplot(x="time", y="total_bill", hue="smoker",
...                    data=tips, palette="Set2")

```

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

F
findns 已提交
197
通过显式传入参数指定顺序控制点的显示顺序:
W
init  
wizardforcel 已提交
198 199 200 201 202 203 204 205 206

```py
>>> ax = sns.pointplot(x="time", y="tip", data=tips,
...                    order=["Dinner", "Lunch"])

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-9.png](img/4c08e24283b6829b3d91e3c23de56923.jpg)

F
findns 已提交
207
用中位数作为集中趋势的估计:
W
init  
wizardforcel 已提交
208 209 210 211 212 213 214 215 216

```py
>>> from numpy import median
>>> ax = sns.pointplot(x="day", y="tip", data=tips, estimator=median)

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-10.png](img/0ec9398faa407996527db66db46c71f2.jpg)

F
findns 已提交
217
用误差线显示均值的标准误差:
W
init  
wizardforcel 已提交
218 219 220 221 222 223 224 225

```py
>>> ax = sns.pointplot(x="day", y="tip", data=tips, ci=68)

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-11.png](img/f9f6dd93a512624527b38dcc26d97e37.jpg)

F
findns 已提交
226
显示观测值的标准偏差而不是置信区间:
W
init  
wizardforcel 已提交
227 228 229 230 231 232 233 234

```py
>>> ax = sns.pointplot(x="day", y="tip", data=tips, ci="sd")

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-12.png](img/f41526e37f8f11614ea339da0e242c51.jpg)

F
findns 已提交
235
将“上下限指示线”增加到误差线的顶部和底部:
W
init  
wizardforcel 已提交
236 237 238 239 240 241 242 243

```py
>>> ax = sns.pointplot(x="day", y="tip", data=tips, capsize=.2)

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-13.png](img/b7df4cf62c681ea39bd145bdb740bc81.jpg)

F
findns 已提交
244
[`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot")[`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot") 以及 [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid")结合使用。这允许您通过额外的分类变量进行分组。使用 [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") 比直接使用 [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") 更为安全,因为它保证了不同切面上变量同步的顺序:
W
init  
wizardforcel 已提交
245 246 247 248 249 250 251 252 253 254 255

```py
>>> g = sns.catplot(x="sex", y="total_bill",
...                 hue="smoker", col="time",
...                 data=tips, kind="point",
...                 dodge=True,
...                 height=4, aspect=.7);

```

![http://seaborn.pydata.org/_images/seaborn-pointplot-14.png](img/2d47b5f96f08a539147e7f0a71d0aa33.jpg)