- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Colouring the edges by weight in networkx (Matplotlib)
63 Lectures 6 hours
To color the edges by weight in 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 nodes to the current graph.
- Add edges to the current graph's nodes.
- Iterate the given graph's edges and set some weight to them.
- Draw current graphs with weights for edge color.
- To display the figure, use show() method.
import random as rd 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)]) for u, v, d in G.edges(data=True): d['weight'] = rd.random() edges, weights = zip(*nx.get_edge_attributes(G, 'weight').items()) nx.draw(G, node_color='b', edge_color=weights, width=2, with_labels=True) plt.show()
- Creating curved edges with NetworkX in Python3 (Matplotlib)
- How to draw node colormap in NetworkX/Matplotlib?
- Drawing a network graph with networkX and Matplotlib
- Matplotlib – Drawing lattices and graphs with Networkx
- How to set NetworkX edge labels offset in Matplotlib?
- How to change the attributes of a networkx / matplotlib graph drawing?
- How to make multipartite graphs using networkx and Matplotlib?
- How to show node name in Matplotlib graphs using networkx?
- How do I customize the display of edge labels using networkx in Matplotlib?
- Bold font weight for LaTeX axes label in Matplotlib
- How can one modify the outline color of a node in networkx using Matplotlib?
- How to plot the outline of the outer edges on a Matplotlib line in Python?
- How to use an update function to animate a NetworkX graph in Matplotlib?
- Font Weight in CSS
- Set the font weight with CSS