-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Edge width from networkx #82
Comments
For the moment, I hard coded weight into value. The following can be inserted into the def nx2pyvis(nx_graph, pyvisnet, default_node_size=1, default_edge_weight=1):
"""
Converts a NetworkX graph into a Pyvis Network
This replaces the Network.from_nx method, which fails
at plotting correctly edge width, as indicated in
https://github.com/WestHealth/pyvis/issues/82
"""
assert(isinstance(nx_graph, nx.Graph))
edges = nx_graph.edges(data=True)
nodes = nx_graph.nodes(data=True)
if len(edges) > 0:
for e in edges:
if 'size' not in nodes[e[0]].keys():
nodes[e[0]]['size'] = default_node_size
nodes[e[0]]['size'] = int(nodes[e[0]]['size'])
if 'size' not in nodes[e[1]].keys():
nodes[e[1]]['size'] = default_node_size
nodes[e[1]]['size'] = int(nodes[e[1]]['size'])
pyvisnet.add_node(e[0], **nodes[e[0]])
pyvisnet.add_node(e[1], **nodes[e[1]])
if 'weight' not in e[2].keys():
e[2]['weight'] = default_edge_weight
e[2]['weight'] = e[2]['weight']
edge_dict = e[2]
edge_dict["value"] = edge_dict.pop("weight")
pyvisnet.add_edge(e[0], e[1], **edge_dict)
for node in nx.isolates(nx_graph):
if 'size' not in nodes[node].keys():
nodes[node]['size']=default_node_size
pyvisnet.add_node(node, **nodes[node]) If you want me to, I can create a PR to address this. |
You're right in that |
@VolodyaCO Can you submit a pull request for this? |
Is there also a straightforward way to toggle wether the numerical weights of the edges are shown when imported from Networkx? |
Nvm you can show the numerical weights of the edges by adding |
@jhunpingco isn't this already handled by @boludo00 commit 4c8071f? |
Thanks. This worked for me when I replaced the |
I'm trying to plot a networkx graph using pyvis version 0.1.8.2.
However, the
weight
edge attribute is taken from networkx to pyvis, but it does not have any relevance in the pyvis visualisation. The idea is that the width of each edge corresponds to theweight
of networkx. Thefrom_nx
method assigns theweight
attribute from networkx to aweight
attribute in the pyvis Network. But the visualisation fails to plot the width of the edges using the information of theweight
attribute:Instead, if I use
value
to indicate this weight, the plot is done correctly:Since
from_nx
is written to acceptweight
as the canonical connection weight, when drawing the graph, it should plot the width of the edges using the information ofweight
instead ofvalue
. The other option is to not mess with jinja'sTemplate
, and hand-codefrom_nx
to transform the nameweight
intovalue
.The text was updated successfully, but these errors were encountered: