Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display networkx graph in HTML

I am trying to display a graph I have created using networkx, but the nodes are overlapping. I want to display it in HTML so I can move nodes using the mouse (it also adds some interaction to the result).

  • Working with python3

How can I display my nx graph in HTML?

Up to now, my code is:

G= nx.Graph()
G.add_nodes_from(nodes) # nodes is a list of nodes names (strings)
G.add_edges_from(edges) # edges is a list of edges between nodes above
pos = nx.spring_layout(G, scale=layout_scale, k=k)
plt.figure(figsize=figsize)
nx.draw_networkx(G, pos=pos)
plt.show()

For now, this code results in displaying the plot as some kind of an image (in the SciView of PyCharm).

I would appreciate any help with that!

Thanks!

like image 370
Almog G Avatar asked Jul 03 '26 08:07

Almog G


1 Answers

Hi I use pyvis for interactive display. If I understand correctly, try my code, it should help you

from pyvis.network import Network
import networkx as nx

G=nx.Graph()

G.add_edge('1', '2')
G.add_edge('1', '3')

nx.draw(G, with_labels = True)
nt = Network('500px', '500px')
nt.from_nx(G)
nt.show('nx.html')
like image 169
Анатолий Avatar answered Jul 04 '26 21:07

Анатолий



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!