MaxDragonheart
MaxDragonheart

Reputation: 1300

Change graph visualization with networkx

I need a clarification with the plot. Using the code below I can see my graph:

fig, ax = plt.subplots(figsize=(10, 10))


position = nx.spring_layout(DG, seed=7)

# Add nodes
nx.draw_networkx_nodes(DG, position, node_size=250, node_color='blue')
nx.draw_networkx_labels(DG, position, font_size=10, font_family="sans-serif")

# Add edges
nx.draw_networkx_edges(DG, position, width=3, edge_color='orange', arrowstyle='->', arrowsize=25)
edge_labels = nx.get_edge_attributes(G, 'orig-id')
nx.draw_networkx_edge_labels(
    G,
    pos=position,
    edge_labels=edge_labels,
    font_size=15,

)

plt.show()

enter image description here

But I would like to see a circular graph as explaned here. So, I've changed my code:

fig, ax = plt.subplots(figsize=(10, 10))

nx.draw_circular(DG)

position = nx.spring_layout(DG, seed=7)

# Add nodes
nx.draw_networkx_nodes(DG, position, node_size=250, node_color='blue')
nx.draw_networkx_labels(DG, position, font_size=10, font_family="sans-serif")

# Add edges
nx.draw_networkx_edges(DG, position, width=3, edge_color='orange', arrowstyle='->', arrowsize=25)
edge_labels = nx.get_edge_attributes(G, 'orig-id')
nx.draw_networkx_edge_labels(
    G,
    pos=position,
    edge_labels=edge_labels,
    font_size=15,

)

plt.show()

And the result is this enter image description here

It is not clear for me how I can plot the graph with orange edges like the graph with black edges.

Upvotes: 1

Views: 249

Answers (1)

Matt Hall
Matt Hall

Reputation: 8152

The problem is that you are drawing the network twice, because nx.draw_circular() draws the entire network as a side effect. (That function can take a lot of arguments to control how edges, vertices, etc, are drawn, but you already have that code laid out so it's probably not the best approach.)

Instead, you should make a circular position dictionary using nx.circular_layout(G), then use that in the other drawing code. For example:

fig, ax = plt.subplots(figsize=(10, 10))

# Compute positions.
position = nx.circular_layout(DG, seed=7)

# Add nodes and labels.
nx.draw_networkx_nodes(DG, position, node_size=250, node_color='blue')
nx.draw_networkx_labels(DG, position, font_size=10, font_family="sans-serif")

# Add edges and labels.
nx.draw_networkx_edges(DG, position, width=3, edge_color='orange', arrowstyle='->', arrowsize=25)
edge_labels = nx.get_edge_attributes(G, 'orig-id')
nx.draw_networkx_edge_labels(G,
                             pos=position,
                             edge_labels=edge_labels,
                             font_size=15,
                             )

Upvotes: 1

Related Questions