I want to plot a directional network graph using networkx in python. When using an alpha value different from 1 the beginnings of the edges are drawn also inside the nodes; the arrows are fine however.

How can I make the edges stay out of my nodes?

I didn't find anything about it in the documentation. Setting alpha=1 would obviously solve it, but that is not what I want.

import math
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

pos={"x":(1/2, math.sqrt(3/4)), "y":(0,0), "z":(1,0)}

G=nx.DiGraph()
G.add_edge("x", "y")
G.add_edge("x", "z")
nx.draw(G, pos=pos, with_labels=True, node_size=1500, alpha=0.3, arrows=True,
        arrowsize=20, width=2)
plt.title("Direct link")
plt.show()

This is what comes out. The edges continue into the "x" node, which is bad.

enter image description here

1 Answers

1
Community On Best Solutions

You can solve this by drawing the nodes multiple times:

import math
import networkx as nx
import matplotlib.pyplot as plt

pos={"x":(1/2, math.sqrt(3/4)), "y":(0,0), "z":(1,0)}

G=nx.DiGraph()
G.add_edge("x", "y")
G.add_edge("x", "z")
nx.draw_networkx_edges(G, pos=pos, with_labels=True, node_size=1500, alpha=0.3, arrows=True,
        arrowsize=20, width=2)
# draw white circles over the lines
nx.draw_networkx_nodes(G, pos=pos, with_labels=True, node_size=1500, alpha=1, arrows=True,
        arrowsize=20, width=2, node_color='w')
# draw the nodes as desired
nx.draw_networkx_nodes(G, pos=pos, node_size=1500, alpha=.3, arrows=True,
        arrowsize=20, width=2)
nx.draw_networkx_labels(G, pos=pos)
plt.title("Direct link")
plt.axis("off")
plt.show()