Reputation: 3830
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
?
Upvotes: 3
Views: 4165
Reputation: 918
Take a look at this answer. All you need to do is use the attribute color=my_font_color[node]
(where my_font_color
is a dictionary with the nodes as keys and color names as values) inside plt.text
and you can have any font color you want for your nodes. It works!
Upvotes: 0
Reputation: 2643
Another solution:
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)
colors = ['red' if node_name == 'F' else 'blue' for node_name in list(G.nodes)]
nx.draw_networkx(G, font_size=16, node_color=colors, with_labels=True)
plt.show()
And if you want to highlight certain nodes with specific names:
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)
highlight_nodes = ['A', 'F', 'B']
colors = ['red' if node_name in highlight_nodes else 'blue' for node_name in list(G.nodes)]
nx.draw_networkx(G, font_size=16, node_color=colors, with_labels=True)
plt.show()
Also, you can replace list(G.nodes) with one of the following options: G, G.nodes, G.nodes(). All of them will give the same result. The sample output from second code snippet:
Upvotes: 2
Reputation: 9482
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')
Upvotes: 7