Featured Article

NetworkX Examples: Master Graph Visualization with Code Snippets

Kenneth Jul 13, 2026

NetworkX is a powerful Python library for creating, manipulating, and studying the structure, dynamics, and functions of complex networks. Its simplicity and versatility make it an indispensable tool for numerous domains, from social network analysis to biological systems modeling. Let's explore some practical examples to illustrate its capabilities.

Jacob Silverman (@SilvermanJacob) on X
Jacob Silverman (@SilvermanJacob) on X

Before diving into examples, ensure you have NetworkX installed. If not, you can install it using pip: `pip install networkx`.

Generate a Mind Map from Any Text
Generate a Mind Map from Any Text

Creating and Visualizing Networks

NetworkX provides intuitive ways to create networks and visualize them using its built-in functions and external libraries like matplotlib and Graphviz.

The Neural Network Zoo - The Asimov Institute
The Neural Network Zoo - The Asimov Institute

Let's create a simple undirected graph with Rock, Paper, and Scissors nodes and visualize it using Graphviz.

Undirected Graph

the world's most famous lines are depicted in this infographtion chart, which shows
the world's most famous lines are depicted in this infographtion chart, which shows

First, import the necessary libraries and create an undirected graph using `networkx.Graph()`.

```python import networkx as nx import graphviz G = nx.Graph() G.add_edge('Rock', 'Paper') G.add_edge('Paper', 'Scissors') G.add_edge('Rock', 'Scissors') ```

Now, let's create a visualization using graphviz:

an image of a large network with many different people on the top and bottom half
an image of a large network with many different people on the top and bottom half

```python dot = graphviz.Dot() dot.node_attr.update(color='lightblue2') for node in G.nodes: dot.node(node) for edge in G.edges: dot.edge(edge[0], edge[1]) dot.view() ```

Directed Graph

A directed graph (digraph) is useful when tracking the direction of relationships. Let's create a directed graph to represent word associations.

Mapping Our Friendships Over Time and Space: The Future of Social Network Analysis
Mapping Our Friendships Over Time and Space: The Future of Social Network Analysis

```python G_directed = nx.DiGraph() G_directed.add_edge('Apple', 'Banana') G_directed.add_edge('Banana', 'Apple') G_directed.add_edge('Apple', 'Tree') nx.draw(G_directed, with_labels=True, node_color='lightblue', edge_color='black') ```

Analyzing Network Properties

the internet and its connections infographicly displayed on a whiteboard with different types of information
the internet and its connections infographicly displayed on a whiteboard with different types of information
a computer screen with many people connected to each other and one is showing the same image
a computer screen with many people connected to each other and one is showing the same image
LJNet - LiveJournal Social Network Visualization
LJNet - LiveJournal Social Network Visualization
OSI Model Layers Explained | Networking Basics for Beginners
OSI Model Layers Explained | Networking Basics for Beginners
3. Network calls replacing function calls
3. Network calls replacing function calls
a large poster with many different types of symbols and numbers in red, white and pink
a large poster with many different types of symbols and numbers in red, white and pink
a diagram with many different types of people in the middle of it, including men and women
a diagram with many different types of people in the middle of it, including men and women
Lin Tan_Module 06
Lin Tan_Module 06
#elv101 #elvsystems #networkdesign #bms #smartbuildings #buildingautomation #engineeringmanagement | Marwan Hilal
#elv101 #elvsystems #networkdesign #bms #smartbuildings #buildingautomation #engineeringmanagement | Marwan Hilal

NetworkX offers various functions to analyze network properties, such as degree centrality, betweenness centrality, and clustering coefficient.

Degree Centrality

The degree centrality of a node is the number of edges connected to it. Let's calculate the degree centrality of our Rock-Paper-Scissors network.

```python degree_centrality = nx.degree_centrality(G) print(degree_centrality) ```

Betweenness Centrality

Betweenness centrality measures the number of times a node lies on the shortest path between other nodes. It helps identify nodes that play a vital role in connecting other parts of the network.

```python betweenness_centrality = nx.betweenness_centrality(G) print(betweenness_centrality) ```

Exploring NetworkX's capabilities is an ongoing journey filled with discovering new ways to model and analyze networks. Whether you're a researcher, data scientist, or just curious about network analysis, NetworkX provides an excellent starting point. Happy network毫无疑عان!