Question
Please advise how to show the edge labels in Graphviz.
Background
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')


