diff --git a/_posts/python/scientific/dendrogram/2015-06-30-dendrograms.html b/_posts/python/scientific/dendrogram/2015-06-30-dendrograms.html index 7a921a3d279d..b509f064d03d 100644 --- a/_posts/python/scientific/dendrogram/2015-06-30-dendrograms.html +++ b/_posts/python/scientific/dendrogram/2015-06-30-dendrograms.html @@ -1,17 +1,17 @@ --- permalink: python/dendrogram/ -description: How to make a dendrogram in Python with Plotly. +description: How to make a dendrogram in Python with Plotly. name: Python Dendrograms has_thumbnail: true thumbnail: thumbnail/dendrogram.jpg -layout: user-guide name: Dendrograms -language: python title: Dendrograms | Plotly -display_as: scientific +language: python has_thumbnail: true -ipynb: ~notebook_demo/36 +display_as: scientific order: 6 +ipynb: ~notebook_demo/262 +layout: user-guide page_type: u-guide --- {% raw %} @@ -33,7 +33,7 @@

Version CheckIn [1]:
@@ -72,13 +72,13 @@
Basic Dendrogram
-
In [2]:
+
In [7]:
+ +
+
+
In [4]:
+
+
+
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']
@@ -145,7 +193,7 @@ 
Set Orientation and Add Labels -
Out[3]:
+
Out[4]:
@@ -172,17 +220,17 @@
Plot a Dendrogram with a HeatmapIn [5]:
-
import plotly.plotly as py
-import plotly.graph_objs as go
-import plotly.figure_factory as ff
+
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
+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")
+                     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
@@ -198,7 +246,8 @@ 
Plot a Dendrogram with a Heatmapdendro_side['data'][i]['xaxis'] = 'x2' # Add Side Dendrogram Data to Figure -figure['data'].extend(dendro_side['data']) +for data in dendro_side['data']: + figure.add_trace(data) # Create Heatmap dendro_leaves = dendro_side['layout']['yaxis']['ticktext'] @@ -210,10 +259,10 @@
Plot a Dendrogram with a Heatmapheatmap = [ go.Heatmap( - x = dendro_leaves, + x = dendro_leaves, y = dendro_leaves, - z = heat_data, - colorscale = 'YIGnBu' + z = heat_data, + colorscale = 'Blues' ) ] @@ -221,43 +270,44 @@
Plot a Dendrogram with a Heatmapheatmap[0]['y'] = dendro_side['layout']['yaxis']['tickvals'] # Add Heatmap Data to Figure -figure['data'].extend(heatmap) +for data in heatmap: + figure.add_trace(data) # Edit Layout figure['layout'].update({'width':800, 'height':800, - 'showlegend':False, 'hovermode': 'closest', + 'showlegend':False, 'hovermode': 'closest', }) # Edit xaxis figure['layout']['xaxis'].update({'domain': [.15, 1], - 'mirror': False, - 'showgrid': False, - 'showline': False, - 'zeroline': False, + '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, + '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, + '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, + 'mirror': False, + 'showgrid': False, + 'showline': False, + 'zeroline': False, + 'showticklabels': False, 'ticks':""}}) # Plot! @@ -293,7 +343,7 @@
Plot a Dendrogram with a HeatmapIn [6]:
@@ -338,10 +390,10 @@

Reference<

-
In [7]:
+
In [8]:
-
help(ff.create_dendrogram)
+
help(ff.create_dendrogram)
 
@@ -360,9 +412,9 @@

Reference<
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>>, hovertext=None)
+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)
@@ -370,47 +422,47 @@ 

Reference< :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 + the pairwise distances :param (list[list]) hovertext: List of hovertext for constituent traces of dendrogram - - clusters - + 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) @@ -425,6 +477,6 @@

Reference<

+ - -{% endraw %} +{% endraw %} \ No newline at end of file diff --git a/_posts/python/scientific/dendrogram/dendrograms.ipynb b/_posts/python/scientific/dendrogram/dendrograms.ipynb index 0de03132ad68..0e63b4372451 100644 --- a/_posts/python/scientific/dendrogram/dendrograms.ipynb +++ b/_posts/python/scientific/dendrogram/dendrograms.ipynb @@ -21,7 +21,7 @@ { "data": { "text/plain": [ - "'2.4.1'" + "'3.4.2'" ] }, "execution_count": 1, @@ -43,7 +43,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -55,7 +55,7 @@ "" ] }, - "execution_count": 2, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -76,7 +76,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "##### Set Orientation and Add Labels" + "##### Set Color Threshold" ] }, { @@ -87,7 +87,7 @@ { "data": { "text/html": [ - "" + "" ], "text/plain": [ "" @@ -98,6 +98,44 @@ "output_type": "execute_result" } ], + "source": [ + "import plotly.plotly as py\n", + "import plotly.figure_factory as ff\n", + "\n", + "import numpy as np\n", + "\n", + "X = np.random.rand(15, 15)\n", + "dendro = ff.create_dendrogram(X, color_threshold=1.5)\n", + "dendro['layout'].update({'width':800, 'height':500})\n", + "py.iplot(dendro, filename='simple_dendrogram_with_color_threshold')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Set Orientation and Add Labels" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import plotly.plotly as py\n", "import plotly.figure_factory as ff\n", @@ -166,7 +204,8 @@ " dendro_side['data'][i]['xaxis'] = 'x2'\n", "\n", "# Add Side Dendrogram Data to Figure\n", - "figure['data'].extend(dendro_side['data'])\n", + "for data in dendro_side['data']:\n", + " figure.add_trace(data)\n", "\n", "# Create Heatmap\n", "dendro_leaves = dendro_side['layout']['yaxis']['ticktext']\n", @@ -181,7 +220,7 @@ " x = dendro_leaves, \n", " y = dendro_leaves,\n", " z = heat_data, \n", - " colorscale = 'YIGnBu'\n", + " colorscale = 'Blues'\n", " )\n", "]\n", "\n", @@ -189,7 +228,8 @@ "heatmap[0]['y'] = dendro_side['layout']['yaxis']['tickvals']\n", "\n", "# Add Heatmap Data to Figure\n", - "figure['data'].extend(heatmap)\n", + "for data in heatmap:\n", + " figure.add_trace(data)\n", "\n", "# Edit Layout\n", "figure['layout'].update({'width':800, 'height':800,\n", @@ -242,14 +282,16 @@ { "data": { "text/plain": [ - "{'mirror': 'allticks',\n", - " 'rangemode': 'tozero',\n", - " 'showgrid': False,\n", - " 'showline': True,\n", - " 'showticklabels': True,\n", - " 'ticks': 'outside',\n", - " 'type': 'linear',\n", - " 'zeroline': False}" + "layout.XAxis({\n", + " 'mirror': 'allticks',\n", + " 'rangemode': 'tozero',\n", + " 'showgrid': False,\n", + " 'showline': True,\n", + " 'showticklabels': True,\n", + " 'ticks': 'outside',\n", + " 'type': 'linear',\n", + " 'zeroline': False\n", + "})" ] }, "execution_count": 6, @@ -270,7 +312,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -279,7 +321,7 @@ "text": [ "Help on function create_dendrogram in module plotly.figure_factory._dendrogram:\n", "\n", - "create_dendrogram(X, orientation='bottom', labels=None, colorscale=None, distfun=None, linkagefun=>, hovertext=None)\n", + "create_dendrogram(X, orientation='bottom', labels=None, colorscale=None, distfun=None, linkagefun= at 0x000001C336B28048>, hovertext=None, color_threshold=None)\n", " BETA function that returns a dendrogram Plotly figure object.\n", " \n", " :param (ndarray) X: Matrix of observations as array of arrays\n", @@ -289,10 +331,10 @@ " :param (function) distfun: Function to compute the pairwise distance from\n", " the observations\n", " :param (function) linkagefun: Function to compute the linkage matrix from\n", - " the pairwise distances\n", + " the pairwise distances\n", " :param (list[list]) hovertext: List of hovertext for constituent traces of dendrogram\n", - " \n", - " clusters\n", + " clusters\n", + " :param (double) color_threshold: Value at which the separation of clusters will be made\n", " \n", " Example 1: Simple bottom oriented dendrogram\n", " ```\n", @@ -345,7 +387,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -377,28 +419,17 @@ "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", - " Cloning https://github.com/plotly/publisher.git to c:\\users\\brand\\appdata\\local\\temp\\pip-req-build-m6pl12mp\n", + " Cloning https://github.com/plotly/publisher.git to c:\\users\\thars\\appdata\\local\\temp\\pip-req-build-p1ivo3bg\n", + "Building wheels for collected packages: publisher\n", + " Running setup.py bdist_wheel for publisher: started\n", + " Running setup.py bdist_wheel for publisher: finished with status 'done'\n", + " Stored in directory: C:\\Users\\thars\\AppData\\Local\\Temp\\pip-ephem-wheel-cache-jom4cebv\\wheels\\99\\3e\\a0\\fbd22ba24cca72bdbaba53dbc23c1768755fb17b3af0f33966\n", + "Successfully built publisher\n", "Installing collected packages: publisher\n", - " Found existing installation: publisher 0.11\n", - " Uninstalling publisher-0.11:\n", - " Successfully uninstalled publisher-0.11\n", - " Running setup.py install for publisher: started\n", - " Running setup.py install for publisher: finished with status 'done'\n", - "Successfully installed publisher-0.11\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "C:\\Python27\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning:\n", - "\n", - "The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n", - "\n", - "C:\\Python27\\lib\\site-packages\\publisher\\publisher.py:53: UserWarning:\n", - "\n", - "Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", - "\n" + " Found existing installation: publisher 0.13\n", + " Uninstalling publisher-0.13:\n", + " Successfully uninstalled publisher-0.13\n", + "Successfully installed publisher-0.13\n" ] } ], @@ -417,15 +448,13 @@ " title = \"Dendrograms | Plotly\",\n", " thumbnail='thumbnail/dendrogram.jpg', language='python',\n", " has_thumbnail='true', display_as='scientific', order=6,\n", - " ipynb= '~notebook_demo/36')" + " ipynb= '~notebook_demo/262')" ] }, { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": true - }, + "metadata": {}, "outputs": [], "source": [] } @@ -433,21 +462,21 @@ "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.14" + "pygments_lexer": "ipython3", + "version": "3.6.8" } }, "nbformat": 4,