I have a graph in networkx which I want to plot. Everything works fine, but I decided it would be useful to color specific nodes. How do I do this?
I have seen, that this can be done by using the command
draw_networkx_nodes(G, pos, nodelist=None, node_size=300, node_color='r', node_shape='o', alpha=1.0, cmap=None, vmin=None, vmax=None, ax=None, linewidths=None, label=None, **kwds)
by setting the color and cmap keyword to the right value. But all examples I have seen so far use cmap=plt.get_cmap(some string)
, which is a standard function which does not seem helpful to me.
To clarify: I have several list of nodes and I want to color all nodes in a list in the same color
You can draw your nodes in groups based on color similar to the example they provide. With each group that you draw, just specify node_color
to the color that you want.
import networkx as nx
G = nx.cubical_graph()
pos = nx.spring_layout(G)
nodes = {
'r': [1, 3, 5],
'b': [0, 2],
'g': [4]
}
for node_color, nodelist in nodes.items():
nx.draw_networkx_nodes(G, pos, nodelist=nodelist, node_color=node_color)
labels = {x: x for x in G.nodes}
nx.draw_networkx_labels(G, pos, labels, font_size=16, font_color='w')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.