Plotlyを使う人にも家族がいることを、私たちは忘れていません。

# はじめに
スーパーでブロッコリーを見るとPlotlyを連想する病気にかかっています。
自分の作業用によく使うPlotlyの使い方をまとめたので公開します。
実際にグリグリ動かしたい方はkaggleのkernelにアップしてるのでそちらでグリグリしてください。
https://www.kaggle.com/kageyama/how-to-use-plotly

※ちなみにPlotlyの3Dグラフをグリグリしたことがない人は、是非とも一度グリグリした方がいい。感動する。

# データロード

データはseabornから
iris,titanic,flightsの3つを適宜使用する。

```python:
import numpy as np
import pandas as pd

import plotly as py
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.figure_factory as ff
import seaborn as sns
```

```python:
iris_data = sns.load_dataset("iris")
titanic_data = sns.load_dataset('titanic')
flights_data = sns.load_dataset('flights')
```

```python:
iris_data.head()
```
![WS000001.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/2620d75e-fca8-a84a-d1fe-3c57222ae1e3.jpeg)

```python:
titanic_data.head()
```

![WS000000.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/e94219b5-efa6-7b6b-d087-3132b3253b4e.jpeg)

```python:
flights_data.head()
```
![WS000002.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/80633939-3c07-8d92-0a69-c3cb9c9030b7.jpeg)

# Scatter Plot
```python:
# Create a trace
trace = go.Scatter(
    x = iris_data['sepal_length'] ,
    y = iris_data['petal_length'] ,
    mode = 'markers'
)

tmp = [trace]

# Plot and embed in ipython notebook!
py.offline.iplot(tmp)
```

![WS000003.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/99d5dfb5-c7ae-3604-378a-135cf8524c6e.jpeg)

```python:
# Style Scatter Plots
trace0 = go.Scatter(
    x = iris_data['sepal_length'],
    y = iris_data['petal_length'],
    name = 'length',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(152, 0, 0, .8)',
        line = dict(
            width = 2,
            color = 'rgb(0, 0, 0)'
        )
    )
)

trace1 = go.Scatter(
    x = iris_data['sepal_width'],
    y = iris_data['petal_width'],
    name = 'width',
    mode = 'markers',
    marker = dict(
        size = 10,
        color = 'rgba(255, 182, 193, .9)',
        line = dict(
            width = 2,
        )
    )
)

tmp = [trace0, trace1]

layout = dict(title = 'Styled Scatter',
              yaxis = dict(zeroline = False),
              xaxis = dict(zeroline = False)
             )

fig = dict(data=tmp, layout=layout)
py.offline.iplot(fig)
```
![WS000004.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/63936a6a-aebb-2559-548f-f8860212760b.jpeg)

# Line Plot
```python:
# Simple Line Plot

# Create a trace
trace1 = go.Scatter(
    x = flights_data[flights_data['month'] == 'January']['year'],
    y = flights_data[flights_data['month'] == 'January']['passengers'],
    name='January'
)

trace2 = go.Scatter(
    x = flights_data[flights_data['month'] == 'February']['year'],
    y = flights_data[flights_data['month'] == 'February']['passengers'],
    name='February'
)

trace3 = go.Scatter(
    x = flights_data[flights_data['month'] == 'March']['year'],
    y = flights_data[flights_data['month'] == 'March']['passengers'],
    name='March'
)

trace4 = go.Scatter(
    x = flights_data[flights_data['month'] == 'April']['year'],
    y = flights_data[flights_data['month'] == 'April']['passengers'],
    name='April'
)

trace5 = go.Scatter(
    x = flights_data[flights_data['month'] == 'May']['year'],
    y = flights_data[flights_data['month'] == 'May']['passengers'],
    name='May'
)

tmp = [trace1,trace2,trace3,trace4,trace5]

py.offline.iplot(tmp)
```

![WS000005.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/8dbfddd7-e471-76d7-42be-2df34c9fadf9.jpeg)

# Filled Area Plot

```python:
trace1 = go.Scatter(
    x = flights_data[flights_data['month'] == 'January']['year'],
    y = flights_data[flights_data['month'] == 'January']['passengers'],
    name='January',
    fill='tozeroy'
)

trace2 = go.Scatter(
    x = flights_data[flights_data['month'] == 'February']['year'],
    y = flights_data[flights_data['month'] == 'February']['passengers'],
    name='February',
    fill='tonexty'
)


tmp = [trace1,trace2]

py.offline.iplot(tmp)
```

![WS000006.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/9c91bcd0-f897-b413-ae29-7a112a4bfbbf.jpeg)

# Bar Chart
```python:
# Basic
tmp = [go.Bar(
            x=flights_data[flights_data['month'] == 'January']['year'],
            y=flights_data[flights_data['month'] == 'January']['passengers'],
            name='January'
    )]
py.offline.iplot(tmp)
```
![WS000007.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/549b6f48-f732-27c0-0fe8-b8b7cffe713e.jpeg)

```python:
# Grouped Bar Chart
trace1 = go.Bar(
    x=flights_data[flights_data['month'] == 'January']['year'],
    y=flights_data[flights_data['month'] == 'January']['passengers'],
    name='January'
)
trace2 = go.Bar(
    x=flights_data[flights_data['month'] == 'February']['year'],
    y=flights_data[flights_data['month'] == 'February']['passengers'],
    name='February'
)

tmp = [trace1, trace2]
layout = go.Layout(
    barmode='group'
)

fig = go.Figure(data=tmp, layout=layout)
py.offline.iplot(fig)
```

![WS000008.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/8e416ef0-79de-1d51-fca3-0600a3181ab6.jpeg)


```python:
# Stacked Bar Chart
trace1 = go.Bar(
    x=flights_data[flights_data['month'] == 'January']['year'],
    y=flights_data[flights_data['month'] == 'January']['passengers'],
    name='January'
)
trace2 = go.Bar(
    x=flights_data[flights_data['month'] == 'February']['year'],
    y=flights_data[flights_data['month'] == 'February']['passengers'],
    name='February'
)

tmp = [trace1, trace2]
layout = go.Layout(
    barmode='stack'
)

fig = go.Figure(data=tmp, layout=layout)
py.offline.iplot(fig)
```

![WS000009.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/7f35dc02-c7d7-0fde-34a7-6a6d95434e99.jpeg)

```python:
# Horizontal Bar Chart
trace1 = go.Bar(
    y=flights_data[flights_data['month'] == 'January']['year'],
    x=flights_data[flights_data['month'] == 'January']['passengers'],
    name='January',
    orientation = 'h',
)
trace2 = go.Bar(
    y=flights_data[flights_data['month'] == 'February']['year'],
    x=flights_data[flights_data['month'] == 'February']['passengers'],
    name='February',
    orientation = 'h',
)

tmp = [trace1, trace2]
layout = go.Layout(
    barmode='stack'
)

fig = go.Figure(data=tmp, layout=layout)
py.offline.iplot(fig)
```

![WS000010.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/b307ccb5-e034-97e0-e9fa-7a35b5fbcfc3.jpeg)

# Pie Chart

```python:
# Basic
labels=['setosa','versicolor','virginica']
values=[len(iris_data[iris_data['species']=='setosa']),
        len(iris_data[iris_data['species']=='versicolor']),
        len(iris_data[iris_data['species']=='virginica'])]

trace=go.Pie(labels=labels, values=values)

py.offline.iplot([trace])
```

![WS000011.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/86081517-7f50-4d18-404a-5df16513fd1d.jpeg)

```python:
fig = {
  "data": [
    {
      "values": [len(iris_data[iris_data['species']=='setosa']),
        len(iris_data[iris_data['species']=='versicolor']),
        len(iris_data[iris_data['species']=='virginica'])],
      "labels": ['setosa','versicolor','virginica'],
      "domain": {"column": 0},
      "name": "species",
      "hoverinfo":"label+percent+name",
      "hole": .4,
      "type": "pie"
    }],
  "layout": {
        "title":"species",
        "grid": {"rows": 1, "columns": 1},
        "annotations": [
            {
                "font": {
                    "size": 20
                },
                "showarrow": False,
                "text": "species",
                "x": 0.5,
                "y": 0.5
            }
        ]
    }
}
py.offline.iplot(fig)
```

![WS000012.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/3ffc263c-d105-8eb1-fe2d-4faee92fef32.jpeg)

# Distplot

```python:
x = iris_data['sepal_length'] 
hist_data = [x]
group_labels = ['sepal_length']

fig = ff.create_distplot(hist_data, group_labels,bin_size=.2)
py.offline.iplot(fig)
```

![WS000013.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/33afeb51-ecb7-d2e8-d170-d03494ca8165.jpeg)

```python:
# Add histogram data
x1 = iris_data['sepal_length'] 
x2 = iris_data['sepal_width']  
x3 = iris_data['petal_length'] 
x4 = iris_data['petal_width'] 

# Group data together
hist_data = [x1, x2, x3, x4]

group_labels = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width']

# Create distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels, bin_size=.2)

# Plot!
py.offline.iplot(fig)
```

![WS000014.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/e856abf4-7a11-6d74-9e7c-6c5045044862.jpeg)

```python:
# Create distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels, bin_size=[.1, .25, .5, 1])

# Plot!
py.offline.iplot(fig)
```

![WS000015.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/e16ac3ec-4c20-3e5f-ff61-82435be75f47.jpeg)

```python:
rug_text_one = ['a', 'b', 'c', 'd', 'e',
                'f', 'g', 'h', 'i', 'j', 
                'k', 'l', 'm', 'n', 'o',
                'p', 'q', 'r', 's', 't', 
                'u', 'v', 'w', 'x', 'y', 'z'] 

rug_text_two = ['aa', 'bb', 'cc', 'dd', 'ee',
                'ff', 'gg', 'hh', 'ii', 'jj', 
                'kk', 'll', 'mm', 'nn', 'oo',
                'pp', 'qq', 'rr', 'ss', 'tt', 
                'uu', 'vv', 'ww', 'xx', 'yy', 'zz'] 

rug_text_three = ['aaa', 'bbb', 'ccc', 'ddd', 'eee',
                'fff', 'ggg', 'hhh', 'iii', 'jjj', 
                'kkk', 'lll', 'mmm', 'nnn', 'ooo',
                'ppp', 'qqq', 'rrr', 'sss', 'ttt', 
                'uuu', 'vvv', 'www', 'xxx', 'yyy', 'zzz'] 

rug_text_four = ['aaaa', 'bbbb', 'cccc', 'dddd', 'eeee',
                'ffff', 'gggg', 'hhhh', 'iiii', 'jjjj', 
                'kkkk', 'llll', 'mmmm', 'nnnn', 'oooo',
                'pppp', 'qqqq', 'rrrr', 'ssss', 'tttt', 
                'uuuu', 'vvvv', 'wwww', 'xxxx', 'yyyy', 'zzzz'] 

rug_text = [rug_text_one, rug_text_two,rug_text_three,rug_text_four]

colors = ['rgb(0, 0, 100)', 'rgb(0, 100, 100)','rgb(100, 100, 100)','rgb(100, 100, 200)']

fig = ff.create_distplot(
    hist_data, group_labels, bin_size=.2,
    rug_text=rug_text, colors=colors)

fig['layout'].update(title='Customized Distplot')

# Plot!
py.offline.iplot(fig)
```

![WS000016.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/519dec71-4a99-9705-1e94-7790b1927fb4.jpeg)

```python:
trace0 = go.Box(
    y=iris_data['sepal_length'],
    name='sepal_length'
)
trace1 = go.Box(
    y=iris_data['sepal_width'],
    name='sepal_width'
)
tmp = [trace0, trace1]
py.offline.iplot(tmp)
```

![WS000017.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/20d88e40-a6cb-cfc9-f857-53ff6bb24f3a.jpeg)

# Boxplot

```python:
trace0 = go.Box(
    x=iris_data['sepal_length'],
    name='sepal_length'
)
trace1 = go.Box(
    x=iris_data['sepal_width'],
    name='sepal_width'
)
tmp = [trace0, trace1]
py.offline.iplot(tmp)
```

![WS000018.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/06b5bccd-4fe9-8065-5e4f-353952cf7cb0.jpeg)

```python:
trace0 = go.Box(
    y=iris_data['sepal_length'],
    name='sepal_length',
    boxpoints='all', 
    jitter=0.3,
    pointpos=-1.8
)
tmp = [trace0]
py.offline.iplot(tmp)
```

![WS000019.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/b260c349-e9c2-3794-4d18-9b34e2a09fa0.jpeg)

```python:
trace0 = go.Box(
    y=iris_data['sepal_length'],
    name='sepal_length',
    marker = dict(
        color = 'rgb(214, 12, 140)',
    )
)
trace1 = go.Box(
    y=iris_data['sepal_width'],
    name='sepal_width',
    marker = dict(
        color = 'rgb(0, 128, 128)',
    )
)
tmp = [trace0, trace1]
py.offline.iplot(tmp)
```

![WS000020.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/c03dd6ca-85fa-a6e3-555a-7fc9ac80492c.jpeg)

```python:
trace0 = go.Box(
    y=iris_data['sepal_length'],
    name='sepal_length',
    boxmean=True
)
trace1 = go.Box(
    y=iris_data['sepal_width'],
    name='sepal_width',
    boxmean='sd'
)
tmp = [trace0, trace1]
py.offline.iplot(tmp)
```

![WS000021.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/0bc57183-0fa5-2b07-6bf2-1ab10fca4abd.jpeg)

```python:
trace0 = go.Box(
    y=iris_data['sepal_length'],
    name='sepal_length',
    x=iris_data['species']
)
trace1 = go.Box(
    y=iris_data['sepal_width'],
    name='sepal_width',
    x=iris_data['species']
)
tmp = [trace0, trace1]
layout = go.Layout(
    boxmode='group'
)
fig = go.Figure(data=tmp, layout=layout)
py.offline.iplot(fig)
```

![WS000022.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/1db9970a-45ae-2a4e-04a3-ae7e266e587d.jpeg)

```python:
x_data = ['sepal_length', 'sepal_width',
          'petal_length', 'petal_width']
y0 = iris_data['sepal_length']
y1 = iris_data['sepal_width']
y2 = iris_data['petal_length']
y3 = iris_data['petal_width']

y_data = [y0,y1,y2,y3]

colors = ['rgba(93, 164, 214, 0.5)', 'rgba(255, 144, 14, 0.5)', 'rgba(44, 160, 101, 0.5)', 'rgba(255, 65, 54, 0.5)']

traces = []

for xd, yd, cls in zip(x_data, y_data, colors):
        traces.append(go.Box(
            y=yd,
            name=xd,
            boxpoints='all',
            jitter=0.5,
            whiskerwidth=0.2,
            fillcolor=cls,
            marker=dict(
                size=2,
            ),
            line=dict(width=1),
        ))

layout = go.Layout(
    title='Iris',
    yaxis=dict(
        autorange=True,
        showgrid=True,
        zeroline=True,
        dtick=5,
        gridcolor='rgb(255, 255, 255)',
        gridwidth=1,
        zerolinecolor='rgb(255, 255, 255)',
        zerolinewidth=2,
    ),
    margin=dict(
        l=40,
        r=30,
        b=80,
        t=100,
    ),
    paper_bgcolor='rgb(243, 243, 243)',
    plot_bgcolor='rgb(243, 243, 243)',
    showlegend=False
)

fig = go.Figure(data=traces, layout=layout)
py.offline.iplot(fig)
```

![WS000023.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/a8677efd-3e4e-9f1f-1809-11b2628f4af9.jpeg)

```python:
x_data = ['sepal_length', 'sepal_width',
          'petal_length', 'petal_width']
y0 = iris_data['sepal_length']
y1 = iris_data['sepal_width']
y2 = iris_data['petal_length']
y3 = iris_data['petal_width']

y_data = [y0,y1,y2,y3]

colors = ['rgba(93, 164, 214, 0.5)', 'rgba(255, 144, 14, 0.5)', 'rgba(44, 160, 101, 0.5)', 'rgba(255, 65, 54, 0.5)']

traces = []

for xd, yd, cls in zip(x_data, y_data, colors):
        traces.append(go.Box(
            y=yd,
            x=iris_data['species'],
            name=xd,
            boxpoints='all',
            jitter=0.5,
            whiskerwidth=0.2,
            fillcolor=cls,
            marker=dict(
                size=2,
            ),
            line=dict(width=1),
        ))

layout = go.Layout(
    title='Iris',
    yaxis=dict(
        autorange=True,
        showgrid=True,
        zeroline=True,
        dtick=5,
        gridcolor='rgb(255, 255, 255)',
        gridwidth=1,
        zerolinecolor='rgb(255, 255, 255)',
        zerolinewidth=2,
    ),
    margin=dict(
        l=40,
        r=30,
        b=80,
        t=100,
    ),
    paper_bgcolor='rgb(243, 243, 243)',
    plot_bgcolor='rgb(243, 243, 243)',
    showlegend=False,
    boxmode='group'
)

fig = go.Figure(data=traces, layout=layout)
py.offline.iplot(fig)
```

![WS000024.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/07d787a5-250e-14fb-7bd0-a85954cc8ef6.jpeg)

# Histgram

```python:
# Basic Histogram
x = iris_data['sepal_length']
tmp = [go.Histogram(x=x)]

py.offline.iplot(tmp)
```

![WS000025.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/1a9f31ec-8704-14d2-751b-8936f8037f4e.jpeg)

```python:
# Normalized Histogram
tmp = [go.Histogram(x=x,histnorm='probability')]

py.offline.iplot(tmp)
```

![WS000026.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/e4d01c3b-aa8f-53a9-3c16-853be3846f50.jpeg)

```python:
# Horizontal Histogram
tmp = [go.Histogram(y=x)]

py.offline.iplot(tmp)
```

![WS000027.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/1ba96c5d-b7cf-0e13-f690-5ec1227078f7.jpeg)

```python:
# Overlaid Histogram
x0 = iris_data['sepal_length']
x1 = iris_data['petal_length']

trace1 = go.Histogram(
    x=x0,
    opacity=0.75,
    name='sepal_length'
)
trace2 = go.Histogram(
    x=x1,
    opacity=0.75,
    name='petal_length'
)

tmp = [trace1, trace2]
layout = go.Layout(barmode='overlay')
fig = go.Figure(data=tmp, layout=layout)

py.offline.iplot(fig)
```

![WS000028.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/2bba3141-c683-b463-18f8-308fe469cb32.jpeg)

```python:
# Stacked Histograms
layout = go.Layout(barmode='stack')
fig = go.Figure(data=tmp, layout=layout)

py.offline.iplot(fig)
```

![WS000029.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/d38fc841-7a07-fef7-b8b7-7dad2d1ad9b8.jpeg)

```python:
# Styled Histogram
x0 = iris_data['sepal_length']
x1 = iris_data['petal_length']

trace1 = go.Histogram(
    x=x0,
    histnorm='percent',
    name='sepal_length',
    xbins=dict(
        start=3.0,
        end=8.0,
        size=0.5
    ),
    marker=dict(
        color='#FFD7E9',
    ),
    opacity=0.75
)
trace2 = go.Histogram(
    x=x1,
    name='petal_length',
    xbins=dict(
        start=3.0,
        end=8.0,
        size=0.5
    ),
    marker=dict(
        color='#EB89B5'
    ),
    opacity=0.75
)
tmp = [trace1, trace2]

layout = go.Layout(
    title='Sampled Results',
    xaxis=dict(
        title='Value'
    ),
    yaxis=dict(
        title='Count'
    ),
    bargap=0.2,
    bargroupgap=0.1
)
fig = go.Figure(data=tmp, layout=layout)
py.offline.iplot(fig)
```

![WS000030.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/f63466bb-e305-c3f4-125f-8c227f6245f6.jpeg)

```python:
# Specify Binning Function
x = iris_data['species']
y = iris_data['sepal_length']

tmp = [
  go.Histogram(
    histfunc = "count",
    y = y,
    x = x,
    name = "count"
  ),
  go.Histogram(
    histfunc = "sum",
    y = y,
    x = x,
    name = "sum"
  )
]

py.offline.iplot(tmp)
```

![WS000031.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/0a54b3de-b96c-842a-33a4-53a073219563.jpeg)

# 2D Histogram

```python:
# 2D Histogram of a Bivariate Normal Distribution
x = iris_data['sepal_length']
y = iris_data['petal_length']

tmp = [
    go.Histogram2d(
        x=x,
        y=y
    )
]
py.offline.iplot(tmp)
```

![WS000032.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/e021d5be-df62-8f20-7074-492060e7c992.jpeg)

```python:
# 2D Histogram Binning and Styling Options
x = iris_data['sepal_length']
y = iris_data['petal_length']

tmp = [
    go.Histogram2d(x=x, y=y, histnorm='probability',
        autobinx=False,
        xbins=dict(start=4, end=8, size=0.1),
        autobiny=False,
        ybins=dict(start=0, end=8, size=0.1),
        colorscale=[[0, 'rgb(12,51,131)'], [0.25, 'rgb(10,136,186)'], [0.5, 'rgb(242,211,56)'], [0.75, 'rgb(242,143,56)'], [1, 'rgb(217,30,30)']]
    )
]
py.offline.iplot(tmp)
```

![WS000033.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/256819af-ce27-5c2c-dbbd-29fee7411726.jpeg)

```python:
# 2D Histogram Overlaid with a Scatter Chart
x0 = iris_data['sepal_length']
y0 = iris_data['petal_length']
x1 = iris_data['sepal_width']
y1 = iris_data['petal_width']

x = np.concatenate([x0, x1])
y = np.concatenate([y0, y1])

trace1 = go.Scatter(
    x=x0,
    y=y0,
    mode='markers',
    showlegend=False,
    marker=dict(
        symbol='x',
        opacity=0.7,
        color='white',
        size=8,
        line=dict(width=1),
    )
)
trace2 = go.Scatter(
    x=x1,
    y=y1,
    mode='markers',
    showlegend=False,
    marker=dict(
        symbol='circle',
        opacity=0.7,
        color='white',
        size=8,
        line=dict(width=1),
    )
)
trace3 = go.Histogram2d(
    x=x,
    y=y,
    colorscale='YlGnBu',
    zmax=10,
    nbinsx=14,
    nbinsy=14,
    zauto=False,
)

layout = go.Layout(
    xaxis=dict( ticks='', showgrid=False, zeroline=False, nticks=20 ),
    yaxis=dict( ticks='', showgrid=False, zeroline=False, nticks=20 ),
    autosize=False,
    height=550,
    width=550,
    hovermode='closest',

)
tmp = [trace1, trace2, trace3]
fig = go.Figure(data=tmp, layout=layout)

py.offline.iplot(fig)
```

![WS000034.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/b179251c-5c8e-fa2a-076b-767488f684c8.jpeg)

# 2D Density Plot

```python:
# 2D Histogram Contour Plot with Histogram Subplots
t = np.linspace(-1, 1.2, 2000)
x = iris_data['sepal_length']
y = iris_data['petal_length']

colorscale = ['#7A4579', '#D56073', 'rgb(236,158,105)', (1, 1, 0.2), (0.98,0.98,0.98)]

fig = ff.create_2d_density(
    x, y, colorscale=colorscale,
    hist_color='rgb(255, 237, 222)', point_size=3
)

py.offline.iplot(fig)
```

![WS000035.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/6d4961f3-26fb-c6fc-4ecb-672d36ed750d.jpeg)

# Facet and Trellis Plot

```python:
# Facet by Column
fig = ff.create_facet_grid(
    iris_data,
    x='sepal_length',
    y='petal_length',
    facet_col='species',
)

py.offline.iplot(fig)
```

![WS000036.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/960608a6-0c99-8915-24ed-f7ec8a5602eb.jpeg)

```python:
# Facet by Row
fig = ff.create_facet_grid(
    iris_data,
    x='sepal_length',
    y='petal_length',
    facet_row='species',
)

py.offline.iplot(fig)
```

![WS000037.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/50d7f860-a922-d783-09b0-f5f054cb352d.jpeg)

```python:
# Facet by Row and Column

fig = ff.create_facet_grid(
    titanic_data,
    x='fare',
    y='age',
    facet_row='sex',
    facet_col='alive',
    color_name='class',
    color_is_cat=True,
)

py.offline.iplot(fig)
```

![WS000038.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/ce53f958-f6f5-5402-4241-d8940abc78cf.jpeg)

```python:
# Custom Colormap
fig = ff.create_facet_grid(
    titanic_data,
    x='fare',
    y='age',
    color_name='sex',
    show_boxes=False,
    marker={'size': 10, 'opacity': 1.0},
    colormap={'male': 'rgb(165, 242, 242)', 'female': 'rgb(253, 174, 216)'}
)
py.offline.iplot(fig)
```

![WS000039.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/8dfa0e81-359f-5ad5-8e04-1651bbcc7d00.jpeg)

```python:
# Plot with Histogram Traces
fig = ff.create_facet_grid(
    titanic_data,
    x='fare',
    y='age',
    facet_row='sex',
    facet_col='alive',
    trace_type='histogram',
)

py.offline.iplot(fig)
```

![WS000040.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/0aa8ccb9-d211-1ba9-de32-6e1bf04d74d3.jpeg)

```python:
# Plot with BoxPlot Traces
fig = ff.create_facet_grid(
    titanic_data,
    y='age',
    facet_row='sex',
    facet_col='alive',
    trace_type='box',
)

py.offline.iplot(fig)
```

![WS000041.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/407d3179-d2c3-8c84-9ef6-43b4c2aa425c.jpeg)

# Scatterplot Matrix

```python:
classes=np.unique(iris_data['species'].values).tolist()
class_code={classes[k]: k for k in range(3)}
color_vals=[class_code[cl] for cl in iris_data['species']]
text=[iris_data.loc[ k, 'species'] for k in range(len(iris_data))]

pl_colorscale=[[0.0, '#19d3f3'],
               [0.333, '#19d3f3'],
               [0.333, '#e763fa'],
               [0.666, '#e763fa'],
               [0.666, '#636efa'],
               [1, '#636efa']]

trace1 = go.Splom(dimensions=[dict(label='sepal_length',
                                 values=iris_data['sepal_length']),
                            dict(label='sepal_width',
                                 values=iris_data['sepal_width']),
                            dict(label='petal_length',
                                 values=iris_data['petal_length']),
                            dict(label='petal_width',
                                 values=iris_data['petal_width'])],
                text=text,
                marker=dict(color=color_vals,
                            size=7,
                            colorscale=pl_colorscale,
                            showscale=False,
                            line=dict(width=0.5,
                                      color='rgb(230,230,230)'))
                )

axis = dict(showline=True,
          zeroline=False,
          gridcolor='#fff',
          ticklen=4)

layout = go.Layout(
    title='Iris Data set',
    dragmode='select',
    width=600,
    height=600,
    autosize=False,
    hovermode='closest',
    plot_bgcolor='rgba(240,240,240, 0.95)',
    xaxis1=dict(axis),
    xaxis2=dict(axis),
    xaxis3=dict(axis),
    xaxis4=dict(axis),
    yaxis1=dict(axis),
    yaxis2=dict(axis),
    yaxis3=dict(axis),
    yaxis4=dict(axis)
)

fig1 = dict(data=[trace1], layout=layout)
py.offline.iplot(fig1)

```

![WS000042.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/e3c01902-3294-7f01-1bc0-aca3c98c528e.jpeg)

# Violin Plot

```python:
# Basic Violin Plot
fig = {
    "data": [{
        "type": 'violin',
        "y": iris_data['sepal_length'],
        "box": {
            "visible": True
        },
        "line": {
            "color": 'black'
        },
        "meanline": {
            "visible": True
        },
        "fillcolor": '#8dd3c7',
        "opacity": 0.6,
        "x0": 'sepal_length'
    }],
    "layout" : {
        "title": "",
        "yaxis": {
            "zeroline": False,
        }
    }
}

py.offline.iplot(fig)
```

![WS000043.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/e7b2155b-4989-3554-ae5a-85204b72ed80.jpeg)

```python:
# Multiple Traces
tmp = []
for i in range(0,len(pd.unique(iris_data['species']))):
    trace = {
            "type": 'violin',
            "x": iris_data['species'][iris_data['species'] == pd.unique(iris_data['species'])[i]],
            "y": iris_data['sepal_length'][iris_data['species'] == pd.unique(iris_data['species'])[i]],
            "name": pd.unique(iris_data['species'])[i],
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            }
        }
    tmp.append(trace)

        
fig = {
    "data": tmp,
    "layout" : {
        "title": "",
        "yaxis": {
            "zeroline": False,
        }
    }
}

py.offline.iplot(fig)
```

![WS000044.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/f8159171-5b04-fe18-76fb-ea3e8a56ca35.jpeg)

```python:
# Grouped Violin Plot
fig = {
    "data": [
        {
            "type": 'violin',
            "x": titanic_data['class'][ titanic_data['sex'] == 'male' ],
            "y": titanic_data['age'] [ titanic_data['sex'] == 'male' ],
            "legendgroup": 'male',
            "scalegroup": 'male',
            "name": 'male',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'blue'
            }
        },
        {
            "type": 'violin',
            "x": titanic_data['class'][ titanic_data['sex'] == 'female' ],
            "y": titanic_data['age'] [ titanic_data['sex'] == 'female' ],
            "legendgroup": 'female',
            "scalegroup": 'female',
            "name": 'female',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'pink'
            }
        }
    ],
    "layout" : {
        "yaxis": {
            "zeroline": False,
        },
        "violinmode": "group"
    }
}


py.offline.iplot(fig)
```

![WS000045.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/aca39d25-d365-8ae7-61c3-2def4e90a554.jpeg)

```python:
# Split Violin Plot
fig = {
    "data": [
        {
            "type": 'violin',
            "x": titanic_data['class'][ titanic_data['sex'] == 'male' ],
            "y": titanic_data['age'] [ titanic_data['sex'] == 'male' ],
            "legendgroup": 'male',
            "scalegroup": 'male',
            "name": 'male',
            "side": 'negative',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'blue'
            }
        },
        {
            "type": 'violin',
            "x": titanic_data['class'][ titanic_data['sex'] == 'female' ],
            "y": titanic_data['age'] [ titanic_data['sex'] == 'female' ],
            "legendgroup": 'female',
            "scalegroup": 'female',
            "name": 'female',
            "side": 'positive',
            "box": {
                "visible": True
            },
            "meanline": {
                "visible": True
            },
            "line": {
                "color": 'pink'
            }
        }
    ],
     "layout" : {
        "yaxis": {
            "zeroline": False,
        },
        "violingap": 0,
        "violinmode": "overlay"
    }
}


py.offline.iplot(fig)
```

![WS000046.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/83346b28-b8ac-0016-70cf-bb731e5e6b25.jpeg)

# Heat Map

```python:
# Basic
trace = go.Heatmap(z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]])
data=[trace]
py.offline.iplot(data)
```

![WS000047.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/2f42991d-d108-96f8-6d1c-ee5cfc92d7b2.jpeg)

```python:
# Heatmap with Categorical Axis Labels
data = pd.pivot_table(data=flights_data, values='passengers', columns='year', index='month', aggfunc=np.mean)
data
```

![WS000048.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/60166f0d-83e3-3444-7c2a-78b1a9bd57fd.jpeg)

```python:
trace = go.Heatmap(z=data.values,
                   x=data.columns,
                   y=data.index)
data=[trace]
py.offline.iplot(data)
```

![WS000049.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/c776249b-faa3-6db9-a28d-8e225e43ba38.jpeg)

# 3D Surface

```python:
pivot_data = pd.pivot_table(data=flights_data, values='passengers', columns='year', index='month', aggfunc=np.mean)
data = [
    go.Surface(
        x=pivot_data.columns,
        y=pivot_data.index,
        z=pivot_data.values
    )
]
layout = go.Layout(
    title='flights',
    autosize=False,
    width=500,
    height=500,
    margin=dict(
        l=65,
        r=50,
        b=65,
        t=90
    )
)
fig = go.Figure(data=data, layout=layout)
py.offline.iplot(fig)
```

![WS000050.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/b7a11e0a-0045-8ba3-0f89-eb43a879850a.jpeg)

# Restyle Button

```python:
pivot_data = pd.pivot_table(data=flights_data, values='passengers', columns='year', index='month', aggfunc=np.mean)
data = [
    go.Surface(
        x=pivot_data.columns,
        y=pivot_data.index,
        z=pivot_data.values
    )
]
layout = go.Layout(
    width=800,
    height=900,
    autosize=False,
    margin=dict(t=0, b=0, l=0, r=0),
    scene=dict(
        xaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        ),
        yaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230, 230)'
        ),
        zaxis=dict(
            gridcolor='rgb(255, 255, 255)',
            zerolinecolor='rgb(255, 255, 255)',
            showbackground=True,
            backgroundcolor='rgb(230, 230,230)'
        ),
        aspectratio = dict(x=1, y=1, z=0.7),
        aspectmode = 'manual'
    )
)

updatemenus=list([
    dict(
        buttons=list([   
            dict(
                args=['type', 'surface'],
                label='3D Surface',
                method='restyle'
            ),
            dict(
                args=['type', 'heatmap'],
                label='Heatmap',
                method='restyle'
            )             
        ]),
        direction = 'left',
        pad = {'r': 10, 't': 10},
        showactive = True,
        type = 'buttons',
        x = 0.1,
        xanchor = 'left',
        y = 1.1,
        yanchor = 'top' 
    ),
])

annotations = list([
    dict(text='Trace type:', x=1949, y=1.085, yref='paper', align='left', showarrow=False)
])
layout['updatemenus'] = updatemenus
layout['annotations'] = annotations

fig = dict(data=data, layout=layout)
py.offline.iplot(fig)
```

![WS000051.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/09a5151d-427e-4bd2-e7fd-9cd3f225f91c.jpeg)

![WS000052.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/53ce398e-4f97-8e5b-e733-a1b4021236b8.jpeg)

# 3D Scatter

```python:
trace1 = go.Scatter3d(
    x=iris_data['sepal_length'],
    y=iris_data['petal_length'],
    z=iris_data['sepal_width'],
    mode='markers',
    marker=dict(
        size=12,
        color=iris_data['sepal_width'],                # set color to an array/list of desired values
        colorscale='Viridis',   # choose a colorscale
        opacity=0.8
    )
)

data = [trace1]
layout = go.Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)
fig = go.Figure(data=data, layout=layout)
py.offline.iplot(fig)
```

![WS000053.JPG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/273349/53aeecce-c8d7-8753-3255-65cee600208b.jpeg)

# 最後に
Plotlyは確かに綺麗なグラフが描けるんだけど、コーディングが結構煩雑になりやすい。
コーディングミスにも気を配る必要が出てくるから、特にこだわりが無いならSeaborn使った方が早いし無難かなと感じる。
会議とか見せ方を工夫したいときには、Plotly使うといいと思う。