Reputation: 22366
Please advise how to show the edge labels in Graphviz.
Trying to show the edge labels in the Graphviz generated from NetworkX.
In NetworkX, it shows the edge labels.
import matplotlib.pyplot as plt
import networkx as nx
MDG = nx.MultiDiGraph()
# --------------------------------------------------------------------------------
# Forward edges
# --------------------------------------------------------------------------------
forward_edges = [
("sushi/0", "goes/1"),
("sushi/0", "with/2"),
("sushi/0", "wasabi/3"),
("goes/1", "with/2"),
("goes/1", "wasabi/3"),
("with/2", "wasabi/3"),
]
MDG.add_edges_from(
forward_edges,
edge_color='b',
weight='length',
length=100,
)
# layout definition must come after adding all nodes/edges.
# Otherwise Node X has no position error.
pos=nx.spring_layout(MDG,seed=5, weight='length')
fig, ax = plt.subplots(figsize=(10, 5))
# --------------------------------------------------------------------------------
# Draw nodes & labels
# --------------------------------------------------------------------------------
nx.draw_networkx_nodes(
MDG,
pos,
ax=ax,
node_size=500,
node_color="cyan",
)
nx.draw_networkx_labels(
MDG,
pos,
ax=ax,
font_weight='bold',
font_size=12,
)
# --------------------------------------------------------------------------------
# Draw forward edges
# --------------------------------------------------------------------------------
nx.draw_networkx_edges(
MDG,
pos,
ax=ax,
edgelist=forward_edges,
edge_color="b",
arrowsize=20,
arrowstyle="->",
connectionstyle='arc3, rad=0.1',
)
nx.draw_networkx_edge_labels(
MDG,
pos,
label_pos=0.2,
edge_labels={
("sushi/0", "goes/1"): 0.3,
("sushi/0", "with/2"): 0.1,
("sushi/0", "wasabi/3"): 0.6,
("goes/1", "with/2"): 0.75,
("goes/1", "wasabi/3"): 0.75,
("with/2", "wasabi/3"): 0.75,
},
font_color='b'
)
nx.set_edge_attributes(
G=MDG,
values={
("sushi/0", "goes/1", 0.1): {"label": 0.1},
("sushi/0", "with/2", 0.1): {"label": 0.2},
("sushi/0", "wasabi/3", 0.6): {"label": 0.6},
("goes/1", "with/2", 0.1): {"label": 0.1},
("goes/1", "wasabi/3", 0.75): {"label": 0.75},
("with/2", "wasabi/3", 0.75): {"label": 0.75},
}
)
However, the Graphviz does not show them.
D = nx.drawing.nx_agraph.to_agraph(MDG)
# Modify node fillcolor and edge color.
D.node_attr.update(color='blue', style='filled', fillcolor='yellow')
D.edge_attr.update(color='blue', arrowsize=1)
D.layout('dot')
D.draw('MDG.png')
Upvotes: 1
Views: 401
Reputation: 1165
You are not passing the label values properly to the edges:
nx.set_edge_attributes(
MDG,
values={
("sushi/0", "goes/1",0): 0.1,
("sushi/0", "with/2",0):0.2,
("sushi/0", "wasabi/3",0) :0.6,
("goes/1", "with/2",0): 0.1,
("goes/1", "wasabi/3",0):0.75,
("with/2", "wasabi/3",0):0.75,
},
name = "label"
)
You need to pass the values as a dict, with the key being (start_node, end_node, edge_key)
. Since you are using a MultiDiGraph, each edge has a key to make it distinguishable from other edges with the same start and end nodes.
You can check the edge keys:
for e in MDG.edges(keys=True):
print(e)
Since in your case, you do not have multiple edges per node pair the keys all equal to zero.
Generating the pygrphviz yields:
Upvotes: 1