- 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 reshape a networkx graph in Python?
To reshape a networkx graph in Python, we can take the following steps −
Create a data frame using Panda's data frame.
Return a graph from Pandas DataFrame containing an edge list using from_pandas_edgelist() method.
Draw the graph G with matplotlib. We can reshape the network by increasing and decreasing the list of keys "from" and "to".
To display the figure, use show() method.
Example
import pandas as pd import networkx as nx from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame({'from': ['A', 'B', 'C', 'A'], 'to': ['D', 'A', 'E', 'C']}) G = nx.from_pandas_edgelist(df, 'from', 'to') nx.draw(G, with_labels=True, node_size=150, alpha=0.5, linewidths=40) plt.show()
Output
- Related Articles
- How to change the attributes of a networkx / matplotlib graph drawing?
- How to use an update function to animate a NetworkX graph in Matplotlib?
- Drawing a network graph with networkX and Matplotlib
- Python – Reshape the data in a Pandas DataFrame
- Write a Python program to reshape a given dataframe in different ways
- How to plot a graph in Python?
- How to draw node colormap in NetworkX/Matplotlib?
- Reshape the Matrix in C++
- How to set NetworkX edge labels offset in Matplotlib?
- Python - How to plot a Pandas DataFrame in a Bar Graph
- How to show node name in Matplotlib graphs using networkx?
- How to make multipartite graphs using networkx and Matplotlib?
- How to plot y=1/x as a single graph in Python?
- How to skip empty dates (weekends) in a financial Matplotlib Python graph?
- How to change the size of a Dash Graph in Python Plotly?

Advertisements