Is there a way to draw networkX graphs with colored nodes (optionally) surrounded by a differently colored ring?

Question:

I would like to draw geometric graphs that have colored nodes that can additionally have colored rings around each node.

If possible I would even like to make the surrounding color rings optional for each node.

How is it possible to (optionally) add a colored ring around different nodes in a graph?

To draw RGGs I rely on this with small modifications. There you can also add colors to the node itself. The implementation uses matplotlib to do the task.

Asked By: baxbear

||

Answers:

I have a simple workaround for this:

Draw the nodes twice and then manipulate the zorder of the PathCollection such that the smaller nodes are in front:

import matplotlib.pyplot as plt
import networkx as nx

G = nx.gnp_random_graph(10, 0.5)
pos = nx.spring_layout(G)

plt.figure(figsize=(8, 8))

node_size = 50
ring_size = 100 + node_size

edges = nx.draw_networkx_edges(G, pos, alpha=0.4)
nodes = nx.draw_networkx_nodes(
    G,
    pos,
    node_size=node_size,
    node_color='red',
)

rings = nx.draw_networkx_nodes(
    G,
    pos,
    node_size=ring_size,
    node_color='green',
)

nodes.set_zorder(2)
rings.set_zorder(1)

plt.axis("off")
plt.show()

enter image description here

Caveat, if you want to change the alpha value of the nodes this does not work very well.

Answered By: math_noob
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.