# seaborn.boxplot
> 譯者:[FindNorthStar](https://github.com/FindNorthStar)
```py
seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)
```
seaborn.boxplot 接口的作用是繪制箱形圖以展現與類別相關的數據分布狀況。
箱形圖(或盒須圖)以一種利于變量之間比較或不同分類變量層次之間比較的方式來展示定量數據的分布。圖中矩形框顯示數據集的上下四分位數,而矩形框中延伸出的線段(觸須)則用于顯示其余數據的分布位置,剩下超過上下四分位間距的數據點則被視為“異常值”。
輸入數據可以通過多種格式傳入,包括:
* 格式為列表,numpy 數組或 pandas Series 對象的數據向量可以直接傳遞給`x`,`y`和`hue`參數。
* 對于長格式的 DataFrame,`x`,`y`,和`hue`參數會決定如何繪制數據。
* 對于寬格式的 DataFrame,每一列數值列都會被繪制。
* 一個數組或向量的列表。
在大多數情況下,可以使用 numpy 或 Python 對象,但更推薦使用 pandas 對象,因為與數據關聯的列名/行名可以用于標注橫軸/縱軸的名稱。此外,您可以使用分類類型對變量進行分組以控制繪圖元素的順序。
此函數始終將其中一個變量視為分類,并在相關軸上的序數位置(0,1,... n)處繪制數據,即使數據屬于數值類型或日期類型也是如此。
更多信息請參閱 [教程](http://seaborn.pydata.org/tutorial/categorical.html#categorical-tutorial)。
參數:`x, y, hue`:`數據`或向量數據中的變量名稱,可選
> 用于繪制長格式數據的輸入。查看樣例以進一步理解。
`data`:DataFrame,數組,數組列表,可選
> 用于繪圖的數據集。如果`x`和`y`都缺失,那么數據將被視為寬格式。否則數據被視為長格式。
`order, hue_order`:字符串列表,可選
> 控制分類變量(對應的條形圖)的繪制順序,若缺失則從數據中推斷分類變量的順序。
`orient`:“v” | “h”,可選
> 控制繪圖的方向(垂直或水平)。這通常是從輸入變量的 dtype 推斷出來的,但是當“分類”變量為數值型或繪制寬格式數據時可用于指定繪圖的方向。
`color`:matplotlib 顏色,可選
> 所有元素的顏色,或漸變調色板的種子顏色。
`palette`:調色板名稱,列表或字典,可選
> 用于`hue`變量的不同級別的顏色。可以從 [`color_palette()`](seaborn.color_palette.html#seaborn.color_palette "seaborn.color_palette") 得到一些解釋,或者將色調級別映射到 matplotlib 顏色的字典。
`saturation`:float,可選
> 控制用于繪制顏色的原始飽和度的比例。通常大幅填充在輕微不飽和的顏色下看起來更好,如果您希望繪圖顏色與輸入顏色規格完美匹配可將其設置為`1`。
`width`:float,可選
> 不使用色調嵌套時完整元素的寬度,或主要分組變量一個級別的所有元素的寬度。
`dodge`:bool,可選
> 使用色調嵌套時,元素是否應沿分類軸移動。
`fliersize`:float,可選
> 用于表示異常值觀察的標記的大小。
`linewidth`:float,可選
> 構圖元素的灰線寬度。
`whis`:float,可選
> 控制在超過高低四分位數時 IQR 的比例,因此需要延長繪制的觸須線段。超出此范圍的點將被識別為異常值。
`notch`:boolean,可選
> 是否使矩形框“凹陷”以指示中位數的置信區間。還有其他幾個參數可以控制凹槽的繪制方式;參見 `plt.boxplot` 以查看關于此問題的更多幫助信息。
`ax`:matplotlib 軸,可選
> 繪圖時使用的 Axes 軸對象,否則使用當前 Axes 軸對象。
`kwargs`:鍵,值映射
> 其他在繪圖時傳給 `plt.boxplot` 的參數。
返回值:`ax`:matplotlib 軸
> 返回 Axes 對軸象,并在其上繪制繪圖。
亦可參見
boxplot 和核密度估計的結合。當一個變量是分類變量的散點圖。可以與其他圖表結合使用以展示各自的觀測結果。分類散點圖的特點是其中數據點互不重疊。可以與其他圖表結合使用以展示各自的觀測結果。
示例
繪制一個單獨的橫向箱型圖:
```py
>>> import seaborn as sns
>>> sns.set(style="whitegrid")
>>> tips = sns.load_dataset("tips")
>>> ax = sns.boxplot(x=tips["total_bill"])
```

根據分類變量分組繪制一個縱向的箱型圖:
```py
>>> ax = sns.boxplot(x="day", y="total_bill", data=tips)
```

根據 2 個分類變量嵌套分組繪制一個箱型圖:
```py
>>> ax = sns.boxplot(x="day", y="total_bill", hue="smoker",
... data=tips, palette="Set3")
```

當一些數據為空時根據嵌套分組繪制一個箱型圖:
```py
>>> ax = sns.boxplot(x="day", y="total_bill", hue="time",
... data=tips, linewidth=2.5)
```

通過顯式傳入參數指定順序控制箱型圖的顯示順序:
```py
>>> ax = sns.boxplot(x="time", y="tip", data=tips,
... order=["Dinner", "Lunch"])
```

針對 DataFrame 里每一個數值型變量繪制箱型圖:
```py
>>> iris = sns.load_dataset("iris")
>>> ax = sns.boxplot(data=iris, orient="h", palette="Set2")
```

使用 `hue` 參數無需改變箱型圖的位置或寬度:
```py
>>> tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
>>> ax = sns.boxplot(x="day", y="total_bill", hue="weekend",
... data=tips, dodge=False)
```

使用 [`swarmplot()`](seaborn.swarmplot.html#seaborn.swarmplot "seaborn.swarmplot") 展示箱型圖頂部的數據點:
```py
>>> ax = sns.boxplot(x="day", y="total_bill", data=tips)
>>> ax = sns.swarmplot(x="day", y="total_bill", data=tips, color=".25")
```

把 [`catplot()`](seaborn.catplot.html#seaborn.catplot "seaborn.catplot") 與 [`pointplot()`](seaborn.pointplot.html#seaborn.pointplot "seaborn.pointplot") 以及 [`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="sex", y="total_bill",
... hue="smoker", col="time",
... data=tips, kind="box",
... height=4, aspect=.7);
```

- seaborn 0.9 中文文檔
- Seaborn 簡介
- 安裝和入門
- 可視化統計關系
- 可視化分類數據
- 可視化數據集的分布
- 線性關系可視化
- 構建結構化多圖網格
- 控制圖像的美學樣式
- 選擇調色板
- seaborn.relplot
- seaborn.scatterplot
- seaborn.lineplot
- seaborn.catplot
- seaborn.stripplot
- seaborn.swarmplot
- seaborn.boxplot
- seaborn.violinplot
- seaborn.boxenplot
- seaborn.pointplot
- seaborn.barplot
- seaborn.countplot
- seaborn.jointplot
- seaborn.pairplot
- seaborn.distplot
- seaborn.kdeplot
- seaborn.rugplot
- seaborn.lmplot
- seaborn.regplot
- seaborn.residplot
- seaborn.heatmap
- seaborn.clustermap
- seaborn.FacetGrid
- seaborn.FacetGrid.map
- seaborn.FacetGrid.map_dataframe
- seaborn.PairGrid
- seaborn.PairGrid.map
- seaborn.PairGrid.map_diag
- seaborn.PairGrid.map_offdiag
- seaborn.PairGrid.map_lower
- seaborn.PairGrid.map_upper
- seaborn.JointGrid
- seaborn.JointGrid.plot
- seaborn.JointGrid.plot_joint
- seaborn.JointGrid.plot_marginals
- seaborn.set
- seaborn.axes_style
- seaborn.set_style
- seaborn.plotting_context
- seaborn.set_context
- seaborn.set_color_codes
- seaborn.reset_defaults
- seaborn.reset_orig
- seaborn.set_palette
- seaborn.color_palette
- seaborn.husl_palette
- seaborn.hls_palette
- seaborn.cubehelix_palette
- seaborn.dark_palette
- seaborn.light_palette
- seaborn.diverging_palette
- seaborn.blend_palette
- seaborn.xkcd_palette
- seaborn.crayon_palette
- seaborn.mpl_palette
- seaborn.choose_colorbrewer_palette
- seaborn.choose_cubehelix_palette
- seaborn.choose_light_palette
- seaborn.choose_dark_palette
- seaborn.choose_diverging_palette
- seaborn.load_dataset
- seaborn.despine
- seaborn.desaturate
- seaborn.saturate
- seaborn.set_hls_values