Skip to content

Instantly share code, notes, and snippets.

@quadrismegistus
Last active July 20, 2023 12:59
Show Gist options
  • Save quadrismegistus/92a7fba479fc1e7d2661909d19d4ae7e to your computer and use it in GitHub Desktop.
Save quadrismegistus/92a7fba479fc1e7d2661909d19d4ae7e to your computer and use it in GitHub Desktop.
Draw Networkx Graph with Pyvis
def draw_graph3(networkx_graph,notebook=True,output_filename='graph.html',show_buttons=True,only_physics_buttons=False):
"""
This function accepts a networkx graph object,
converts it to a pyvis network object preserving its node and edge attributes,
and both returns and saves a dynamic network visualization.
Valid node attributes include:
"size", "value", "title", "x", "y", "label", "color".
(For more info: https://pyvis.readthedocs.io/en/latest/documentation.html#pyvis.network.Network.add_node)
Valid edge attributes include:
"arrowStrikethrough", "hidden", "physics", "title", "value", "width"
(For more info: https://pyvis.readthedocs.io/en/latest/documentation.html#pyvis.network.Network.add_edge)
Args:
networkx_graph: The graph to convert and display
notebook: Display in Jupyter?
output_filename: Where to save the converted network
show_buttons: Show buttons in saved version of network?
only_physics_buttons: Show only buttons controlling physics of network?
"""
# import
from pyvis import network as net
# make a pyvis network
pyvis_graph = net.Network(notebook=notebook)
# for each node and its attributes in the networkx graph
for node,node_attrs in networkx_graph.nodes(data=True):
pyvis_graph.add_node(str(node),**node_attrs)
# for each edge and its attributes in the networkx graph
for source,target,edge_attrs in networkx_graph.edges(data=True):
# if value/width not specified directly, and weight is specified, set 'value' to 'weight'
if not 'value' in edge_attrs and not 'width' in edge_attrs and 'weight' in edge_attrs:
# place at key 'value' the weight of the edge
edge_attrs['value']=edge_attrs['weight']
# add the edge
pyvis_graph.add_edge(str(source),str(target),**edge_attrs)
# turn buttons on
if show_buttons:
if only_physics_buttons:
pyvis_graph.show_buttons(filter_=['physics'])
else:
pyvis_graph.show_buttons()
# return and also save
return pyvis_graph.show(output_filename)
##
# For example:
##
# make a new neworkx network
import networkx as nx
G=nx.Graph()
# add nodes and edges (color can be html color name or hex code)
G.add_node('a',color='red',size=4)
G.add_node('b',color='#30a1a5',size=3)
G.add_node('c',color='green',size=1)
G.add_edge('a','b',weight=1023)
G.add_edge('a','c',weight=435)
G.add_edge('b','c',weight=100)
# draw
draw_graph3(G)
@quadrismegistus
Copy link
Author

Updated! Thanks @DomHudson!

@DeepaMahm
Copy link

Hi @quadrismegistus

May I know if there is a way to get this working when "x", "y", and "z" are present?

@Cybernetic1
Copy link

Seems the new version of pyVis doesn't even need this. You just call their function after you've built the network G:

from pyvis.network import Network
vis = Network('500px', '500px')
vis.from_nx(G)
vis.show('pyVis.html')

It's all automated :)

@da115115
Copy link

Seems the new version of pyVis doesn't even need this. You just call their function after you've built the network G:

from pyvis.network import Network
vis = Network('500px', '500px')
vis.from_nx(G)
vis.show('pyVis.html')

It's all automated :)

Yes, thanks!

Here is the documentation: https://pyvis.readthedocs.io/en/latest/tutorial.html#networkx-integration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment