未验证 提交 e4818a1d 编写于 作者: 飞龙 提交者: GitHub

Merge pull request #18 from Stuming/master

Update 21.md
# seaborn.countplot
> 译者:[Stuming](https://github.com/Stuming)
```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)
```
Show the counts of observations in each categorical bin using bars.
seaborn.countplot使用条形图显示每个类别中观测值的数量。
这个函数可以被认为是针对类别变量的直方图。基本的API和选项与[`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot")完全相同,因此可以对比学习。
A count plot can be thought of as a histogram across a categorical, instead of quantitative, variable. The basic API and options are identical to those for [`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot"), so you can compare counts across nested variables.
可以通过多种格式传入数据,包括:
Input data can be passed in a variety of formats, including:
* 通过列表、numpy数组、或者pandas Series对象表示的向量数据,数据直接传给`x`, `y`, 和/或`hue`参数。
* 长格式的DataFrame,此时会通过`x`, `y`以及`hue`变量决定如何绘制数据。
* 宽格式的DataFrame,此时每个数值型的column都会被绘制。
* 数组或者列表形式的向量
* Vectors of data represented as lists, numpy arrays, or pandas Series objects passed directly to the `x`, `y`, and/or `hue` parameters.
* A “long-form” DataFrame, in which case the `x`, `y`, and `hue` variables will determine how the data are plotted.
* A “wide-form” DataFrame, such that each numeric column will be plotted.
* An array or list of vectors.
在绝大多数情况下,数据格式都可以使用numpy或者Python对象,但是推荐使用pandas对象,因为pandas对象中相关的名称会被用于注释坐标轴。此外,可以通过设置分组变量为使用Categorical类型来控制绘制元素的顺序。
In most cases, it is possible to use numpy or Python objects, but pandas objects are preferable because the associated names will be used to annotate the axes. Additionally, you can use Categorical types for the grouping variables to control the order of plot elements.
这个函数总会将变量作为类别变量进行处理,按顺序(0, 1, ... n)在相应坐标轴绘制数据,即使数据为数值或者日期类型。
This function always treats one of the variables as categorical and draws data at ordinal positions (0, 1, … n) on the relevant axis, even when the data has a numeric or date type.
更多信息参考[tutorial](../tutorial/categorical.html#categorical-tutorial).
See the [tutorial](../tutorial/categorical.html#categorical-tutorial) for more information.
参数:`x, y, hue`: `data`或者向量数据中的变量名,可选
参数:`x, y, hue`:names of variables in `data` or vector data, optional
> 用于绘制长格式数据的输入。查看解释示例
> Inputs for plotting long-form data. See examples for interpretation.
`data`:DataFrame, 数组,或者包含数组的列表,可选
`data`:DataFrame, array, or list of arrays, optional
> 用于绘制的数据集。如果`x`和`y`不存在,那么会将数据按宽格式进行处理,否则应当为长格式。
> Dataset for plotting. If `x` and `y` are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
`order, hue_order`:包含字符串的列表,可选
`order, hue_order`:lists of strings, optional
> 类别层级绘制的顺序,否则层级会从数据对象中推测。
> Order to plot the categorical levels in, otherwise the levels are inferred from the data objects.
`orient`: “v” | “h”, 可选
`orient`:“v” | “h”, optional
> 绘制的朝向(竖直或者水平)。通过从输入变量的dtype进行推断。当类别变量是数值型或者绘制宽格式数据时,可以进行指定。
> Orientation of the plot (vertical or horizontal). This is usually inferred from the dtype of the input variables, but can be used to specify when the “categorical” variable is a numeric or when plotting wide-form data.
`color`: matplotlib颜色,可选
`color`:matplotlib color, optional
> 所有元素的颜色,或者渐变调色盘的种子。
> Color for all of the elements, or seed for a gradient palette.
`palette`: 调色盘名称,列表或字典,可选
`palette`:palette name, list, or dict, optional
> 用于`hue`变量的不同级别的颜色。应当是[`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette")可以解释的东西,或者将色调级别映射到matplotlib颜色的字典。
> Colors to use for the different levels of the `hue` variable. Should be something that can be interpreted by [`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette"), or a dictionary mapping hue levels to matplotlib colors.
`saturation`: float, 可选
`saturation`:float, optional
> 在原有饱和度的比例下绘制颜色。大片的图块通常在略微不饱和的颜色下看起来更好,而如果想要绘制的颜色与输入颜色规格完全匹配,应当设置此值为`1`。
> Proportion of the original saturation to draw colors at. Large patches often look better with slightly desaturated colors, but set this to `1` if you want the plot colors to perfectly match the input color spec.
`dodge`: bool, 可选
`dodge`:bool, optional
> 当使用色调嵌套时,决定是否沿着类别轴对元素进行移位。
> When hue nesting is used, whether elements should be shifted along the categorical axis.
`ax`: matplotlib轴,可选
`ax`:matplotlib Axes, optional
> 绘制图像的轴对象,不指定时使用当前轴。
> Axes object to draw the plot onto, otherwise uses the current Axes.
`kwargs`: 键值映射
`kwargs`:key, value mappings
> 其他关键字参数会被传递给`plt.bar`.
> Other keyword arguments are passed to `plt.bar`.
返回值:`ax`: matplotlib轴
返回值:`ax`:matplotlib Axes
> 返回带有绘图的轴对象。
> Returns the Axes object with the plot drawn onto it.
另请参阅
See also
[`barplot()`](seaborn.barplot.html#seaborn.barplot "seaborn.barplot"): 使用条形图显示点估计和置信区间。
Show point estimates and confidence intervals using bars.Combine a categorical plot with a class:<cite>FacetGrid</cite>.
[`factorplot()`](seaborn.factorplot.html#seaborn.factorplot "seaborn.factorplot"): 结合类别图与`FacetGrid`类。
Examples
示例
Show value counts for a single categorical variable:
显示单个类别变量的计数值:
```py
>>> import seaborn as sns
......@@ -86,7 +90,7 @@ Show value counts for a single categorical variable:
![http://seaborn.pydata.org/_images/seaborn-countplot-1.png](img/840a3b4206930d48fae0f0d4549b87a0.jpg)
Show value counts for two categorical variables:
显示两个类别变量的计数值:
```py
>>> ax = sns.countplot(x="class", hue="who", data=titanic)
......@@ -95,7 +99,7 @@ Show value counts for two categorical variables:
![http://seaborn.pydata.org/_images/seaborn-countplot-2.png](img/7fde400b120a0b6cd6aca0cb2de690db.jpg)
Plot the bars horizontally:
水平绘制条形图:
```py
>>> ax = sns.countplot(y="class", hue="who", data=titanic)
......@@ -104,7 +108,7 @@ Plot the bars horizontally:
![http://seaborn.pydata.org/_images/seaborn-countplot-3.png](img/512afaa1b09caf55faf909a34995137a.jpg)
Use a different color palette:
使用不同的颜色色盘:
```py
>>> ax = sns.countplot(x="who", data=titanic, palette="Set3")
......@@ -113,7 +117,7 @@ Use a different color palette:
![http://seaborn.pydata.org/_images/seaborn-countplot-4.png](img/d3ac1e2e1f590489ef76341664562f87.jpg)
Use `plt.bar` keyword arguments for a different look:
使用`plt.bar`的关键字参数获得不同的显示效果:
```py
>>> ax = sns.countplot(x="who", data=titanic,
......@@ -125,7 +129,7 @@ Use `plt.bar` keyword arguments for a different look:
![http://seaborn.pydata.org/_images/seaborn-countplot-5.png](img/d23d0851c88c12754e0c1e08aac20d01.jpg)
Use [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") to combine a [`countplot()`](#seaborn.countplot "seaborn.countplot") and a [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid"). This allows grouping within additional categorical variables. Using [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") is safer than using [`FacetGrid`](seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") directly, as it ensures synchronization of variable order across facets:
使用[`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")更加安全,因为这样做可以确保跨分面的变量顺序同步:
```py
>>> g = sns.catplot(x="class", hue="who", col="survived",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册