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.
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
Below is the Python code:
To add numbering in the node add one argument with_labels=True in draw() function.
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 :
- draw_circular(G, keywrds) : This gives cicular layout of the graph G.
- draw_planar(G, keywrds) :] This gives a planar layout of a planar networkx graph G.
- draw_random(G, keywrds) : This gives a random layout of the graph G.
- draw_spectral(G, keywrds) : This gives a spectral 2D layout of the graph G.
- draw_spring(G, keywrds) : This gives a spring layout of the graph G.
- draw_shell(G, keywrds) : This gives a shell layout of the graph G.
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.
- Operations on Graph and Special Graphs using Networkx module | Python
- Visualize Graphs in Python
- Directed Graphs, Multigraphs and Visualization in Networkx
- How To Visualize Sparse Matrix in Python using Matplotlib?
- Python | Visualize missing values (NaN) values using Missingno Library
- Python | Clustering, Connectivity and other Graph properties using Networkx
- Network Centrality Measures in a Graph using Networkx | Python
- Small World Model - Using Python Networkx
- Introduction to Social Networks using NetworkX in Python
- Link Prediction - Predict edges in a network using Networkx
- NetworkX : Python software package for study of complex networks
- Plotting Various Sounds on Graphs using Python and Matplotlib
- Plot Live Graphs using Python Dash and Plotly
- Plotting graphs using Python's plotly and cufflinks module
- Python Bokeh - Plotting Horizontal Bar Graphs
- Python Bokeh - Plotting Vertical Bar Graphs
- Plot multiple separate graphs for same data from one Python script
- Generating random strings until a given string is generated
- Styling Graphs in Pygal
- Violinplot in Python using axes class of Matplotlib
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.