> 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/wqew/2182plotly-and-cufflinks.md).

# 2.1.8.2.Plotly and Cufflinks

## 2.1.8.2.Plotly and Cufflinks

### 1. 使用library

```
import pandas as pd
import numpy as np
from plotly import __version__
```

* 確認版本

```
from plotly import __version__
print(__version__)
```

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

```
%matplotlib inline
```

* 使用Cufflinks

```
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
cf.go_offline()
```

* 產生DataFrame資料
  * 產生(100, 4)的隨機DataFrame

    ```
    df = pd.DataFrame(np.random.randn(100, 4), columns='A B C D'.split())
    df.head()
    ```

    ![](/files/-M4M0TZO2PVO0asWvI6F)
  * 產生Category, Values的DataFrame

    ```
    df2 = pd.DataFrame({'Category':['A', 'B', 'C'], 'Values':[32, 43, 50]})
    df2.head()
    ```

    ![](/files/-M4M0TZQvnmi6N3mYEgw)

### 2. 畫圖的基本操作

```
df.iplot()
```

![](/files/-M4M0TZSt7W88et0DjJc)

## 3. Scatter plot

```
df.iplot(kind = 'scatter', x = 'A', y = 'B')
```

![](/files/-M4M0TZUK_9Ln4y9bjUc)

* 點狀

```
df.iplot(kind = 'scatter', x = 'A', y = 'B', mode = 'markers', size = 20)
```

![](/files/-M4M0TZWXROjDLr19FC5)

## 4. Bar plot

* 畫出特定資料的bar

```
df2.iplot(kind='bar', x='Category', y='Values')
```

![](/files/-M4M0TZYGdtlGYxYuKBO)

* 各種類數量的bar

```
df.count().iplot(kind='bar')
```

![](/files/-M4M0TZ_XBnWI2G5ffu0)

## 5. Box plot

```
df.iplot(kind='box')
```

![](/files/-M4M0TZb6vGKE2PWikUO)

## 6.3D surface plot

* 產生三維資料

```
df3 = pd.DataFrame({'x':[1,2,3,4,5], 'y':[10,20,30,40,50], 'z':[500,400,300,200,100]})
```

![](/files/-M4M0TZdRcrSR-Hwg6kk)

* 基本用法

```
df3.iplot(kind='surface')
```

![](/files/-M4M0TZfu3I611IlKUuC)

* 改變色調

```
df3.iplot(kind='surface', colorscale = 'rdylbu')
```

![](/files/-M4M0TZhIbAMDFRqFpih)

## 7. Histogram plot

```
df.iplot(kind='hist')
```

![](/files/-M4M0TZjJjdGT7Swkc4S)

## 8. Spread plot

```
df[['A', 'B']].iplot(kind='spread')
```

![](/files/-M4M0TZltinJRnUrDzBK)

## 9. Bubble plot

```
df.iplot(kind='bubble', x='A', y='B',size='C')
```

![](/files/-M4M0TZnE7dIdA8CB-Jm)

## 10. Scatter matrix plot

```
df.scatter_matrix()
```

![](/files/-M4M0TZpnm_6ys_jgZwt)
