Skip to content

Instantly share code, notes, and snippets.

@aloncohen1
Created November 17, 2019 23:17
Show Gist options
  • Save aloncohen1/bb92a1cbed81b74c76cd20ee2f23dc2a to your computer and use it in GitHub Desktop.
Save aloncohen1/bb92a1cbed81b74c76cd20ee2f23dc2a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"race_colors = {'men':'#7A84DD','unknown':'#7A84DD', 'dwarves':\"#B15B60\", 'elves':'#8ACAE5', 'hobbits':'#BD9267', 'ents':'#F1A54D', \n",
" 'orcs': \"#020104\",'ainur':'#3A7575',\"half-elven\":\"#8ACAE5\",\"ents\":\"#E3845D\",\"dragons\":\"#F10301\"\n",
" ,\"trolls\":\"#863E34\"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from pyvis.network import Network\n",
"from unidecode import unidecode\n",
"import urllib\n",
"\n",
"\n",
"G = Network(height=\"800px\", width=\"100%\", bgcolor=\"#222222\", font_color=\"white\",notebook=True)\n",
"\n",
"for character, weight in character_count.items():\n",
" if weight > 15:\n",
" character=unidecode(urllib.unquote_plus(character))\n",
" if character in race_dict.keys():\n",
" if race_dict[character] in race_colors.keys():\n",
" color = race_colors[race_dict[character]]\n",
" else:\n",
" color = None\n",
" else:\n",
" color = None\n",
" G.add_node(character,\n",
" title=\"\",\n",
" weight=weight,\n",
" size=weight,\n",
"# size=1,\n",
" color=color,\n",
" label=character)\n",
"\n",
"for character_net, weight in character_network.items():\n",
" if weight >= 2:\n",
" character_0=unidecode(urllib.unquote_plus(character_net[0]))\n",
" character_1=unidecode(urllib.unquote_plus(character_net[1]))\n",
" if character_0 in G.node_ids and character_1 in G.node_ids:\n",
"\n",
" G.add_edge(character_0,\n",
" character_1,\n",
" weight=weight,\n",
" title=character_0+' -> '+character_1+': '+str(weight)\n",
" ,width=1.5)\n",
"\n",
"G.barnes_hut(gravity=-5000, central_gravity=0, spring_length=200, spring_strength=0.009, damping=0.025, overlap=0)\n",
"G.show('lotr_network_graph.html')"
]
}
],
"metadata": {
"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": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment