Executing black over all jupyter notebook (#190)

Reverting black for the jupyter notebooks gets old. Can we just run
black over all of them?
This commit is contained in:
2023-10-04 20:03:47 +02:00
committed by GitHub
parent 030ad00c7d
commit 41f2c9f995
15 changed files with 658 additions and 487 deletions

View File

@@ -79,16 +79,20 @@
"import numpy as np\n",
"\n",
"# create sample data for one company\n",
"data = {'Jahr': ['2017', '2018', '2019', '2020', '2021', '2022'],\n",
" 'Umsatz': [19, 23, 30, 42, 37, 45]}\n",
"data = {\n",
" \"Jahr\": [\"2017\", \"2018\", \"2019\", \"2020\", \"2021\", \"2022\"],\n",
" \"Umsatz\": [19, 23, 30, 42, 37, 45],\n",
"}\n",
"# save as pandas dataframe\n",
"df = pd.DataFrame.from_dict(data)\n",
"\n",
"# create sample data for a second company\n",
"data2 = {'Jahr': ['2017', '2018', '2019', '2020', '2021', '2022'],\n",
" 'Umsatz': [15, 21, 33, 28, 27, 30]}\n",
"data2 = {\n",
" \"Jahr\": [\"2017\", \"2018\", \"2019\", \"2020\", \"2021\", \"2022\"],\n",
" \"Umsatz\": [15, 21, 33, 28, 27, 30],\n",
"}\n",
"# save as pandas dataframe\n",
"df2 = pd.DataFrame.from_dict(data2)\n"
"df2 = pd.DataFrame.from_dict(data2)"
]
},
{
@@ -1006,12 +1010,12 @@
],
"source": [
"# create bar plot\n",
"fig_saeule = px.bar(df, y = 'Umsatz', labels = {'index': '', 'Umsatz': ''})\n",
"fig_saeule = px.bar(df, y=\"Umsatz\", labels={\"index\": \"\", \"Umsatz\": \"\"})\n",
"# set color\n",
"fig_saeule.update_traces(marker_color = '#00509b')\n",
"fig_saeule.update_traces(marker_color=\"#00509b\")\n",
"\n",
"# save as image\n",
"fig_saeule.write_image('Saeule.png')\n",
"fig_saeule.write_image(\"Saeule.png\")\n",
"# show in notebook\n",
"fig_saeule.show()"
]
@@ -1924,12 +1928,12 @@
],
"source": [
"# create horizontal bar plot\n",
"fig_balken = px.bar(df, x = 'Umsatz', labels = {'index': '', 'Umsatz': ''}, orientation='h')\n",
"fig_balken = px.bar(df, x=\"Umsatz\", labels={\"index\": \"\", \"Umsatz\": \"\"}, orientation=\"h\")\n",
"# set color\n",
"fig_balken.update_traces(marker_color = '#00509b')\n",
"fig_balken.update_traces(marker_color=\"#00509b\")\n",
"\n",
"# save as image\n",
"fig_balken.write_image('Balken.png')\n",
"fig_balken.write_image(\"Balken.png\")\n",
"# show in notebook\n",
"fig_balken.show()"
]
@@ -2841,14 +2845,19 @@
}
],
"source": [
"\n",
"# sreate bar plot with named labels and title\n",
"fig_saeule_titel = px.bar(df, x = 'Jahr', y = 'Umsatz', labels = {'Umsatz': 'Umsatz in Mio.€'}, title = 'Umsatzentwicklung von Unternehmen A')\n",
"fig_saeule_titel = px.bar(\n",
" df,\n",
" x=\"Jahr\",\n",
" y=\"Umsatz\",\n",
" labels={\"Umsatz\": \"Umsatz in Mio.€\"},\n",
" title=\"Umsatzentwicklung von Unternehmen A\",\n",
")\n",
"# set color\n",
"fig_saeule_titel.update_traces(marker_color = '#00509b')\n",
"fig_saeule_titel.update_traces(marker_color=\"#00509b\")\n",
"\n",
"# save as image\n",
"fig_saeule_titel.write_image('Saeule_Titel.png')\n",
"fig_saeule_titel.write_image(\"Saeule_Titel.png\")\n",
"# show in notebook\n",
"fig_saeule_titel.show()"
]
@@ -3764,27 +3773,31 @@
],
"source": [
"# create figure\n",
"fig_saeule_zwei= go.Figure()\n",
"fig_saeule_zwei = go.Figure()\n",
"\n",
"# add trace for company 1\n",
"fig_saeule_zwei.add_trace(go.Bar(x = df['Jahr'], y = df['Umsatz'], name = 'A', marker_color=\"#00509b\"))\n",
"fig_saeule_zwei.add_trace(\n",
" go.Bar(x=df[\"Jahr\"], y=df[\"Umsatz\"], name=\"A\", marker_color=\"#00509b\")\n",
")\n",
"\n",
"# add trace for company 2\n",
"fig_saeule_zwei.add_trace(go.Bar(x = df2['Jahr'], y = df2['Umsatz'], name = 'B', marker_color = \"#6f7072\"))\n",
"fig_saeule_zwei.add_trace(\n",
" go.Bar(x=df2[\"Jahr\"], y=df2[\"Umsatz\"], name=\"B\", marker_color=\"#6f7072\")\n",
")\n",
"\n",
"# update layout to grouped\n",
"fig_saeule_zwei.update_layout(barmode='group')\n",
"fig_saeule_zwei.update_layout(barmode=\"group\")\n",
"\n",
"# set title and labels\n",
"fig_saeule_zwei.update_layout(\n",
" title = \"Vergleich der Umsatzentwicklung\",\n",
" xaxis_title = \"Jahr\",\n",
" yaxis_title = \"Umsatz in Mio.€\",\n",
" legend_title = \"Unternehmen\",\n",
" title=\"Vergleich der Umsatzentwicklung\",\n",
" xaxis_title=\"Jahr\",\n",
" yaxis_title=\"Umsatz in Mio.€\",\n",
" legend_title=\"Unternehmen\",\n",
")\n",
"\n",
"# save as image\n",
"fig_saeule_zwei.write_image('Saeule_Zwei.png')\n",
"fig_saeule_zwei.write_image(\"Saeule_Zwei.png\")\n",
"# show in notebook\n",
"fig_saeule_zwei.show()"
]
@@ -4697,9 +4710,9 @@
],
"source": [
"# create line plot\n",
"fig_line = px.line(df, y = df['Umsatz'], labels = {'index':'', 'Umsatz': ''})\n",
"fig_line = px.line(df, y=df[\"Umsatz\"], labels={\"index\": \"\", \"Umsatz\": \"\"})\n",
"# set color\n",
"fig_line.update_traces(line_color = '#00509b')\n",
"fig_line.update_traces(line_color=\"#00509b\")\n",
"# save as image\n",
"fig_line.write_image(\"Linie.png\")\n",
"# show in network\n",
@@ -5617,15 +5630,31 @@
"# create figure\n",
"fig_line = go.Figure()\n",
"# add trace for company 1\n",
"fig_line.add_trace(go.Scatter(x = df['Jahr'], y = df['Umsatz'], name = 'A', line_color = '#00509b', marker_color = '#00509b'))\n",
"fig_line.add_trace(\n",
" go.Scatter(\n",
" x=df[\"Jahr\"],\n",
" y=df[\"Umsatz\"],\n",
" name=\"A\",\n",
" line_color=\"#00509b\",\n",
" marker_color=\"#00509b\",\n",
" )\n",
")\n",
"# add trace for company 2\n",
"fig_line.add_trace(go.Scatter(x = df2['Jahr'], y = df2['Umsatz'], name = 'B', line_color = '#6f7072', marker_color = '#6f7072'))\n",
"fig_line.add_trace(\n",
" go.Scatter(\n",
" x=df2[\"Jahr\"],\n",
" y=df2[\"Umsatz\"],\n",
" name=\"B\",\n",
" line_color=\"#6f7072\",\n",
" marker_color=\"#6f7072\",\n",
" )\n",
")\n",
"# set title and labels\n",
"fig_line.update_layout(\n",
" title = \"Vergleich der Umsatzentwicklung\",\n",
" xaxis_title = \"Jahr\",\n",
" yaxis_title = \"Umsatz in Mio.€\",\n",
" legend_title = \"Unternehmen\",\n",
" title=\"Vergleich der Umsatzentwicklung\",\n",
" xaxis_title=\"Jahr\",\n",
" yaxis_title=\"Umsatz in Mio.€\",\n",
" legend_title=\"Unternehmen\",\n",
")\n",
"# save as image\n",
"fig_line.write_image(\"Linie_Vergleich.png\")\n",
@@ -6534,13 +6563,15 @@
],
"source": [
"# create sample data\n",
"x = [1,2,2,3,5,5,6,6,6,7,8,8,8,10,10,10,7,4,3,4,9,6]\n",
"y = [1,2,3,3,3,4,5,5,6,6,7,7,8,8,9,10,2,10,8,6,6,4]\n",
"x = [1, 2, 2, 3, 5, 5, 6, 6, 6, 7, 8, 8, 8, 10, 10, 10, 7, 4, 3, 4, 9, 6]\n",
"y = [1, 2, 3, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 10, 2, 10, 8, 6, 6, 4]\n",
"\n",
"# create scatter plot\n",
"scatter = go.Figure(data=go.Scatter(x=x, y=y, mode='markers', marker = {'color': '#00509b'}))\n",
"scatter = go.Figure(\n",
" data=go.Scatter(x=x, y=y, mode=\"markers\", marker={\"color\": \"#00509b\"})\n",
")\n",
"# save as image\n",
"scatter.write_image('Streudiagramm.png')\n",
"scatter.write_image(\"Streudiagramm.png\")\n",
"# show in netbook\n",
"scatter.show()"
]
@@ -7445,13 +7476,15 @@
"source": [
"# create sample data\n",
"sentiment = [0.1, 0.3, 0.6]\n",
"scores = ['negativ', 'neutral', 'positiv']\n",
"scores = [\"negativ\", \"neutral\", \"positiv\"]\n",
"# create pie chart\n",
"fig_kreis = px.pie(values=sentiment, color = scores, color_discrete_map={'negativ':'black',\n",
" 'neutral':'#6f7072',\n",
" 'positiv':'#00509b'})\n",
"fig_kreis = px.pie(\n",
" values=sentiment,\n",
" color=scores,\n",
" color_discrete_map={\"negativ\": \"black\", \"neutral\": \"#6f7072\", \"positiv\": \"#00509b\"},\n",
")\n",
"# save as image\n",
"fig_kreis.write_image('Kreis.png')\n",
"fig_kreis.write_image(\"Kreis.png\")\n",
"# show in notebook\n",
"fig_kreis.show()"
]
@@ -8356,14 +8389,22 @@
}
],
"source": [
"# create figure \n",
"fig_sentiment = px.pie(values=sentiment, names=scores, color = scores, color_discrete_map={'negativ':'lightcoral',\n",
" 'neutral':'moccasin',\n",
" 'positiv':'darkseagreen'}, title = 'Stimmungsanalyse basierend auf Nachrichtenartikel X')\n",
"# create figure\n",
"fig_sentiment = px.pie(\n",
" values=sentiment,\n",
" names=scores,\n",
" color=scores,\n",
" color_discrete_map={\n",
" \"negativ\": \"lightcoral\",\n",
" \"neutral\": \"moccasin\",\n",
" \"positiv\": \"darkseagreen\",\n",
" },\n",
" title=\"Stimmungsanalyse basierend auf Nachrichtenartikel X\",\n",
")\n",
"# change line color\n",
"fig_sentiment.update_traces(marker = dict(line=dict(color='#000000', width=2)))\n",
"fig_sentiment.update_traces(marker=dict(line=dict(color=\"#000000\", width=2)))\n",
"# save as image\n",
"fig_sentiment.write_image('Kreis_Sentiment.png')\n",
"fig_sentiment.write_image(\"Kreis_Sentiment.png\")\n",
"# show in notebook\n",
"fig_sentiment.show()"
]
@@ -8408,34 +8449,39 @@
"compound = 0.75\n",
"angle = (compound * 100 - 50) * 1.8\n",
"\n",
"x_length = (np.sin(np.radians(angle))*1.2)/2\n",
"y_length = (np.cos(np.radians(angle))*1.2)/2\n",
"x_length = (np.sin(np.radians(angle)) * 1.2) / 2\n",
"y_length = (np.cos(np.radians(angle)) * 1.2) / 2\n",
"\n",
"if compound < 0.5:\n",
" y_tail = y_length\n",
" y_head = -y_length\n",
" x_tail = x_length\n",
" x_head = - x_length\n",
" x_head = -x_length\n",
"else:\n",
" y_tail = -y_length\n",
" y_head = y_length\n",
" x_tail = - x_length\n",
" x_tail = -x_length\n",
" x_head = x_length\n",
"\n",
"\n",
"dx = x_head - x_tail\n",
"dy = y_head - y_tail\n",
"\n",
"fig, ax = plt.subplots() \n",
"arrow = patches.FancyArrowPatch((x_tail, y_tail), (x_head, y_head),\n",
" mutation_scale=100, ec = 'darkseagreen', fc = 'darkseagreen')\n",
"fig, ax = plt.subplots()\n",
"arrow = patches.FancyArrowPatch(\n",
" (x_tail, y_tail),\n",
" (x_head, y_head),\n",
" mutation_scale=100,\n",
" ec=\"darkseagreen\",\n",
" fc=\"darkseagreen\",\n",
")\n",
"ax.add_patch(arrow)\n",
"plt.axis('off')\n",
"plt.axis(\"off\")\n",
"ax.set_xlim([-1, 1])\n",
"ax.set_ylim([-1, 1])\n",
"ax.set_title('Aktueller Stimmungstrend', fontsize=20)\n",
"fig.suptitle(' ', fontsize=24)\n",
"fig.savefig('Pfeil.png')\n"
"ax.set_title(\"Aktueller Stimmungstrend\", fontsize=20)\n",
"fig.suptitle(\" \", fontsize=24)\n",
"fig.savefig(\"Pfeil.png\")"
]
}
],

View File

@@ -92,39 +92,40 @@
"G = nx.MultiGraph()\n",
"\n",
"# create list of nodes with attributes as a dictionary\n",
"nodes = [(1, {'label': 'Firma 1', 'branche': 'Branche 1', 'land': 'Land 1'}), \n",
" (2, {'label': 'Firma 2', 'branche': 'Branche 1', 'land': 'Land 2'}),\n",
" (3, {'label': 'Firma 3', 'branche': 'Branche 1', 'land': 'Land 3'}),\n",
" (4, {'label': 'Firma 4', 'branche': 'Branche 2', 'land': 'Land 4'}),\n",
" (5, {'label': 'Firma 5', 'branche': 'Branche 2', 'land': 'Land 1'}),\n",
" (6, {'label': 'Firma 6', 'branche': 'Branche 2', 'land': 'Land 3'}),\n",
" (7, {'label': 'Firma 7', 'branche': 'Branche 3', 'land': 'Land 3'}),\n",
" (8, {'label': 'Firma 8', 'branche': 'Branche 3', 'land': 'Land 2'}),\n",
" (9, {'label': 'Firma 9', 'branche': 'Branche 4', 'land': 'Land 1'}),\n",
" (10, {'label': 'Firma 10', 'branche': 'Branche 4', 'land': 'Land 4'}),\n",
" ]\n",
"nodes = [\n",
" (1, {\"label\": \"Firma 1\", \"branche\": \"Branche 1\", \"land\": \"Land 1\"}),\n",
" (2, {\"label\": \"Firma 2\", \"branche\": \"Branche 1\", \"land\": \"Land 2\"}),\n",
" (3, {\"label\": \"Firma 3\", \"branche\": \"Branche 1\", \"land\": \"Land 3\"}),\n",
" (4, {\"label\": \"Firma 4\", \"branche\": \"Branche 2\", \"land\": \"Land 4\"}),\n",
" (5, {\"label\": \"Firma 5\", \"branche\": \"Branche 2\", \"land\": \"Land 1\"}),\n",
" (6, {\"label\": \"Firma 6\", \"branche\": \"Branche 2\", \"land\": \"Land 3\"}),\n",
" (7, {\"label\": \"Firma 7\", \"branche\": \"Branche 3\", \"land\": \"Land 3\"}),\n",
" (8, {\"label\": \"Firma 8\", \"branche\": \"Branche 3\", \"land\": \"Land 2\"}),\n",
" (9, {\"label\": \"Firma 9\", \"branche\": \"Branche 4\", \"land\": \"Land 1\"}),\n",
" (10, {\"label\": \"Firma 10\", \"branche\": \"Branche 4\", \"land\": \"Land 4\"}),\n",
"]\n",
"\n",
"# create list of edges with attributes as a dictionary\n",
"edges = [\n",
" (1, 2, {'label': 'beziehung1'}), \n",
" (5, 2, {'label': 'beziehung2'}), \n",
" (1, 3, {'label': 'beziehung3'}), \n",
" (2, 4, {'label': 'beziehung3'}), \n",
" (2, 6, {'label': 'beziehung4'}), \n",
" (2, 5, {'label': 'beziehung4'}),\n",
" (8, 10, {'label': 'beziehung4'}),\n",
" (9, 10, {'label': 'beziehung3'}), \n",
" (3, 7, {'label': 'beziehung2'}), \n",
" (6, 8, {'label': 'beziehung1'}), \n",
" (6, 9, {'label': 'beziehung1'}), \n",
" (1, 6, {'label': 'beziehung2'})\n",
" ]\n",
" (1, 2, {\"label\": \"beziehung1\"}),\n",
" (5, 2, {\"label\": \"beziehung2\"}),\n",
" (1, 3, {\"label\": \"beziehung3\"}),\n",
" (2, 4, {\"label\": \"beziehung3\"}),\n",
" (2, 6, {\"label\": \"beziehung4\"}),\n",
" (2, 5, {\"label\": \"beziehung4\"}),\n",
" (8, 10, {\"label\": \"beziehung4\"}),\n",
" (9, 10, {\"label\": \"beziehung3\"}),\n",
" (3, 7, {\"label\": \"beziehung2\"}),\n",
" (6, 8, {\"label\": \"beziehung1\"}),\n",
" (6, 9, {\"label\": \"beziehung1\"}),\n",
" (1, 6, {\"label\": \"beziehung2\"}),\n",
"]\n",
"\n",
"# add nodes to the graph\n",
"G.add_nodes_from(nodes)\n",
"\n",
"# add edges to the graph, to hide arrow heads of the edges use option arrows = 'false'\n",
"G.add_edges_from(edges, arrows = 'false')"
"G.add_edges_from(edges, arrows=\"false\")"
]
},
{
@@ -147,7 +148,9 @@
"outputs": [],
"source": [
"for node in G.nodes:\n",
" G.nodes[node]['title'] = G.nodes[node]['label'] + '\\n' + 'Anzahl Verbindungen: ' + str(G.degree[node])"
" G.nodes[node][\"title\"] = (\n",
" G.nodes[node][\"label\"] + \"\\n\" + \"Anzahl Verbindungen: \" + str(G.degree[node])\n",
" )"
]
},
{
@@ -206,16 +209,16 @@
"outputs": [],
"source": [
"# scaling the size of the nodes by 5*degree\n",
"scale = 5 \n",
"scale = 5\n",
"\n",
"# getting all nodes and their number of connections\n",
"d = dict(G.degree)\n",
"\n",
"# updating dict\n",
"d.update((x, scale*(y+1)) for x, y in d.items())\n",
"d.update((x, scale * (y + 1)) for x, y in d.items())\n",
"\n",
"# setting size attribute according to created dictionary\n",
"nx.set_node_attributes(G,d,'size')"
"nx.set_node_attributes(G, d, \"size\")"
]
},
{
@@ -236,10 +239,17 @@
"from pyvis.network import Network\n",
"\n",
"# create network, 'directed = true' allows multiple edges between nodes\n",
"nt = Network('1000px', '1000px', neighborhood_highlight=True, notebook=True, cdn_resources='in_line', directed=True)\n",
"nt = Network(\n",
" \"1000px\",\n",
" \"1000px\",\n",
" neighborhood_highlight=True,\n",
" notebook=True,\n",
" cdn_resources=\"in_line\",\n",
" directed=True,\n",
")\n",
"\n",
"# populates the nodes and edges data structures\n",
"nt.from_nx(G)\n"
"nt.from_nx(G)"
]
},
{
@@ -275,25 +285,28 @@
"outputs": [],
"source": [
"# define new function that sets the color of the nodes\n",
"def color_type (net, type):\n",
" ''' color_type sets the color of a network depending on an attribute of the nodes\n",
" net: network\n",
" type: 'branche' or 'land' '''\n",
"def color_type(net, type):\n",
" \"\"\"color_type sets the color of a network depending on an attribute of the nodes\n",
" net: network\n",
" type: 'branche' or 'land'\"\"\"\n",
"\n",
" colormap = {'Branche 1': '#87CEEB',\n",
" 'Branche 2': '#0f4c81',\n",
" 'Branche 3': '#B2FFFF', \n",
" 'Branche 4': '#191970',\n",
" 'Land 1': '#F8D568', \n",
" 'Land 2': '#F58025', \n",
" 'Land 3': '#CC5500', \n",
" 'Land 4': '#C0362C'}\n",
" colormap = {\n",
" \"Branche 1\": \"#87CEEB\",\n",
" \"Branche 2\": \"#0f4c81\",\n",
" \"Branche 3\": \"#B2FFFF\",\n",
" \"Branche 4\": \"#191970\",\n",
" \"Land 1\": \"#F8D568\",\n",
" \"Land 2\": \"#F58025\",\n",
" \"Land 3\": \"#CC5500\",\n",
" \"Land 4\": \"#C0362C\",\n",
" }\n",
" for node in net.nodes:\n",
" node['color'] = colormap[node[type]]\n",
" node[\"color\"] = colormap[node[type]]\n",
" return net\n",
"\n",
"\n",
"# set color based on attribute\n",
"nt = color_type(nt, 'branche')"
"nt = color_type(nt, \"branche\")"
]
},
{
@@ -310,8 +323,8 @@
"metadata": {},
"outputs": [],
"source": [
"# set all edge colors \n",
"nt.options.edges.color = 'grey'"
"# set all edge colors\n",
"nt.options.edges.color = \"grey\""
]
},
{
@@ -360,13 +373,20 @@
],
"source": [
"# activate physics options to try out different solver\n",
"#nt.show_buttons(filter_=['physics'])\n",
"# nt.show_buttons(filter_=['physics'])\n",
"\n",
"# set physics options\n",
"nt.barnes_hut(gravity=-8000, central_gravity=0.3, spring_length=200, spring_strength=0.1, damping=0.09, overlap=0)\n",
"nt.barnes_hut(\n",
" gravity=-8000,\n",
" central_gravity=0.3,\n",
" spring_length=200,\n",
" spring_strength=0.1,\n",
" damping=0.09,\n",
" overlap=0,\n",
")\n",
"\n",
"# create html and save in same folder\n",
"nt.show('Netzwerk_Verflechtungsanalyse.html')"
"nt.show(\"Netzwerk_Verflechtungsanalyse.html\")"
]
},
{
@@ -416,16 +436,16 @@
"from pyvis.network import Network\n",
"\n",
"sn = nx.Graph()\n",
"sn_nodes = [1,2,3,4,5,6,7]\n",
"sn_edges = [(1,4),(2,4),(3,4),(4,5),(5,6),(5,7)]\n",
"sn_nodes = [1, 2, 3, 4, 5, 6, 7]\n",
"sn_edges = [(1, 4), (2, 4), (3, 4), (4, 5), (5, 6), (5, 7)]\n",
"\n",
"sn.add_nodes_from(sn_nodes, color = '#00509b')\n",
"sn.add_nodes_from(sn_nodes, color=\"#00509b\")\n",
"sn.add_edges_from(sn_edges)\n",
"\n",
"net = Network('1000px', '1000px', notebook=True, cdn_resources='in_line')\n",
"net = Network(\"1000px\", \"1000px\", notebook=True, cdn_resources=\"in_line\")\n",
"\n",
"net.from_nx(sn)\n",
"net.show('Netzwerk.html')\n"
"net.show(\"Netzwerk.html\")"
]
}
],

View File

@@ -82,19 +82,25 @@
"import pandas as pd\n",
"\n",
"# create dataframe based on the sample data\n",
"df_nodes = pd.read_csv('nodes.csv', sep = ';')\n",
"df_nodes = pd.read_csv(\"nodes.csv\", sep=\";\")\n",
"\n",
"# define shape based on the type\n",
"node_shape = {'Company': 'dot', 'Person': 'triangle'}\n",
"df_nodes['shape'] = df_nodes['type'].map(node_shape)\n",
"node_shape = {\"Company\": \"dot\", \"Person\": \"triangle\"}\n",
"df_nodes[\"shape\"] = df_nodes[\"type\"].map(node_shape)\n",
"\n",
"# define color based on branche\n",
"node_color = {'Branche 1': ' #f3e8eeff', 'Branche 2': '#bacdb0ff', 'Branche 3': '#729b79ff', 'Branche 4': '#475b63ff', 'Branche 5': '#2e2c2fff'}\n",
"df_nodes['color'] = df_nodes['branche'].map(node_color)\n",
"node_color = {\n",
" \"Branche 1\": \" #f3e8eeff\",\n",
" \"Branche 2\": \"#bacdb0ff\",\n",
" \"Branche 3\": \"#729b79ff\",\n",
" \"Branche 4\": \"#475b63ff\",\n",
" \"Branche 5\": \"#2e2c2fff\",\n",
"}\n",
"df_nodes[\"color\"] = df_nodes[\"branche\"].map(node_color)\n",
"\n",
"# add information column that can be used for the mouse over in the graph\n",
"df_nodes = df_nodes.fillna('')\n",
"df_nodes['title'] = df_nodes['label'] + '\\n' + df_nodes['branche']\n",
"df_nodes = df_nodes.fillna(\"\")\n",
"df_nodes[\"title\"] = df_nodes[\"label\"] + \"\\n\" + df_nodes[\"branche\"]\n",
"\n",
"# show first five entries of the dataframe\n",
"print(df_nodes.head())"
@@ -127,7 +133,7 @@
],
"source": [
"# create dataframe based on the sample data\n",
"df_edges = pd.read_csv('edges.csv', sep = ';')\n",
"df_edges = pd.read_csv(\"edges.csv\", sep=\";\")\n",
"\n",
"# show first five entries of the dataframe\n",
"print(df_edges.head())"
@@ -157,10 +163,10 @@
"graph = nx.MultiGraph()\n",
"\n",
"# create edges from dataframe\n",
"graph = nx.from_pandas_edgelist(df_edges, source = 'from', target = 'to', edge_attr= 'label')\n",
"graph = nx.from_pandas_edgelist(df_edges, source=\"from\", target=\"to\", edge_attr=\"label\")\n",
"\n",
"# update node attributes from dataframe\n",
"nodes_attr = df_nodes.set_index('id').to_dict(orient = 'index')\n",
"nodes_attr = df_nodes.set_index(\"id\").to_dict(orient=\"index\")\n",
"nx.set_node_attributes(graph, nodes_attr)"
]
},
@@ -193,11 +199,11 @@
" # create empty dictionary\n",
" dict = {}\n",
" # get node id\n",
" dict['id'] = node\n",
" dict[\"id\"] = node\n",
" # get k-neighbours for k=1,2,3, subtract -1 since output of single_source_shortest_path_length contains node itself\n",
" dict['k=1'] = len(nx.single_source_shortest_path_length(graph, node, cutoff=1))-1\n",
" dict['k=2'] = len(nx.single_source_shortest_path_length(graph, node, cutoff=2))-1\n",
" dict['k=3'] = len(nx.single_source_shortest_path_length(graph, node, cutoff=3))-1\n",
" dict[\"k=1\"] = len(nx.single_source_shortest_path_length(graph, node, cutoff=1)) - 1\n",
" dict[\"k=2\"] = len(nx.single_source_shortest_path_length(graph, node, cutoff=2)) - 1\n",
" dict[\"k=3\"] = len(nx.single_source_shortest_path_length(graph, node, cutoff=3)) - 1\n",
" # append list for each node\n",
" k_neighbours.append(dict)\n",
"\n",
@@ -225,40 +231,45 @@
"from pyvis.network import Network\n",
"\n",
"# initiate network\n",
"net = Network(directed=False, neighborhood_highlight=True, bgcolor = \"white\", font_color=\"black\")\n",
"net = Network(\n",
" directed=False, neighborhood_highlight=True, bgcolor=\"white\", font_color=\"black\"\n",
")\n",
"\n",
"# pass networkx graph to pyvis\n",
"net.from_nx(graph)\n",
"\n",
"# set edge options \n",
"# set edge options\n",
"net.inherit_edge_colors(False)\n",
"net.set_edge_smooth('dynamic')\n",
"net.set_edge_smooth(\"dynamic\")\n",
"\n",
"# chose size format\n",
"size_type = 'edges' # select 'edges' or 'eigen'\n",
"size_type = \"edges\" # select 'edges' or 'eigen'\n",
"\n",
"adj_list = net.get_adj_list()\n",
"\n",
"if size_type == 'eigen':\n",
"if size_type == \"eigen\":\n",
" eigenvector = nx.eigenvector_centrality(graph)\n",
"\n",
"# calculate and update size of the nodes depending on their number of edges\n",
"for node_id, neighbors in adj_list.items():\n",
" if size_type == 'edges':\n",
" size = len(neighbors)*5\n",
" if size_type == 'eigen':\n",
" size = eigenvector[node_id]*200\n",
" next((node.update({'size': size}) for node in net.nodes if node['id'] == node_id), None)\n",
" if size_type == \"edges\":\n",
" size = len(neighbors) * 5\n",
" if size_type == \"eigen\":\n",
" size = eigenvector[node_id] * 200\n",
" next(\n",
" (node.update({\"size\": size}) for node in net.nodes if node[\"id\"] == node_id),\n",
" None,\n",
" )\n",
"\n",
"# set the node distance and spring lenght using repulsion\n",
"net.repulsion(node_distance=250, spring_length=150)\n",
"\n",
"# activate physics buttons to further explore the available solvers:\n",
"# barnesHut, forceAtlas2Based, repulsion, hierarchicalRepulsion\n",
"net.show_buttons(filter_=['physics'])\n",
"net.show_buttons(filter_=[\"physics\"])\n",
"\n",
"# save graph as HTML\n",
"net.save_graph('networkx_pyvis.html')\n"
"net.save_graph(\"networkx_pyvis.html\")"
]
},
{