21.md 5.3 KB
Newer Older
W
init  
wizardforcel 已提交
1 2
# seaborn.countplot

S
Stuming 已提交
3 4
> 译者:[Stuming](https://github.com/Stuming)

W
init  
wizardforcel 已提交
5 6 7 8
```py
seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)
```

W
wizardforcel 已提交
9
seaborn.countplot 使用条形图显示每个类别中观测值的数量。
W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11
这个函数可以被认为是针对类别变量的直方图。基本的 API 和选项与[`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot")完全相同,因此可以对比学习。
W
init  
wizardforcel 已提交
12

S
Stuming 已提交
13
可以通过多种格式传入数据,包括:
W
init  
wizardforcel 已提交
14

W
wizardforcel 已提交
15 16 17
*   通过列表、numpy 数组、或者 pandas Series 对象表示的向量数据,数据直接传给`x`, `y`, 和/或`hue`参数。
*   长格式的 DataFrame,此时会通过`x`, `y`以及`hue`变量决定如何绘制数据。
*   宽格式的 DataFrame,此时每个数值型的 column 都会被绘制。
S
Stuming 已提交
18
*   数组或者列表形式的向量
W
init  
wizardforcel 已提交
19

W
wizardforcel 已提交
20
在绝大多数情况下,数据格式都可以使用 numpy 或者 Python 对象,但是推荐使用 pandas 对象,因为 pandas 对象中相关的名称会被用于注释坐标轴。此外,可以通过设置分组变量为使用 Categorical 类型来控制绘制元素的顺序。
W
init  
wizardforcel 已提交
21

S
Stuming 已提交
22
这个函数总会将变量作为类别变量进行处理,按顺序(0, 1, ... n)在相应坐标轴绘制数据,即使数据为数值或者日期类型。
W
init  
wizardforcel 已提交
23

S
Stuming 已提交
24
更多信息参考[tutorial](../tutorial/categorical.html#categorical-tutorial). 
W
init  
wizardforcel 已提交
25

S
Stuming 已提交
26
参数:`x, y, hue`: `data`或者向量数据中的变量名,可选
W
init  
wizardforcel 已提交
27

S
Stuming 已提交
28
> 用于绘制长格式数据的输入。查看解释示例
W
init  
wizardforcel 已提交
29

S
Stuming 已提交
30
`data`:DataFrame, 数组,或者包含数组的列表,可选
W
init  
wizardforcel 已提交
31

S
Stuming 已提交
32
> 用于绘制的数据集。如果`x`和`y`不存在,那么会将数据按宽格式进行处理,否则应当为长格式。 
W
init  
wizardforcel 已提交
33

S
Stuming 已提交
34
`order, hue_order`:包含字符串的列表,可选
W
init  
wizardforcel 已提交
35

S
Stuming 已提交
36
> 类别层级绘制的顺序,否则层级会从数据对象中推测。
W
init  
wizardforcel 已提交
37

S
Stuming 已提交
38
`orient`: “v” | “h”, 可选
W
init  
wizardforcel 已提交
39

W
wizardforcel 已提交
40
> 绘制的朝向(竖直或者水平)。通过从输入变量的 dtype 进行推断。当类别变量是数值型或者绘制宽格式数据时,可以进行指定。
W
init  
wizardforcel 已提交
41

W
wizardforcel 已提交
42
`color`: matplotlib 颜色,可选
W
init  
wizardforcel 已提交
43

S
Stuming 已提交
44
> 所有元素的颜色,或者渐变调色盘的种子。
W
init  
wizardforcel 已提交
45

S
Stuming 已提交
46
`palette`: 调色盘名称,列表或字典,可选
W
init  
wizardforcel 已提交
47

W
wizardforcel 已提交
48
> 用于`hue`变量的不同级别的颜色。应当是[`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")可以解释的东西,或者将色调级别映射到 matplotlib 颜色的字典。
W
init  
wizardforcel 已提交
49

S
Stuming 已提交
50
`saturation`: float, 可选
W
init  
wizardforcel 已提交
51

S
Stuming 已提交
52
> 在原有饱和度的比例下绘制颜色。大片的图块通常在略微不饱和的颜色下看起来更好,而如果想要绘制的颜色与输入颜色规格完全匹配,应当设置此值为`1`。
W
init  
wizardforcel 已提交
53

S
Stuming 已提交
54
`dodge`: bool, 可选
W
init  
wizardforcel 已提交
55

S
Stuming 已提交
56
> 当使用色调嵌套时,决定是否沿着类别轴对元素进行移位。
W
init  
wizardforcel 已提交
57

W
wizardforcel 已提交
58
`ax`: matplotlib 轴,可选
W
init  
wizardforcel 已提交
59

S
Stuming 已提交
60
> 绘制图像的轴对象,不指定时使用当前轴。
W
init  
wizardforcel 已提交
61

S
Stuming 已提交
62
`kwargs`: 键值映射
W
init  
wizardforcel 已提交
63

S
Stuming 已提交
64
> 其他关键字参数会被传递给`plt.bar`.
W
init  
wizardforcel 已提交
65

W
wizardforcel 已提交
66

W
wizardforcel 已提交
67
返回值:`ax`: matplotlib 轴
W
init  
wizardforcel 已提交
68

S
Stuming 已提交
69
> 返回带有绘图的轴对象。
W
init  
wizardforcel 已提交
70

W
wizardforcel 已提交
71

W
init  
wizardforcel 已提交
72

S
Stuming 已提交
73
另请参阅
W
init  
wizardforcel 已提交
74

S
Stuming 已提交
75
[`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot"): 使用条形图显示点估计和置信区间。
S
Stuming 已提交
76

S
Stuming 已提交
77
[`factorplot()`](seaborn.factorplot.html#seaborn.factorplot "seaborn.factorplot"): 结合类别图与`FacetGrid`类。
W
init  
wizardforcel 已提交
78

S
Stuming 已提交
79
示例
W
init  
wizardforcel 已提交
80

S
Stuming 已提交
81
显示单个类别变量的计数值:
W
init  
wizardforcel 已提交
82 83 84 85 86 87 88 89 90 91 92

```py
>>> import seaborn as sns
>>> sns.set(style="darkgrid")
>>> titanic = sns.load_dataset("titanic")
>>> ax = sns.countplot(x="class", data=titanic)

```

![http://seaborn.pydata.org/_images/seaborn-countplot-1.png](img/840a3b4206930d48fae0f0d4549b87a0.jpg)

S
Stuming 已提交
93
显示两个类别变量的计数值:
W
init  
wizardforcel 已提交
94 95 96 97 98 99 100 101

```py
>>> ax = sns.countplot(x="class", hue="who", data=titanic)

```

![http://seaborn.pydata.org/_images/seaborn-countplot-2.png](img/7fde400b120a0b6cd6aca0cb2de690db.jpg)

S
Stuming 已提交
102
水平绘制条形图:
W
init  
wizardforcel 已提交
103 104 105 106 107 108 109 110

```py
>>> ax = sns.countplot(y="class", hue="who", data=titanic)

```

![http://seaborn.pydata.org/_images/seaborn-countplot-3.png](img/512afaa1b09caf55faf909a34995137a.jpg)

S
Stuming 已提交
111
使用不同的颜色色盘:
W
init  
wizardforcel 已提交
112 113 114 115 116 117 118 119

```py
>>> ax = sns.countplot(x="who", data=titanic, palette="Set3")

```

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

S
Stuming 已提交
120
使用`plt.bar`的关键字参数获得不同的显示效果:
W
init  
wizardforcel 已提交
121 122 123 124 125 126 127 128 129 130 131

```py
>>> ax = sns.countplot(x="who", data=titanic,
...                    facecolor=(0, 0, 0, 0),
...                    linewidth=5,
...                    edgecolor=sns.color_palette("dark", 3))

```

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

S
Stuming 已提交
132
使用[`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot")实现结合[`countplot()`](#seaborn.countplot "seaborn.countplot")以及[`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 已提交
133 134 135 136 137 138 139 140 141

```py
>>> g = sns.catplot(x="class", hue="who", col="survived",
...                 data=titanic, kind="count",
...                 height=4, aspect=.7);

```

![http://seaborn.pydata.org/_images/seaborn-countplot-6.png](img/5d75285f3c08542cd9db2296ef737d54.jpg)