Skip to content
Related Articles

Related Articles

Improve Article
Python | Visualize graphs generated in NetworkX using Matplotlib
  • Last Updated : 24 Jun, 2019

Prerequisites : Generating Graph using Network X, Matplotlib Intro

In this article, we will be discussing how to plot a graph generated by NetworkX in Python using Matplotlib. NetworkX is not a graph visualising package but basic drawing with Matplotlib is included in the software package.

Step 1 : Import networkx and matplotlib.pyplot in the project file.




# importing networkx 
import networkx as nx
  
# importing matplotlib.pyplot
import matplotlib.pyplot as plt

Step 2 : Generate a graph using networkx.
Step 3 : Now use draw() function of networkx.drawing to draw the graph.
Step 4 : Use savefig("filename.png") function of matplotlib.pyplot to save the drawing of graph in filename.png file.

Below is the Python code:






# importing networkx 
import networkx as nx
# importing matplotlib.pyplot
import matplotlib.pyplot as plt
  
g = nx.Graph()
  
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 4)
g.add_edge(1, 4)
g.add_edge(1, 5)
  
nx.draw(g)
plt.savefig("filename.png")

Output:
Graph with no label

To add numbering in the node add one argument with_labels=True in draw() function.




# importing networkx 
import networkx as nx
# importing matplotlib.pyplot
import matplotlib.pyplot as plt
  
g = nx.Graph()
  
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 4)
g.add_edge(1, 4)
g.add_edge(1, 5)
  
nx.draw(g, with_labels = True)
plt.savefig("filename.png")

Output:
Graph with Labels

Different graph types and plotting can be done using networkx drawing and matplotlib.
Note** : Here keywrds is referred to optional keywords that we can mention use to format the graph plotting. Some of the general graph layouts are :

  1. draw_circular(G, keywrds) : This gives cicular layout of the graph G.
  2. draw_planar(G, keywrds) :] This gives a planar layout of a planar networkx graph G.
  3. draw_random(G, keywrds) : This gives a random layout of the graph G.
  4. draw_spectral(G, keywrds) : This gives a spectral 2D layout of the graph G.
  5. draw_spring(G, keywrds) : This gives a spring layout of the graph G.
  6. draw_shell(G, keywrds) : This gives a shell layout of the graph G.

Example :




# importing networkx
import networkx as nx
# importing matplotlib.pyplot
import matplotlib.pyplot as plt
  
g = nx.Graph()
  
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 4)
g.add_edge(1, 4)
g.add_edge(1, 5)
g.add_edge(5, 6)
g.add_edge(5, 7)
g.add_edge(4, 8)
g.add_edge(3, 8)
  
# drawing in circular layout
nx.draw_circular(g, with_labels = True)
plt.savefig("filename1.png")
  
# clearing the current plot
plt.clf()
  
# drawing in planar layout
nx.draw_planar(g, with_labels = True)
plt.savefig("filename2.png")
  
# clearing the current plot
plt.clf()
  
# drawing in random layout
nx.draw_random(g, with_labels = True)
plt.savefig("filename3.png")
  
# clearing the current plot
plt.clf()
  
# drawing in specrtal layout
nx.draw_spectral(g, with_labels = True)
plt.savefig("filename4.png")
  
# clearing the current plot
plt.clf()
  
# drawing in spring layout
nx.draw_spring(g, with_labels = True)
plt.savefig("filename5.png")
  
# clearing the current plot
plt.clf()
  
# drawing in shell layout
nx.draw_shell(g, with_labels = True)
plt.savefig("filename6.png")
  
# clearing the current plot
plt.clf()

Outputs :

Circular Layout
Circular layout
Planar Layout
Planar Layout
Random Layout
Random layout
Spectral Layout
Spectral layout
Spring Layout
Spring layout
Shell Layout
Shell layout

Reference : NetworkX Drawing Documentation

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :