- 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 do I customize the display of edge labels using networkx in Matplotlib?
To set the networkx edge labels offset, 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.
- Position the nodes using Fruchterman-Reingold force-directed algorithm.
- Draw the graph G with Matplotlib.
- Draw edge labels.
- To display the figure, 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, 3), (3, 4), (4, 1), (1, 3)]) pos = nx.spring_layout(G) for u, v, d in G.edges(data=True): d['weight'] = 3 edges, weights = zip(*nx.get_edge_attributes(G, 'weight').items()) nx.draw(G, pos, node_color='b', edge_color=weights, width=2, with_labels=True) nx.draw_networkx_edge_labels(G, pos, { (1, 2): "x", (2, 3): "y", (3, 4): "w", (4, 1): "z", (1, 3): "v" }, label_pos=0.75 ) plt.show()
Output
- Related Articles
- How to set NetworkX edge labels offset in Matplotlib?
- How to show node name in Matplotlib graphs using networkx?
- How to make multipartite graphs using networkx and Matplotlib?
- How can one modify the outline color of a node in networkx using Matplotlib?
- How can I make the xtick labels of a plot be simple drawings using Matplotlib?
- How can I draw inline line labels in Matplotlib?
- How to customize the axis label in a Seaborn jointplot using Matplotlib?
- How to customize the X-axis in Matplotlib?
- How to customize spines of Matplotlib figures?
- How to draw node colormap in NetworkX/Matplotlib?
- How can I display an np.array with pylab.imshow() using Matplotlib?
- How to change the attributes of a networkx / matplotlib graph drawing?
- How can I rotate xtick labels through 90 degrees in Matplotlib?
- Colouring the edges by weight in networkx (Matplotlib)
- How to customize X-axis ticks in Matplotlib?

Advertisements