How to permanetly display the label of an edge
I am aware that it is possible to hover over an edge to display its label following these steps. However, I would like said label to be displayed at all times. I read on an issue that one could include the label keyword when adding an edge to a graph, but it isn't working for me. I would like the MWE code below to show '5' on the indicated edge at all times. Thank you for any help.
from pyvis.network import Network
import networkx as nx
nx_graph = nx.cycle_graph(10)
nx_graph.nodes[1]['title'] = 'Number 1'
nx_graph.nodes[1]['group'] = 1
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
nx_graph.nodes[3]['group'] = 10
nx_graph.add_node(20, size=20, title='couple', label='2', group=2)
nx_graph.add_node(21, size=15, title='couple', group=2)
nx_graph.add_edge(20, 21, weight=5, title='7', label='5') ## How to display the label for this edge ?
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)
nt = Network("500px", "500px", notebook=True)
nt.from_nx(nx_graph)
# nt.show_buttons(filter_=['physics'])
nt.show_buttons()
nt.show("nx.html")
This code should work correctly. There was no problem in version 0.1.9, but in the new version, there is a problem. For an unknown and wrong reason, the tag of the previous version has been removed. Therefore, it is not possible to observe the changes and fix it very fast.
okay, the origin of the problem is this PR: https://github.com/WestHealth/pyvis/pull/126 to fix it you need to change the code like this:
from pyvis.network import Network
import pyvis._version
import networkx as nx
nx_graph = nx.cycle_graph(10)
nx_graph.nodes[1]['title'] = 'Number 1'
nx_graph.nodes[1]['group'] = 1
nx_graph.nodes[3]['title'] = 'I belong to a different group!'
nx_graph.nodes[3]['group'] = 10
nx_graph.add_node(20, size=20, title='couple', label='2', group=2)
nx_graph.add_node(21, size=15, title='couple', group=2)
nx_graph.add_edge(20, 21, weight=5, title='7', label='5') ## How to display the label for this edge ?
nx_graph.add_node(25, size=25, label='lonely', title='lonely node', group=3)
nt = Network("500px", "500px", notebook=True)
if pyvis._version.__version__ > '0.1.9':
nt.from_nx(nx_graph, show_edge_weights=False)
else:
nt.from_nx(nx_graph)
# nt.show_buttons(filter_=['physics'])
nt.show_buttons()
nt.show("nx.html")
FYI, for anyone coming across this in the future. Make sure your edge labels are type string. Float do not appear on the graph.
Thank you. This solves my issue
If your graph is constructed from adjacency matrix, i.e all weights are known, here's a quick way to convert it to labels that are shown by pyvis
for edge, data in nx_graph.edges.items():
data["label"] = str(data["weight"])