{"id":30961,"date":"2022-06-22T04:48:00","date_gmt":"2022-06-22T04:48:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=30961"},"modified":"2022-07-06T10:43:47","modified_gmt":"2022-07-06T10:43:47","slug":"networkx-interactive-network-graphs","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/networkx-interactive-network-graphs","title":{"rendered":"Create Interactive Network Graphs in Python"},"content":{"rendered":"\n<p>I am sure you have built network graphs in python before using a special library known as Networkx. Have you ever wondered if there was a way to interact with graphs? Guess what?! There is a library named Pyvis which helps to improve the interactivity of network graphs in Python programming language.<\/p>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/www.askpython.com\/python-modules\/networkx-package\">NetworkX Package \u2013 Python Graph Library<\/a><\/em><\/strong><\/p>\n\n\n\n<p>The Pyvis library enables visualization and adds interactivity to network graphs. The library is built on top of the powerful and mature library known as VisJS JavaScript. This allows fast and responsive interactions and extracts the network graphs in the form of low-level JavaScript and HTML.<\/p>\n\n\n\n<p>Installing the Pyvis library is simple and straightforward that can be done using the pip command in the command prompt of the system using the command below.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Code Implementation<\/h2>\n\n\n\n<p>Let\u2019s now move on to the code implementation of the interactive network graphs using the Pyvis library in Python programming language. We will start off by importing all the necessary libraries\/modules using the code snippet below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nfrom pyvis import network as net\nfrom IPython.core.display import display, HTML\nimport random\n<\/pre><\/div>\n\n\n<p>We will start off by creating a network graph with only nodes and no edges between them. The creation of an empty graph can be done using the Network function which specifies the properties of the network graph inside it including the background color, heading, height, and width.<\/p>\n\n\n\n<p>Next, we will make use of the <code>add_node<\/code> function to add nodes to the network graph. We will be adding 10 nodes (from 1 to 10) and then convert the network graph into HTML format to add interactivity and save the HTML file as well.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ng_only_nodes =  net.Network(height=&#039;600px&#039;,width=&#039;90%&#039;,\n                  bgcolor=&#039;white&#039;,font_color=&quot;red&quot;,\n                  heading=&quot;Networkx Graph with only Nodes&quot;)\n\nfor i in range(1,11):  \n  g_only_nodes.add_node(i)\n\ng_only_nodes.show(&#039;Only_Nodes.html&#039;)\ndisplay(HTML(&#039;Only_Nodes.html&#039;))\n<\/pre><\/div>\n\n\n<p>Have a look at what the network graph with only nodes looks like.<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"842\" style=\"aspect-ratio: 1048 \/ 842;\" width=\"1048\" controls src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Only-Nodes-Networkx-Pyviz.mp4\"><\/video><\/figure>\n\n\n\n<p>The next step in the creation of network graphs is adding edges between the nodes. We will be adding random edges between random nodes. Have a look at the function for the same below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ndef generate_edge():\n  s = random.randint(1,10)\n  d = random.randint(1,10)\n  return (s,d)\n<\/pre><\/div>\n\n\n<p>In the function, we will be generating random source and destination nodes pair using the <code>random.randint<\/code> function. We will be getting random nodes between 1 and 10. To make sure we have enough edges; we will be generating 20 random edges. To make sure that the same edge is not repeated again and again, we will be keeping a record of the pairs of (source, destination) nodes. Have a look at the code below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\ng =  net.Network(height=&#039;600px&#039;,width=&#039;90%&#039;,\n                  bgcolor=&#039;white&#039;,font_color=&quot;red&quot;,\n                  heading=&quot;A Simple Networkx Graph&quot;)\n\nfor i in range(1,11):  \n  g.add_node(i)\n\ni=0\nchosen_set = &#x5B;]\nwhile(i!=20):\n  eg = generate_edge()\n  if(eg&#x5B;0]!=eg&#x5B;1] and not (eg in chosen_set)):\n      chosen_set.append(eg)\n      g.add_edge(eg&#x5B;0],eg&#x5B;1])\n      i+=1\n\ng.show(&#039;Simple_Network_Graph.html&#039;)\ndisplay(HTML(&#039;Simple_Network_Graph.html&#039;))\n<\/pre><\/div>\n\n\n<p>After the addition of edges, we will have a network graph that looks something like the one below. Look how amazing the network graph turns out to be and how interactive the graph is!<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"842\" style=\"aspect-ratio: 1048 \/ 842;\" width=\"1048\" controls src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Simple-Network-graph-Pyviz.mp4\"><\/video><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Pyvis is a powerful python module for visualizing and interactively manipulating network graphs using Python programming language. I hope you were able to build the network graphs using the library and enjoyed interacting with the graphs.<\/p>\n\n\n\n<p>Thank you for reading!<\/p>\n\n\n\n<p>Happy coding! &#x1f603;<\/p>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/network-analysis-in-python\">Network Analysis in Python \u2013 A Complete Guide<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n","protected":false},"excerpt":{"rendered":"<p>I am sure you have built network graphs in python before using a special library known as Networkx. Have you ever wondered if there was a way to interact with graphs? Guess what?! There is a library named Pyvis which helps to improve the interactivity of network graphs in Python programming language. Also Read: NetworkX [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":30984,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-30961","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/30961","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=30961"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/30961\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/30984"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=30961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=30961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=30961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}