Issue
I am using NetworkX to visualise a large graph with > 1000 nodes. As part of that visualisation, I wanted to be able to highlight certain nodes.
I have seen this question and am aware that NetworkX will allow you to highlight a node by changing the node colour like this:
import networkx as nx
import matplotlib.pyplot as plt
edges = [['A','B'], ['A','C'], ['A','D'], ['B','E'], ['B','F'], ['D','G'],['D','H'],['F','I'],['G','J'],['A','K']]
G = nx.Graph()
G.add_edges_from(edges)
colours = ['blue']*5 + ['red'] + ['blue']*5
nx.draw_networkx(G, font_size=16, node_color=colours)
plt.show()
But, with a large number of nodes, I have had to reduce the node size significantly, otherwise it just appears as a blurry cloud of overlapping nodes, so changing the node colour isn't effective.
What I would ideally like to do is change the label font colour to, say, red for the text label of chosen nodes.Unlike node_color
, however, NetworkX only seems to offer a global argument font_color
to change the colour for all node labels and it won't accept, e.g. font_color=colours
.
If there any way through NetworkX or Matplotlib to either change the font colour for a specific node/group of nodes, or to add any sort of call-out, or any other way to highlight certain nodes without relying on changing node_color
?
Solution
You can draw a subgraph on top of your original graph:
# generate node positions:
pos = nx.spring_layout(G)
# draw graph
nx.draw_networkx(G, pos=pos, font_size=16, node_color='blue', font_color='white')
# draw subgraph for highlights
nx.draw_networkx(G.subgraph('F'), pos=pos, font_size=16, node_color='red', font_color='green')
Answered By - warped
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.