I need to visualize graphs where each pair of nodes connected by a comma represents an edge, and the numeric value represents the intensity of that edge. For instance, ('A', 'B'): 0.71 means that node A is connected to node B with an edge intensity of 0.71. Now, I need to visualize these graphs in python. Here are the graphs data.
Graph 1
('A', 'B'): 0.71
('M', 'B'): 0.67
('N', 'B'): 0.64
('A', 'O'): 0.62
('O', 'B'): 0.60
('N', 'O'): 0.53
('M', 'O'): 0.46
('A', 'N'): 0.18
('M', 'N'): 0.11
Graph2
('ABC', 'ADC'): 0.53
Graph3
('CDE', 'CFH'): 0.28
Graph4
('GHI', 'GMI'): 0.20
Graph5
('XYZ', 'XWZ'): 0.17
Can anyone assist me visualize these graphs clearly?
I tested your code, and I encountered the same issue of overlapping nodes that I had with my own code. I have attached the output image generated by your code for your reference. I used the following command to save the file: plt.savefig('graph_image.png')
Simply use networkx
:
import matplotlib.pyplot as plt
import networkx as nx
data = {
('A', 'B'): 0.71,
('M', 'B'): 0.67,
('N', 'B'): 0.64,
('A', 'O'): 0.62,
('O', 'B'): 0.60,
('N', 'O'): 0.53,
('M', 'O'): 0.46,
('A', 'N'): 0.18,
('M', 'N'): 0.11
}
Create the graph from your dataset:
G = nx.Graph()
for key, value in data.items():
G.add_edge(*key, weight=value)
And render components as you wish:
fig, axe = plt.subplots()
pos = nx.spring_layout(G, seed=123456)
nx.draw_networkx_nodes(G, pos, ax=axe)
nx.draw_networkx_edges(G, pos, ax=axe)
nx.draw_networkx_labels(G, pos, font_size=12, ax=axe)
nx.draw_networkx_edge_labels(G, pos, nx.get_edge_attributes(G, "weight"), ax=axe)
To control edge color based on weights you can use the following snippet:
fig, axe = plt.subplots()
pos = nx.spring_layout(G, seed=123456)
nx.draw_networkx_nodes(G, pos, ax=axe)
nx.draw_networkx_edges(G, pos, ax=axe, edge_color=data.values(), edge_cmap=plt.cm.jet)
nx.draw_networkx_labels(G, pos, font_size=12, ax=axe)
nx.draw_networkx_edge_labels(G, pos, nx.get_edge_attributes(G, "weight"), ax=axe)
nx.draw_networkx_edge_labels(G, pos, nx.get_edge_attributes(G, "weight"), ax=axe)