{
"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": [
"#### Network reflecting coappearances of characters in
Victor Hugo's novel Les Miserables"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We define our graph as an `igraph.Graph` object. [Python `igraph`](http://igraph.org/python/)\n",
"is a library for high-performance graph generation and analysis. Install the Python library with `sudo pip install python-igraph`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import igraph as ig"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read graph data from a `json` file:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[u'nodes', u'links']\n"
]
}
],
"source": [
"import json\n",
"import urllib2\n",
"\n",
"data = []\n",
"req = urllib2.Request(\"https://raw.githubusercontent.com/plotly/datasets/master/miserables.json\")\n",
"opener = urllib2.build_opener()\n",
"f = opener.open(req)\n",
"data = json.loads(f.read())\n",
"\n",
"print data.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get the number of nodes:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"77"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"N=len(data['nodes'])\n",
"N"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the list of edges and the Graph object from Edges:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"L=len(data['links'])\n",
"Edges=[(data['links'][k]['source'], data['links'][k]['target']) for k in range(L)]\n",
"\n",
"G=ig.Graph(Edges, directed=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extract the node attributes, 'group', and 'name':"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{u'group': 1, u'name': u'Myriel'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data['nodes'][0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"labels=[]\n",
"group=[]\n",
"for node in data['nodes']:\n",
" labels.append(node['name'])\n",
" group.append(node['group'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get the node positions, set by the Kamada-Kawai layout for 3D graphs:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"layt=G.layout('kk', dim=3) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`layt` is a list of three elements lists (the coordinates of nodes):"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4.195949332184983, 1.172321178571202, -2.5543268281789135]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layt[5]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set data for the Plotly plot of the graph:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"Xn=[layt[k][0] for k in range(N)]# x-coordinates of nodes\n",
"Yn=[layt[k][1] for k in range(N)]# y-coordinates\n",
"Zn=[layt[k][2] for k in range(N)]# z-coordinates\n",
"Xe=[]\n",
"Ye=[]\n",
"Ze=[]\n",
"for e in Edges:\n",
" Xe+=[layt[e[0]][0],layt[e[1]][0], None]# x-coordinates of edge ends\n",
" Ye+=[layt[e[0]][1],layt[e[1]][1], None] \n",
" Ze+=[layt[e[0]][2],layt[e[1]][2], None] "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import plotly.plotly as py\n",
"import plotly.graph_objs as go\n",
"\n",
"trace1=go.Scatter3d(x=Xe,\n",
" y=Ye,\n",
" z=Ze,\n",
" mode='lines',\n",
" line=dict(color='rgb(125,125,125)', width=1),\n",
" hoverinfo='none'\n",
" )\n",
"\n",
"trace2=go.Scatter3d(x=Xn,\n",
" y=Yn,\n",
" z=Zn,\n",
" mode='markers',\n",
" name='actors',\n",
" marker=dict(symbol='circle',\n",
" size=6,\n",
" color=group,\n",
" colorscale='Viridis',\n",
" line=dict(color='rgb(50,50,50)', width=0.5)\n",
" ),\n",
" text=labels,\n",
" hoverinfo='text'\n",
" )\n",
"\n",
"axis=dict(showbackground=False,\n",
" showline=False,\n",
" zeroline=False,\n",
" showgrid=False,\n",
" showticklabels=False,\n",
" title=''\n",
" )\n",
"\n",
"layout = go.Layout(\n",
" title=\"Network of coappearances of characters in Victor Hugo's novel
Les Miserables (3D visualization)\",\n",
" width=1000,\n",
" height=1000,\n",
" showlegend=False,\n",
" scene=dict(\n",
" xaxis=dict(axis),\n",
" yaxis=dict(axis),\n",
" zaxis=dict(axis),\n",
" ),\n",
" margin=dict(\n",
" t=100\n",
" ),\n",
" hovermode='closest',\n",
" annotations=[\n",
" dict(\n",
" showarrow=False,\n",
" text=\"Data source: [1] miserables.json\",\n",
" xref='paper',\n",
" yref='paper',\n",
" x=0,\n",
" y=0.1,\n",
" xanchor='left',\n",
" yanchor='bottom',\n",
" font=dict(\n",
" size=14\n",
" )\n",
" )\n",
" ], )"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data=[trace1, trace2]\n",
"fig=go.Figure(data=data, layout=layout)\n",
"\n",
"py.iplot(fig, filename='Les-Miserables')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Reference\n",
"See https://plotly.com/python/reference/#scatter3d for more information and chart attribute options!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"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 c:\\users\\thars\\appdata\\local\\temp\\pip-req-build-ogrbpd\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-unt43t\\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",
"Successfully installed publisher-0.11\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Anaconda\\Anaconda2\\lib\\site-packages\\IPython\\nbconvert.py:13: ShimWarning: The `IPython.nbconvert` package has been deprecated since IPython 4.0. You should import from nbconvert instead.\n",
" \"You should import from nbconvert instead.\", ShimWarning)\n",
"C:\\Anaconda\\Anaconda2\\lib\\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",
"\n",
"import publisher\n",
"publisher.publish(\n",
" 'Les-miserables-network.ipynb', 'python/3d-network-graph/', 'Python 3D Network Graphs',\n",
" 'How to make 3D Network Graphs in Python. ',\n",
" title = '3D Network Graphs in Python | plotly',\n",
" name = '3D Network Graphs',\n",
" has_thumbnail='true', thumbnail='thumbnail/3dnetwork.jpg', \n",
" language='python', page_type='example_index', \n",
" display_as='3d_charts', order=13,\n",
" ipynb= '~notebook_demo/226')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.14"
}
},
"nbformat": 4,
"nbformat_minor": 1
}