
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- How to change the attributes of a networkx / matplotlib graph drawing?
- Drawing a network graph with networkX and Matplotlib
- How to use an update function to animate a NetworkX graph in 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?
- Reshape the Matrix in C++
- How to draw node colormap in NetworkX/Matplotlib?
- Python - How to plot a Pandas DataFrame in a Bar Graph
- How to set NetworkX edge labels offset in Matplotlib?
- How to convert ggplot2 graph into a plotly graph in R?
- How to show node name in Matplotlib graphs using networkx?
- How to make multipartite graphs using networkx and Matplotlib?
- Graph Plotting in Python
- Generate a graph using Dictionary in Python
Advertisements