Python | Visualize graphs generated in NetworkX using Matplotlib Last Updated : 14 Aug, 2021 Comments Improve Suggest changes Like Article Like Report Prerequisites: Generating Graph using Network X, Matplotlib IntroIn this article, we will be discussing how to plot a graph generated by NetworkX in Python using Matplotlib. NetworkX is not a graph visualizing package but basic drawing with Matplotlib is included in the software package. Step 1 : Import networkx and matplotlib.pyplot in the project file. Python3 # 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:  Python3 # 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: To add numbering in the node add one argument with_labels=True in draw() function.  Python3 # 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: Different graph types and plotting can be done using networkx drawing and matplotlib. Note** : Here keywords is referred to optional keywords that we can mention use to format the graph plotting. Some of the general graph layouts are :  draw_circular(G, keywords) : This gives circular layout of the graph G.draw_planar(G, keywords) :] This gives a planar layout of a planar networkx graph G.draw_random(G, keywords) : This gives a random layout of the graph G.draw_spectral(G, keywords) : This gives a spectral 2D layout of the graph G.draw_spring(G, keywords) : This gives a spring layout of the graph G.draw_shell(G, keywords) : This gives a shell layout of the graph G.  Example :  Python3 # 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 spectral 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 Planar Layout Random Layout Spectral Layout Spring Layout Shell Layout Reference : NetworkX Drawing Documentation Comment More infoAdvertise with us Next Article Directed Graphs, Multigraphs and Visualization in Networkx A AyushShukla8 Follow Improve Article Tags : Python python-modules Python-matplotlib Practice Tags : python Similar Reads NetworkX : Python software package for study of complex networks NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks. 3 min read Python | Visualize graphs generated in NetworkX using Matplotlib Prerequisites: Generating Graph using Network X, Matplotlib IntroIn this article, we will be discussing how to plot a graph generated by NetworkX in Python using Matplotlib. NetworkX is not a graph visualizing package but basic drawing with Matplotlib is included in the software package. Step 1 : Im 3 min read Directed Graphs, Multigraphs and Visualization in Networkx Prerequisite: Basic visualization technique for a Graph In the previous article, we have learned about the basics of Networkx module and how to create an undirected graph. Note that Networkx module easily outputs the various Graph parameters easily, as shown below with an example. Python3 1== import 10 min read Complete Graph using Networkx in Python A complete graph also called a Full Graph it is a graph that has n vertices where the degree of each vertex is n-1. In other words, each vertex is connected with every other vertex. Example: Complete Graph with 6 edges: C_G6 Properties of Complete Graph: The degree of each vertex is n-1.The total nu 3 min read Introduction to Social Networks using NetworkX in Python Prerequisite - Python Basics Ever wondered how the most popular social networking site Facebook works? How we are connected with friends using just Facebook? So, Facebook and other social networking sites work on a methodology called social networks. Social networking is used in mostly all social m 4 min read Wheel Graph using Networkx Python A wheel graph is a type of graph in which if we connect each node in an n-1 node cycle graph to nth node kept at the centre we get a wheel graph. The definition would be more clear after seeing the example below. Wheel Graph with n nodes is represented by Wn . Example: W5: W5  W6: W6 Properties of W 2 min read Small World Model - Using Python Networkx In this article, we will learn how to create a Small World Network using Networx module in Python. Before proceeding that let's first understand some basics about Small World Phenomenon. What is Small World Phenomenon ? Small World Phenomenon is the study and notion that we are all connected via a s 4 min read Ego graph Using Networkx in Python Prerequisite - Graphs, Networkx Basics Ego network is a special type of network consisting of one central node and all other nodes directly connected to it. The central node is known as ego, while the other surrounding nodes directly connected to it are known as alters. Ego networks are mostly used 4 min read Star Graph using Networkx Python In this article, we are going to see Star Graph using Networkx Python. A Star graph is a special type of graph in which n-1 vertices have degree 1 and a single vertex have degree n â 1. This looks like that n â 1 vertex is connected to a single central vertex. A star graph with total n â vertex is t 2 min read Network Centrality Measures in a Graph using Networkx | Python Centrality Measures allows us to pinpoint the most important nodes of a Graph. This essentially helps us to identify : Influential nodes in a Social Network. Nodes that disseminate information to many nodes Hubs in a transportation network Important pages in the Web Nodes that prevent the Network fr 6 min read Like