I have the following graph:
graph = {("A","A"): 3, ("A","B"): 4, ("A","C"): 1}
And I am trying to draw a graph that shows a loop in node ("A") labeled with the number 3. The loop should look like the one for node (1) here.
It has to be an indirected graph.
So far I am using this:
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
for edge in graph:
G.add_edge(edge[0], edge[1])
graph_pos=nx.shell_layout(G)
nx.draw_networkx_nodes(G,graph_pos)
nx.draw_networkx_edges(G,graph_pos)
nx.draw_networkx_labels(G, graph_pos)
nx.draw_networkx_edge_labels(G, graph_pos, edge_labels = graph)
plt.show()
The output is:
As you can see, there is no loop around node "A" even thought there is an edge ("A", "A"). Any idea on how I can make it happen?
EDIT - why not a duplicate?
The question Matplotlib and Networkx - drawing a self loop node is for directed graphs. When you create a graph using G=nx.MultiDiGraph
you can set attributes for the edges using G.graph['edge']
.
Using nx.Graph()
(indirected graph), the results for G.graph['edge']
is an empty dictionary.
The difference between that question and this question is, in essence, that I use nx.Graph
and that question uses nx.MultiDiGraph
.