Skip to content

Commit 9c058c0

Browse files
committed
dendrogram
1 parent b718c8b commit 9c058c0

File tree

1 file changed

+47
-73
lines changed

1 file changed

+47
-73
lines changed

notebooks/dendrogram.md

Lines changed: 47 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ jupyter:
1111
display_name: Python 3
1212
language: python
1313
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.6.7
1424
plotly:
1525
description: How to make a dendrogram in Python with Plotly.
1626
display_as: scientific
@@ -24,69 +34,58 @@ jupyter:
2434
permalink: python/dendrogram/
2535
thumbnail: thumbnail/dendrogram.jpg
2636
title: Dendrograms | Plotly
37+
v4upgrade: true
2738
---
2839

29-
#### New to Plotly?
30-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
31-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
32-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
33-
#### Version Check
34-
Note: Dendrograms are available in version 1.8.7+.
35-
Run `pip install plotly --upgrade` to update your Plotly version.
40+
#### Basic Dendrogram
3641

37-
```python
38-
import plotly
39-
plotly.__version__
40-
```
42+
A [dendrogram](https://en.wikipedia.org/wiki/Dendrogram) is a diagram representing a tree. The figure factory `create_dendrogram` performs [hierachical clustering](https://en.wikipedia.org/wiki/Hierarchical_clustering) on data and represents the resulting tree. Values on the tree depth axis correspond to distances between clusters.
4143

42-
##### Basic Dendrogram
44+
Dendrogram plots are commonly used in computational biology to show the clustering of genes or samples, sometimes in the margin of heatmaps.
4345

4446
```python
45-
import plotly.plotly as py
4647
import plotly.figure_factory as ff
47-
4848
import numpy as np
4949

50-
X = np.random.rand(15, 15)
51-
dendro = ff.create_dendrogram(X)
52-
dendro['layout'].update({'width':800, 'height':500})
53-
py.iplot(dendro, filename='simple_dendrogram')
50+
X = np.random.rand(15, 12) # 15 samples, with 12 dimensions each
51+
fig = ff.create_dendrogram(X)
52+
fig.update_layout(width=800, height=500)
53+
fig.show()
5454
```
5555

56-
##### Set Color Threshold
56+
#### Set Color Threshold
5757

5858
```python
59-
import plotly.plotly as py
6059
import plotly.figure_factory as ff
6160

6261
import numpy as np
6362

64-
X = np.random.rand(15, 15)
65-
dendro = ff.create_dendrogram(X, color_threshold=1.5)
66-
dendro['layout'].update({'width':800, 'height':500})
67-
py.iplot(dendro, filename='simple_dendrogram_with_color_threshold')
63+
X = np.random.rand(15, 10) # 15 samples, with 10 dimensions each
64+
fig = ff.create_dendrogram(X, color_threshold=1.5)
65+
fig.update_layout(width=800, height=500)
66+
fig.show()
6867
```
6968

70-
##### Set Orientation and Add Labels
69+
#### Set Orientation and Add Labels
7170

7271
```python
73-
import plotly.plotly as py
7472
import plotly.figure_factory as ff
7573

7674
import numpy as np
7775

78-
X = np.random.rand(10, 10)
76+
X = np.random.rand(10, 12)
7977
names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark', 'Alice', 'Charlie', 'Rob', 'Lisa', 'Lily']
8078
fig = ff.create_dendrogram(X, orientation='left', labels=names)
81-
fig['layout'].update({'width':800, 'height':800})
82-
py.iplot(fig, filename='dendrogram_with_labels')
79+
fig.update_layout(width=800, height=800)
80+
fig.show()
8381
```
8482

85-
##### Plot a Dendrogram with a Heatmap
83+
#### Plot a Dendrogram with a Heatmap
84+
85+
See also the [Dash Bio demo](https://dash-bio.plotly.host/dash-clustergram/).
8686

8787
```python
88-
import plotly.plotly as py
89-
import plotly.graph_objs as go
88+
import plotly.graph_objects as go
9089
import plotly.figure_factory as ff
9190

9291
import numpy as np
@@ -101,9 +100,9 @@ data_array = data_array.transpose()
101100
labels = data.dtype.names
102101

103102
# Initialize figure by creating upper dendrogram
104-
figure = ff.create_dendrogram(data_array, orientation='bottom', labels=labels)
105-
for i in range(len(figure['data'])):
106-
figure['data'][i]['yaxis'] = 'y2'
103+
fig = ff.create_dendrogram(data_array, orientation='bottom', labels=labels)
104+
for i in range(len(fig['data'])):
105+
fig['data'][i]['yaxis'] = 'y2'
107106

108107
# Create Side Dendrogram
109108
dendro_side = ff.create_dendrogram(data_array, orientation='right')
@@ -112,7 +111,7 @@ for i in range(len(dendro_side['data'])):
112111

113112
# Add Side Dendrogram Data to Figure
114113
for data in dendro_side['data']:
115-
figure.add_trace(data)
114+
fig.add_trace(data)
116115

117116
# Create Heatmap
118117
dendro_leaves = dendro_side['layout']['yaxis']['ticktext']
@@ -131,82 +130,57 @@ heatmap = [
131130
)
132131
]
133132

134-
heatmap[0]['x'] = figure['layout']['xaxis']['tickvals']
133+
heatmap[0]['x'] = fig['layout']['xaxis']['tickvals']
135134
heatmap[0]['y'] = dendro_side['layout']['yaxis']['tickvals']
136135

137136
# Add Heatmap Data to Figure
138137
for data in heatmap:
139-
figure.add_trace(data)
138+
fig.add_trace(data)
140139

141140
# Edit Layout
142-
figure['layout'].update({'width':800, 'height':800,
141+
fig.update_layout({'width':800, 'height':800,
143142
'showlegend':False, 'hovermode': 'closest',
144143
})
145144
# Edit xaxis
146-
figure['layout']['xaxis'].update({'domain': [.15, 1],
145+
fig.update_layout(xaxis={'domain': [.15, 1],
147146
'mirror': False,
148147
'showgrid': False,
149148
'showline': False,
150149
'zeroline': False,
151150
'ticks':""})
152151
# Edit xaxis2
153-
figure['layout'].update({'xaxis2': {'domain': [0, .15],
152+
fig.update_layout(xaxis2={'domain': [0, .15],
154153
'mirror': False,
155154
'showgrid': False,
156155
'showline': False,
157156
'zeroline': False,
158157
'showticklabels': False,
159-
'ticks':""}})
158+
'ticks':""})
160159

161160
# Edit yaxis
162-
figure['layout']['yaxis'].update({'domain': [0, .85],
161+
fig.update_layout(yaxis={'domain': [0, .85],
163162
'mirror': False,
164163
'showgrid': False,
165164
'showline': False,
166165
'zeroline': False,
167166
'showticklabels': False,
168-
'ticks': ""})
167+
'ticks': ""
168+
})
169169
# Edit yaxis2
170-
figure['layout'].update({'yaxis2':{'domain':[.825, .975],
170+
fig.update_layout(yaxis2={'domain':[.825, .975],
171171
'mirror': False,
172172
'showgrid': False,
173173
'showline': False,
174174
'zeroline': False,
175175
'showticklabels': False,
176-
'ticks':""}})
176+
'ticks':""})
177177

178178
# Plot!
179-
py.iplot(figure, filename='dendrogram_with_heatmap')
180-
```
181-
182-
```python
183-
dendro_side['layout']['xaxis']
179+
fig.show()
184180
```
185181

186182
### Reference
187183

188184
```python
189185
help(ff.create_dendrogram)
190186
```
191-
192-
```python
193-
from IPython.display import display, HTML
194-
195-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
196-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
197-
198-
! pip install git+https://github.com/plotly/publisher.git --upgrade
199-
import publisher
200-
publisher.publish(
201-
'dendrograms.ipynb', 'python/dendrogram/', 'Python Dendrograms',
202-
'How to make a dendrogram in Python with Plotly. ',
203-
name = 'Dendrograms',
204-
title = "Dendrograms | Plotly",
205-
thumbnail='thumbnail/dendrogram.jpg', language='python',
206-
has_thumbnail='true', display_as='scientific', order=6,
207-
ipynb= '~notebook_demo/262')
208-
```
209-
210-
```python
211-
212-
```

0 commit comments

Comments
 (0)