alosc
alosc

Reputation: 63

Using NetworkX to show a digraph on Jupyter Notebook

I'm trying to draw a randomly generated tournament in python using the networkx package, but jupyter notebook doesn't display anything upon running, my graph is properly loaded, since i can call number of edges and such, just the plotting part isn't working. I searched here for an answer, and closest thing was to add plt.show(), after calling the nx.draw(G) function, but this didn't solve my issue.

Any help is welcome!

My code for reference:

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


SIZE=10

T = nx.DiGraph() 
A = np.zeros((SIZE,SIZE))

# GENERATING AN ADJACENCY MATRIX FOR A RANDOM TOURNAMENT

for i in range(SIZE):
    for j in range(i):
        if i==j:
            pass
        else:
            if (np.random.binomial(n=1,p=0.5,size=1)==1)[0]:
                A[i,j]=1
            else:
                A[j,i]=1
            
# ADDING EDGES TO THE GRAPH
                
for i in range(SIZE): 
    for j in range(SIZE): 
        if A[i][j] == 1: 
            T.add_edge(i,j) 
            


# TRYING TO PLOT THE GRAPH

nx.draw(T)
plt.show()

Upvotes: 0

Views: 5446

Answers (1)

sedavidw
sedavidw

Reputation: 11691

You need to add the following to beginning of your jupyter notebook

%matplotlib inline

This will cause plots created to be displayed in the notebook itself

Upvotes: 2

Related Questions