Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network graph not showing arrows along edge in Python

I have an adjacency matrix A and an array defining the coordinates of each node:

import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
%matplotlib inline  

Import adjacency matrix A[i,j]
A = np.matrix([[0, 1, 1, 0, 0, 1, 0],
               [0, 0, 1, 1, 0, 0, 0],
               [0, 0, 0, 1, 1, 1, 0],
               [0, 0, 0, 0, 1, 1, 0],
               [0, 0, 0, 0, 0, 0, 1], 
               [0, 0, 0, 0, 0, 0, 1],
               [0, 0, 0, 0, 0, 0, 0]])

## Import node coordinates
xy = np.array([[0, 0],
               [-20, 20],
               [17, 27], 
               [-6, 49], 
               [15, 65], 
               [-20, 76],
               [5,  100]]) 

My goal is to draw the graph displaying how the nodes are connect between one another. Therefore each edge should have an arrow or bidirectional arrow showing what direction to proceed along it.

I was able to display the connectivity but there are no arrows even though I specified the parameter as True.

## Draw newtwork
G = nx.from_numpy_matrix(A, xy)
nx.draw_networkx(G, pos=xy, width=3, arrows=True)

Can you please suggest me a way to achieve my goal without modifying the input data (A and xy)?

enter image description here

like image 844
Federico Gentile Avatar asked Mar 07 '26 04:03

Federico Gentile


1 Answers

At some point, I got very annoyed at the lack of proper arrow support in the networkx drawing facilities and wrote my own, while keeping the API pretty much the same. Code can be found here.

enter image description here

import numpy as np
import netgraph

A = np.matrix([[0, 1, 1, 0, 0, 1, 0],
              [0, 0, 1, 1, 0, 0, 0],
              [0, 0, 0, 1, 1, 1, 0],
              [0, 0, 0, 0, 1, 1, 0],
              [0, 0, 0, 0, 0, 0, 1],
              [0, 0, 0, 0, 0, 0, 1],
              [0, 0, 0, 0, 0, 0, 0]])

xy = np.array([[0, 0],
               [-20, 20],
               [17, 27],
               [-6, 49],
               [15, 65],
               [-20, 76],
               [5,  100]])

N = len(A)
node_labels = dict(zip(range(N), range(N)))
netgraph.draw(np.array(A), xy / np.float(np.max(xy)), node_labels=node_labels)
like image 189
Paul Brodersen Avatar answered Mar 09 '26 18:03

Paul Brodersen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!