- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to show node name in Matplotlib graphs using networkx?
To show node name in graphs using networkx, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Initialize a graph with edges, name, or graph attributes.
- Add multiple nodes using add_nodes_from() method.
- Add all the edges using add_edge_from() method.
- Draw the graph G with Matplotlib using draw() method. Set with_labels to True.
- To display the graph, we can use show() method.
Example
import matplotlib.pylab as plt import networkx as nx plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True G = nx.DiGraph() G.add_nodes_from([1, 2, 3, 4]) G.add_edges_from([(1, 2), (2, 1), (2, 3), (1,4), (3,4)]) nx.draw(G, with_labels=True) plt.show()
Output
- Related Articles
- How to make multipartite graphs using networkx and Matplotlib?
- How to draw node colormap in NetworkX/Matplotlib?
- Matplotlib – Drawing lattices and graphs with Networkx
- How can one modify the outline color of a node in networkx using Matplotlib?
- Show Matplotlib graphs to image as fullscreen
- How to plot 3D graphs using Python Matplotlib?
- Adding textures to graphs using Matplotlib
- How to set NetworkX edge labels offset in Matplotlib?
- Displaying bar graphs using Matplotlib
- How to plot multiple graphs in Matplotlib?
- How do I customize the display of edge labels using networkx in Matplotlib?
- Displaying horizontal bar graphs using Matplotlib
- How to show two figures using Matplotlib?
- How to use an update function to animate a NetworkX graph in Matplotlib?
- How to create broken horizontal bar graphs in matplotlib?

Advertisements