- 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 make multipartite graphs using networkx and Matplotlib?
To make multipartite graph in networkx, we can take the following steps −
Set the figure size and adjust the padding between and around the subplots.
Create a list of subset sizes and colors.
Define a method for multilayered graph that could return a multilayered graph object.
Set the color of the nodes.
Position the nodes in layers of straight lines.
Draw the graph G with Matplotlib.
Set equal axis properties.
To display the figure, use show() method.
Example
import itertools import matplotlib.pyplot as plt import networkx as nx plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True subset_sizes = [5, 5, 4, 3, 2, 4, 4, 3] subset_color = [ "gold", "violet", "violet", "violet", "violet", "limegreen", "limegreen", "darkorange", ] def multilayered_graph(*subset_sizes): extents = nx.utils.pairwise(itertools.accumulate((0,) + subset_sizes)) layers = [range(start, end) for start, end in extents] G = nx.Graph() for (i, layer) in enumerate(layers): G.add_nodes_from(layer, layer=i) for layer1, layer2 in nx.utils.pairwise(layers): G.add_edges_from(itertools.product(layer1, layer2)) return G G = multilayered_graph(*subset_sizes) color = [subset_color[data["layer"]] for v, data in G.nodes(data=True)] pos = nx.multipartite_layout(G, subset_key="layer") nx.draw(G, pos, node_color=color, with_labels=False) plt.axis("equal") plt.show()
Output
- Related Articles
- How to show node name in Matplotlib graphs using networkx?
- Matplotlib – Drawing lattices and graphs with Networkx
- How to plot 3D graphs using Python Matplotlib?
- Adding textures to graphs using Matplotlib
- Displaying bar graphs using Matplotlib
- How to draw node colormap in NetworkX/Matplotlib?
- Plotting multiple line graphs using Pandas and Matplotlib
- How to set NetworkX edge labels offset in Matplotlib?
- Displaying horizontal bar graphs using Matplotlib
- Drawing a network graph with networkX and Matplotlib
- How to plot multiple graphs in Matplotlib?
- How do I customize the display of edge labels using networkx in Matplotlib?
- How to change the attributes of a networkx / matplotlib graph drawing?
- How can one modify the outline color of a node in networkx using Matplotlib?
- How to use an update function to animate a NetworkX graph in Matplotlib?

Advertisements