Federico Gentile
Federico Gentile

Reputation: 5940

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

Upvotes: 2

Views: 4667

Answers (4)

King Alawaka
King Alawaka

Reputation: 529

you can use nx.is_directed(G) to check whether your graph is directed or not. If it's not use the following code to initialize a directed graph.

 G=nx.DiGraph()

Upvotes: 0

baarath srinivasan
baarath srinivasan

Reputation: 73

I was facing similar issue where i was not getting arrow mark even after using draw_networkx with arrow parameter as True.

Found it was because of graph created using pandas was not digraph ( Arrow would be shown only for digraph)

You can check if the graph is digraph or not using this below function

nx.is_directed(G)

And you can follow below snippet to create digraph from pandas

G = nx.from_pandas_edgelist(df, "source", "target", create_using=nx.DiGraph())

Upvotes: 1

smundlay
smundlay

Reputation: 155

I managed to get "arrows". From digging around on other stack overflow questions (here, and here) it seems like this is the best way to get arrows using matplotlib.

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


#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]]) 

G = nx.from_numpy_matrix(np.array(A), create_using = nx.MultiDiGraph())
pos = xy
nx.draw(G,pos)
labels = {i: i + 1 for i in G.nodes()}
nx.draw_networkx_labels(G, pos, labels, font_size=15, arrows=True)
plt.show()

output

Upvotes: 3

Paul Brodersen
Paul Brodersen

Reputation: 13031

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)

Upvotes: 5

Related Questions