【密度の可視化】plotlyで動的な可視化をする【python3,hist,kde,join,contour,heat map】

python==3.8
plotly==4.10.0

















#ヒストグラム

積み上げるか、透かすかはbarmodeで操作する

##透かし

```python
import plotly.graph_objects as go
df = px.data.tips()

fig = px.histogram(df, x="total_bill", y="tip", color="sex", marginal="rug", hover_data=df.columns)
fig.update_layout(barmode='overlay')
fig.update_traces(opacity=0.75)
fig.show()
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/f19c9534-43e2-bb92-4af9-82f61caf2ab6.png)



##積み上げ

```python
import plotly.graph_objects as go
df = px.data.tips()

fig = px.histogram(df, x="total_bill", y="tip", color="sex", marginal="violin", hover_data=df.columns)
fig.update_layout(barmode='stack')
fig.update_traces(opacity=0.75)
fig.show()
```


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/c2acc7e9-2a43-c4bc-dc67-36ffacece9de.png)





#累積ヒストグラム(cumsum)

```python
import plotly.graph_objects as go
df = px.data.tips()
fig = go.Figure(data=[go.Histogram(x=df.tip.values, cumulative_enabled=True)])
fig.show()
```

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/4755495a-08d3-d594-a9e4-0446c6b915b1.png)







#カーネル密度推定(kde)とhist

kdeをplotするにはfigure_factoryのdistplotをつかう
すこしめんどくさい
データはarrayをdict型で渡す必要がある


```python
import plotly.figure_factory as ff
df = px.data.tips()
group_labels=["total"]
fig = ff.create_distplot([df["total_bill"].values], group_labels)
fig.show()
```



![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/d9a02d6f-bcee-d269-4481-2bced632da5b.png)


##histとkdeで色分け

arrayの入れ子状で渡すことで自然に色分けしてくれる

```python
import plotly.figure_factory as ff
df = px.data.tips()
hist_data = [df.query("day=='Sun'").tip.values, 
             df.query("day=='Sat'").tip.values, 
             df.query("day=='Thur'").tip.values]

group_labels = ['Sun', 'Sat', 'Thur']

fig = ff.create_distplot(hist_data, group_labels, bin_size=.2)
fig.show()
```


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/7f51ee49-6a0a-9f73-9d57-c1f45dccf6a2.png)



##histを消してkdeだけを残す



```python
import plotly.figure_factory as ff
df = px.data.tips()
hist_data = [df.query("day=='Sun'").tip.values, 
             df.query("day=='Sat'").tip.values, 
             df.query("day=='Thur'").tip.values]

group_labels = ['Sun', 'Sat', 'Thur']

fig = ff.create_distplot(hist_data, group_labels, bin_size=.2,show_hist=False)

fig.update_layout(title_text='only kde plot')

fig.show()
```


他にもcolors,bin_size,show_curveなどが指定できる





![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/d30fa76b-b8bb-36d4-cd33-d7c053b25567.png)




#密度等高線図


```python
import plotly.express as px
df = px.data.iris()
fig = px.density_contour(df, x="sepal_width", y="sepal_length")
fig.show()
```


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/636a1563-1e60-791f-704d-afde89efa64a.png)




##等高線の高さで塗りつぶす

```python
import plotly.express as px
df = px.data.iris()
fig = px.density_contour(df, x="sepal_width", y="sepal_length")
fig.update_traces(contours_coloring="fill", contours_showlabels = True)
fig.show()
```




![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/add124e9-7d89-d2ed-cc01-27d7e7e93e85.png)


##こういう作り方もある


```python
import plotly.express as px

df = px.data.tips()
fig = go.Figure()

fig.add_trace(go.Histogram2dContour(
        x = df['total_bill'].values,
        y = df['tip'].values,
        colorscale = 'Blues',
        reversescale = True,
        xaxis = 'x',
        yaxis = 'y'
    ))

fig.show()
```



![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/3ba0cf37-609c-4e2d-4bbe-760ad07a5020.png)







##join plotも密度等高線図から作れる

```python
import plotly.express as px
df = px.data.iris()
fig = px.density_contour(df, x="sepal_width", y="sepal_length", color="species", marginal_x="rug", marginal_y="histogram")
fig.show()
```




![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/5418c9b0-c396-ebf0-2fa8-6fd68875e727.png)




#heat map


##ヒストグラム同士の等高線図をheatmapとして表すjoin plot

```python
import plotly.express as px
df = px.data.tips()
fig = px.density_heatmap(df, x="total_bill", y="tip", marginal_x="rug", marginal_y="histogram")
fig.show()
```




![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/275572/e87ecd11-6ed0-bed8-9fc5-846275e95f7e.png)