# 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/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-19%20%E4%B8%8A%E5%8D%889.13.50.png)

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

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-20%20%E4%B8%8A%E5%8D%887.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/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-20%20%E4%B8%8A%E5%8D%887.45.59.png)
  * 視覺化歷史各年度中的各月份的航班密度

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

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-20%20%E4%B8%8A%E5%8D%887.50.02.png)

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

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-20%20%E4%B8%8A%E5%8D%887.52.33.png)
  * 改變熱力圖的色調及線條

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

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-20%20%E4%B8%8A%E5%8D%887.53.29.png)

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

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

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-20%20%E4%B8%8A%E5%8D%888.00.40.png)

* 正規化

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

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-05-20%20%E4%B8%8A%E5%8D%888.01.36.png)
