Networkx not drawing arrows in directed graph

Viewed 1298

I have a 5-node directed network that I want to represent graphically using networkx. The nodes should be connected in this way:

  • Every node is connected to each other with a dashed line
  • Active connections are read from a file and are represented by solid black arrows
  • Active connections to and from the node where the code is running (in this case, node 1) should be in red.

The code does what I want, but for some reason, even though I specify arrow=True when I want to make the red edges, the arrow head does not show up. It seems to be ignoring every option I specify after edge_color, for some reason.

I noticed that when I draw the edges between all nodes, if I do not specify arrows=False it ignores the style='dashed', alpha=0.5 and just draws a gray solid line. Does that have anything to do with the issue?

Here is my code, with the problematic parts annotated with comments:

import networkx as nx
import matplotlib.pyplot as plt

class Graph(nx.DiGraph):
    def __init__(self, *args, **kwargs):
        self.nodeNum = 1
        self.pos = {'1': (0, 10),
                    '2': (10, 3),
                    '3': (6, -8),
                    '4': (-6, -8),
                    '5': (-10, 3)}
        self.allConns = []
        for i in range(5):
            for j in range(i, 5):
                self.allConns.append((str(i+1), str(j+1)))
        super().__init__(*args, **kwargs)

    def read_connections(self):
        filename = 'PATH/TO/FILE'
        with open(filename) as f:
            self.connList = []
            self.nodeConns = []
            for line in f:
                if line[-2] == '1':
                    self.connList.append((str(line[0]), str(line[1])))
                    if line[0] == self.nodeNum or line[1] == self.nodeNum:
                        self.nodeConns.append((str(line[0]), str(line[1])))
                    else:
                        pass
                else:
                    pass

    def generate_graph(self):
        self.read_connections()
        self.add_nodes_from(self.pos.keys())
        self.add_edges_from(self.connList)
        

    def draw_graph(self, fig=None, ax=None, show=False):
        if fig == None:
            fig = plt.figure()
        if ax == None:
            ax = fig.add_subplot(111)
        ax.axis('off')
        options = {'node_color': 'cyan',
                   'node_size': 1500,
                   'width': 2,
                   'arrowstyle': '-|>',
                   'arrowsize': 14,
                   'font_size': 20
                   }
        
        # This works, but if I do not specify arrows=False it draws solid lines with no transparency
        nx.draw_networkx_edges(self, self.pos, ax=ax, edgelist=self.allConns, edge_color='grey',
                               style='dashed', alpha=0.5, arrows=False)  

        # This is the main part which works. It connects the nodes from the file with black arrows
        nx.draw_networkx(self, self.pos, arrows=True, ax=ax, **options)

        # This is the part that gives me problems. It seems to ignore every option after edge_color
        nx.draw_networkx_edges(self, self.pos, ax=ax, edgelist=self.nodeConns, edge_color='r',
                               style='solid',alpha=1,
                               arrows=True, width=3, arrowsize=14, arrowstyle='-|>')
        nx.draw_networkx_nodes(self, self.pos, ax=ax, nodelist=[self.nodeNum], node_color='r',
                               node_size=1500)

        fig.tight_layout()
        if show == True:
            fig.show()

if __name__ == '__main__':
    graph = Graph()
    graph.generate_graph()
    graph.draw_graph(show=True)

And an example of the result. Note how the red edges do not have arrow heads.

Example

1 Answers

I'm quite sure arrows are drawn, but simply not visible, because you afterwards draw nodes with higher node size as default. Giving the node_size to draw_networkx_edges should solve your problem

nx.draw_networkx_edges(self, self.pos, ax=ax, 
edgelist=self.nodeConns, edge_color='r', 
style='solid',alpha=1, arrows=True, width=3, 
arrowsize=14, arrowstyle='-|>',
node_size=1500,
)

Also the documentation of draw_networkx_edges contains the following note

For directed graphs, arrows are drawn at the head end. (...) Be sure to include node_size as a keyword argument; arrows are drawn considering the size of nodes

Related