# 2.1.7.2.Distribution Plots

## 0. Distribution Plots的繪圖種類

* sns.distplot (kde, bin)
* sns.jointplot
* sns.pairplot
* sns.rugplot

## 1. 使用library

```
import seaborn as sns
```

* 將圖表直接嵌入到Notebook之中

```
%matplotlib inline
```

* 讀入資料

```
tips = sns.load_dataset('tips')
tips.head()
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.13.50.png)

## [2.Distribution plot](https://seaborn.pydata.org/tutorial/distributions.html)

* [等義於使用pandas內建的plot.hist或是plot(kind = 'hist')](https://jenhsuan.gitbooks.io/python/content/2323.html)

  ```
    tips['total_bill'].hist(bins = 30)
    tips['total_bill'].plot(kind='hist', bins = 30)
    tips['total_bill'].plot.hist(bins = 30)
  ```
* kde (Kernel distribution estimation)預設為true
  * 也可以設為False

    ```
    sns.distplot(tips['total_bill'], kde=False)
    sns.distplot(tips['total_bill'])
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.08.28.png)
* bin

```
sns.distplot(tips['total_bill'], kde=False, bins=30)
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.09.22.png)

## [3. Joint plot](https://seaborn.pydata.org/generated/seaborn.jointplot.html)

* 比較資料來源中的兩個屬性之間的相關性或非相關性
  * 繪圖前可以先設定背景的方格線

    ```
    sns.set_style('whitegrid')
    ```
  * 例如比較資料中**帳單金額**與**小費**間的關聯性

    ```
    sns.jointplot(x='total_bill',y='tip',data=tips)
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.16.04.png)
  * 用顏色深淺來代表相關性 (顏色越深, 相關性越高)

    ```
    sns.jointplot(x='total_bill',y='tip',data=tips,kind='hex')
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.18.35.png)
  * 線性回歸圖

    ```
    sns.jointplot(x='total_bill',y='tip',data=tips,kind='reg')
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.20.14.png)
  * kde (Kernel distribution estimation)

    ```
    sns.jointplot(x='total_bill',y='tip',data=tips,kind='kde')
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.21.39.png)

## 4.Pair plot

* 可以將資料來源中的數值欄位一一比較, 畫出關聯圖
  * 輸入資料來源

    ```
    sns.pairplot(tips)
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.26.26.png)
  * 加入類別欄位的比較

    * 例如比較性別在資料上的差異

    ```
    sns.pairplot(tips,hue='sex')
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.23.40.png)
  * 調整色系

    ```
    sns.pairplot(tips,hue='sex',palette='coolwarm')
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.27.25.png)

## [5.Rug plot](https://seaborn.pydata.org/generated/seaborn.rugplot.html)

* 以stick的方式表現資料

```
sns.rugplot(tips['total_bill'])
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-19%20上午9.30.46.png)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/python/chapter-2courses/21python-for-data-science-and-machine-learning-bootcamp/217python-for-data-visualization-seaborn/2172distribution-plot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
