{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### New to Plotly?\n", "Plotly's Python library is free and open source! [Get started](https://plotly.com/python/getting-started/) by downloading the client and [reading the primer](https://plotly.com/python/getting-started/).\n", "
You can set up Plotly to work in [online](https://plotly.com/python/getting-started/#initialization-for-online-plotting) or [offline](https://plotly.com/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plotly.com/python/getting-started/#start-plotting-online).\n", "
We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Basic Tree-Plot in Plotly with [igraph](http://igraph.org/python/)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'0.7.1'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import plotly.plotly as py\n", "import plotly.graph_objs as go\n", "\n", "import igraph\n", "from igraph import *\n", "igraph.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Set Up Tree with [igraph](http://igraph.org/python/)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "nr_vertices = 25\n", "v_label = map(str, range(nr_vertices))\n", "G = Graph.Tree(nr_vertices, 2) # 2 stands for children number\n", "lay = G.layout('rt')\n", "\n", "position = {k: lay[k] for k in range(nr_vertices)}\n", "Y = [lay[k][1] for k in range(nr_vertices)]\n", "M = max(Y)\n", "\n", "es = EdgeSeq(G) # sequence of edges\n", "E = [e.tuple for e in G.es] # list of edges\n", "\n", "L = len(position)\n", "Xn = [position[k][0] for k in range(L)]\n", "Yn = [2*M-position[k][1] for k in range(L)]\n", "Xe = []\n", "Ye = []\n", "for edge in E:\n", " Xe+=[position[edge[0]][0],position[edge[1]][0], None]\n", " Ye+=[2*M-position[edge[0]][1],2*M-position[edge[1]][1], None] \n", "\n", "labels = v_label" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create Plotly Traces" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "lines = go.Scatter(x=Xe,\n", " y=Ye,\n", " mode='lines',\n", " line=dict(color='rgb(210,210,210)', width=1),\n", " hoverinfo='none'\n", " )\n", "dots = go.Scatter(x=Xn,\n", " y=Yn,\n", " mode='markers',\n", " name='',\n", " marker=dict(symbol='dot',\n", " size=18, \n", " color='#6175c1', #'#DB4551', \n", " line=dict(color='rgb(50,50,50)', width=1)\n", " ),\n", " text=labels,\n", " hoverinfo='text',\n", " opacity=0.8\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create Text Inside the Circle via Annotations" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_annotations(pos, text, font_size=10, font_color='rgb(250,250,250)'):\n", " L=len(pos)\n", " if len(text)!=L:\n", " raise ValueError('The lists pos and text must have the same len')\n", " annotations = go.Annotations()\n", " for k in range(L):\n", " annotations.append(\n", " go.Annotation(\n", " text=labels[k], # or replace labels with a different list for the text within the circle \n", " x=pos[k][0], y=2*M-position[k][1],\n", " xref='x1', yref='y1',\n", " font=dict(color=font_color, size=font_size),\n", " showarrow=False)\n", " )\n", " return annotations " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add Axis Specifications and Create the Layout" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "axis = dict(showline=False, # hide axis line, grid, ticklabels and title\n", " zeroline=False,\n", " showgrid=False,\n", " showticklabels=False,\n", " )\n", "\n", "layout = dict(title= 'Tree with Reingold-Tilford Layout', \n", " annotations=make_annotations(position, v_label),\n", " font=dict(size=12),\n", " showlegend=False,\n", " xaxis=go.XAxis(axis),\n", " yaxis=go.YAxis(axis), \n", " margin=dict(l=40, r=40, b=85, t=100),\n", " hovermode='closest',\n", " plot_bgcolor='rgb(248,248,248)' \n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Plot!" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data=go.Data([lines, dots])\n", "fig=dict(data=data, layout=layout)\n", "fig['layout'].update(annotations=make_annotations(position, v_label))\n", "py.iplot(fig, filename='Tree-Reingold-Tilf')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reference\n", "See https://plotly.com/python/reference/ for more information and chart attribute options and http://igraph.org/python/ for more information about the igraph package!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Collecting git+https://github.com/plotly/publisher.git\n", " Cloning https://github.com/plotly/publisher.git to /var/folders/ld/6cl3s_l50wd40tdjq2b03jxh0000gp/T/pip-aq6dfc11-build\n", "Installing collected packages: publisher\n", " Found existing installation: publisher 0.10\n", " Uninstalling publisher-0.10:\n", " Successfully uninstalled publisher-0.10\n", " Running setup.py install for publisher ... \u001b[?25l-\b \b\\\b \bdone\n", "\u001b[?25hSuccessfully installed publisher-0.10\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/IPython/nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated. You should import from nbconvert instead.\n", " \"You should import from nbconvert instead.\", ShimWarning)\n", "/Users/brandendunbar/Desktop/test/venv/lib/python2.7/site-packages/publisher/publisher.py:53: UserWarning: Did you \"Save\" this notebook before running this command? Remember to save, always save.\n", " warnings.warn('Did you \"Save\" this notebook before running this command? '\n" ] } ], "source": [ "from IPython.display import display, HTML\n", "\n", "display(HTML(''))\n", "display(HTML(''))\n", "\n", "! pip install git+https://github.com/plotly/publisher.git --upgrade\n", "import publisher\n", "publisher.publish(\n", " 'tree-plot.ipynb', 'python/tree-plots/', 'Python Tree-plots | plotly',\n", " 'How to make interactive tree-plot in Python with Plotly. '\n", " 'An examples of a tree-plot in Plotly.',\n", " title = 'Python Tree-plots | plotly',\n", " name = 'Tree-plots',\n", " thumbnail='thumbnail/treeplot.jpg', language='python',\n", " page_type='example_index', has_thumbnail='true', display_as='statistical', order=10.5,\n", " ipynb= '~notebook_demo/28')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }