Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!
Note: Dendrograms are available in version 1.8.7+.
Run pip install plotly --upgrade
to update your Plotly version.
import plotly
plotly.__version__
'3.4.2'
import plotly.plotly as py
import plotly.figure_factory as ff
import numpy as np
X = np.random.rand(15, 15)
dendro = ff.create_dendrogram(X)
dendro['layout'].update({'width':800, 'height':500})
py.iplot(dendro, filename='simple_dendrogram')
import plotly.plotly as py
import plotly.figure_factory as ff
import numpy as np
X = np.random.rand(15, 15)
dendro = ff.create_dendrogram(X, color_threshold=1.5)
dendro['layout'].update({'width':800, 'height':500})
py.iplot(dendro, filename='simple_dendrogram_with_color_threshold')
import plotly.plotly as py
import plotly.figure_factory as ff
import numpy as np
X = np.random.rand(10, 10)
names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark', 'Alice', 'Charlie', 'Rob', 'Lisa', 'Lily']
fig = ff.create_dendrogram(X, orientation='left', labels=names)
fig['layout'].update({'width':800, 'height':800})
py.iplot(fig, filename='dendrogram_with_labels')
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.figure_factory as ff
import numpy as np
from scipy.spatial.distance import pdist, squareform
# get data
data = np.genfromtxt("http://files.figshare.com/2133304/ExpRawData_E_TABM_84_A_AFFY_44.tab",
names=True,usecols=tuple(range(1,30)),dtype=float, delimiter="\t")
data_array = data.view((np.float, len(data.dtype.names)))
data_array = data_array.transpose()
labels = data.dtype.names
# Initialize figure by creating upper dendrogram
figure = ff.create_dendrogram(data_array, orientation='bottom', labels=labels)
for i in range(len(figure['data'])):
figure['data'][i]['yaxis'] = 'y2'
# Create Side Dendrogram
dendro_side = ff.create_dendrogram(data_array, orientation='right')
for i in range(len(dendro_side['data'])):
dendro_side['data'][i]['xaxis'] = 'x2'
# Add Side Dendrogram Data to Figure
for data in dendro_side['data']:
figure.add_trace(data)
# Create Heatmap
dendro_leaves = dendro_side['layout']['yaxis']['ticktext']
dendro_leaves = list(map(int, dendro_leaves))
data_dist = pdist(data_array)
heat_data = squareform(data_dist)
heat_data = heat_data[dendro_leaves,:]
heat_data = heat_data[:,dendro_leaves]
heatmap = [
go.Heatmap(
x = dendro_leaves,
y = dendro_leaves,
z = heat_data,
colorscale = 'Blues'
)
]
heatmap[0]['x'] = figure['layout']['xaxis']['tickvals']
heatmap[0]['y'] = dendro_side['layout']['yaxis']['tickvals']
# Add Heatmap Data to Figure
for data in heatmap:
figure.add_trace(data)
# Edit Layout
figure['layout'].update({'width':800, 'height':800,
'showlegend':False, 'hovermode': 'closest',
})
# Edit xaxis
figure['layout']['xaxis'].update({'domain': [.15, 1],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'ticks':""})
# Edit xaxis2
figure['layout'].update({'xaxis2': {'domain': [0, .15],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks':""}})
# Edit yaxis
figure['layout']['yaxis'].update({'domain': [0, .85],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks': ""})
# Edit yaxis2
figure['layout'].update({'yaxis2':{'domain':[.825, .975],
'mirror': False,
'showgrid': False,
'showline': False,
'zeroline': False,
'showticklabels': False,
'ticks':""}})
# Plot!
py.iplot(figure, filename='dendrogram_with_heatmap')
dendro_side['layout']['xaxis']
layout.XAxis({ 'mirror': 'allticks', 'rangemode': 'tozero', 'showgrid': False, 'showline': True, 'showticklabels': True, 'ticks': 'outside', 'type': 'linear', 'zeroline': False })
help(ff.create_dendrogram)
Help on function create_dendrogram in module plotly.figure_factory._dendrogram: create_dendrogram(X, orientation='bottom', labels=None, colorscale=None, distfun=None, linkagefun=<function <lambda> at 0x000001C336B28048>, hovertext=None, color_threshold=None) BETA function that returns a dendrogram Plotly figure object. :param (ndarray) X: Matrix of observations as array of arrays :param (str) orientation: 'top', 'right', 'bottom', or 'left' :param (list) labels: List of axis category labels(observation labels) :param (list) colorscale: Optional colorscale for dendrogram tree :param (function) distfun: Function to compute the pairwise distance from the observations :param (function) linkagefun: Function to compute the linkage matrix from the pairwise distances :param (list[list]) hovertext: List of hovertext for constituent traces of dendrogram clusters :param (double) color_threshold: Value at which the separation of clusters will be made Example 1: Simple bottom oriented dendrogram ``` import plotly.plotly as py from plotly.figure_factory import create_dendrogram import numpy as np X = np.random.rand(10,10) dendro = create_dendrogram(X) plot_url = py.plot(dendro, filename='simple-dendrogram') ``` Example 2: Dendrogram to put on the left of the heatmap ``` import plotly.plotly as py from plotly.figure_factory import create_dendrogram import numpy as np X = np.random.rand(5,5) names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark'] dendro = create_dendrogram(X, orientation='right', labels=names) dendro['layout'].update({'width':700, 'height':500}) py.iplot(dendro, filename='vertical-dendrogram') ``` Example 3: Dendrogram with Pandas ``` import plotly.plotly as py from plotly.figure_factory import create_dendrogram import numpy as np import pandas as pd Index= ['A','B','C','D','E','F','G','H','I','J'] df = pd.DataFrame(abs(np.random.randn(10, 10)), index=Index) fig = create_dendrogram(df, labels=Index) url = py.plot(fig, filename='pandas-dendrogram') ```
from IPython.display import display, HTML
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" />'))
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
! pip install git+https://github.com/plotly/publisher.git --upgrade
import publisher
publisher.publish(
'dendrograms.ipynb', 'python/dendrogram/', 'Python Dendrograms',
'How to make a dendrogram in Python with Plotly. ',
name = 'Dendrograms',
title = "Dendrograms | Plotly",
thumbnail='thumbnail/dendrogram.jpg', language='python',
has_thumbnail='true', display_as='scientific', order=6,
ipynb= '~notebook_demo/262')
Collecting git+https://github.com/plotly/publisher.git Cloning https://github.com/plotly/publisher.git to c:\users\thars\appdata\local\temp\pip-req-build-p1ivo3bg Building wheels for collected packages: publisher Running setup.py bdist_wheel for publisher: started Running setup.py bdist_wheel for publisher: finished with status 'done' Stored in directory: C:\Users\thars\AppData\Local\Temp\pip-ephem-wheel-cache-jom4cebv\wheels\99\3e\a0\fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966 Successfully built publisher Installing collected packages: publisher Found existing installation: publisher 0.13 Uninstalling publisher-0.13: Successfully uninstalled publisher-0.13 Successfully installed publisher-0.13