> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/python/chapter-2courses/21python-for-data-science-and-machine-learning-bootcamp/217python-for-data-visualization-seaborn/2174matrix-plots.md).

# 2.1.7.4.Matrix Plots

## 0. Matrix Plots的繪圖種類

* sns.heatmap
* sns.clustermap

## 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)

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

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

## 2.heatmap (熱力圖)

* 視覺化矩陣形式中各行列的數值關係
  * [自相關](https://blog.csdn.net/lll1528238733/article/details/75114360)矩陣的視覺化

    ```
    tc = tips.corr()
    sns.heatmap(tc)
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-20%20上午7.45.59.png)
  * 視覺化歷史各年度中的各月份的航班密度

    ```
    # 重新排列資料
    fp = flights.pivot_table(index='month', columns='year', values='passengers')
    ```

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

    ```
    sns.heatmap(fp)
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-20%20上午7.52.33.png)
  * 改變熱力圖的色調及線條

    ```
    sns.heatmap(fp, cmap='coolwarm', linecolor='blue', linewidth=1)
    ```

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

## 3.clustermap (階層式熱力圖)

```
sns.clustermap(fp)
```

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

* 正規化

```
sns.clustermap(fp, cmap='coolwarm', standard_scale=1)
```

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