# 2.1.5.Python for Data Visual Visualization - Pandas Built-in Data Visualization

![](/files/-M4M0UfyNEBeGoUKDZlD)

## 1.使用library

```
import numpy as np
import pandas as pd
```

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

```
%matplotlib inline
```

## 2.資料判讀與讀取

* 讀取資料格式 (df1)
  * 由於df1的資料有時間這個索引值, 因此必須指定index為0

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-27%20下午9.38.34.png)
  * 讀取資料

    ```
    df1 = pd.read_csv('df1', index_col = 0)
    df1.head()
    ```

    ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-27%20下午9.47.12.png)
* 讀取資料格式 (df2)
  * 由於此資料沒有索引值, 不需指定index ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-27%20下午9.42.39.png)
  * 讀取資料

    ```
    df2 = pd.read_csv('df2')
    df2.head()
    ```

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

## 3.Setting before plot

* 一般會配合[Matplotlib](https://jenhsuan.gitbooks.io/python/content/test.html)一起使用
  * 設定圖的大小

    ```
    plt.figure(figsize=(10,6))
    ```

## 4.Histogram plot

* 將df1的資料畫成長條圖

```
df1['A'].hist(bins = 30)
```

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

* 也可以用

```
df1['A'].plot(kind='hist', bins = 30)
```

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

* 可以搭配filter使用, 例如

```
df[df[B] == 1][A].plot(kind='hist', bins = 30)
```

* 用hist比較不同性質的資料

```
df = pd.read_csv('loan_data.csv')
df.head()
plt.figure()
df[df['credit.policy']==1]['fico'].plot(kind = 'hist', bins = 30, color = 'blue', alpha = 0.5, label = 'Credit.Policy=1')
df[df['credit.policy']==0]['fico'].plot(kind = 'hist', bins = 30, color = 'red', alpha = 0.5, label = 'Credit.Policy=0')
plt.legend()
plt.xlabel('FICO')
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-06-30%20下午12.45.24.png)

## 5.Area plot

* alpha為透明度參數

```
df2.plot.area(alpha = 0.4)
```

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

## 6.Line plot

* 基本用法

```
df1.plot.line(x=df1.index, y = 'B')
```

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

## 6.Bar plot

* 基本用法

```
df2.plot.bar(alpha = 0.4)
```

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

* stacked

```
df2.plot.bar(alpha = 0.4, stacked = True)
```

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

## 7.Scatter plot

* 基本用法

```
df1.plot.scatter(x='A', y='B')
```

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

* 將原始資料作為色調值的來源

```
df1.plot.scatter(x='A', y='B', c='C', cmap='coolwarm')
```

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

* 將原始資料作為尺度的來源

```
df1.plot.scatter(x='A', y='B', s=df1['C']*100)
```

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

## 8.Box plot

* 基本用法, 關於boxplot的定義可以參考[此篇筆記](https://jenhsuan.gitbooks.io/python/content/217python-for-data-visualization-seaborn/2173categorical-plots.html)

```
df2.plot.box()
```

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

* 將兩資料的box plot畫在一起

```
df1[['A','B']].plot.box()
```

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

## 9.Density plot

* 基本用法

```
df2.plot.density()
```

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

* 改變線條的style (width, size)

```
df2.plot.density(lw=5,ls='--')
```

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

## 10.Hexbin plot

* 基本用法

```
df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])df.plot.hexbin(x='a', y='b', gridsize=25, cmap='coolwarm')
```

![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202018-05-27%20下午10.25.56.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/2323.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.
