Fundamentals 13 min read

Introduction to NetworkX: Installation, Basic Usage, and Graph Operations in Python

This article introduces the Python NetworkX library, covering its installation via Anaconda, fundamental graph creation, node and edge manipulation, attribute handling, directed and multigraph features, built‑in generators, analysis functions, and visualization with Matplotlib, all illustrated with concrete code examples.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Introduction to NetworkX: Installation, Basic Usage, and Graph Operations in Python

NetworkX is a Python package for creating, manipulating, and studying the structure, dynamics, and functions of complex networks. It supports loading and storing graphs in various formats, generating random or classic graphs, analyzing structures, building models, designing algorithms, and visualizing networks.

Installation : While NetworkX can be installed alongside many related Python libraries, the easiest method is to install the Anaconda distribution, which includes NetworkX and many other useful packages for both Windows and Linux.

Basic usage (examples were run on Ubuntu with Visual Studio Code):

Creating an empty graph: <code>import networkx as nx G = nx.Graph()</code> In NetworkX, nodes can be any hashable object (strings, images, XML objects, other graphs, etc.), but None cannot be used as a node.

Adding nodes: <code>G.add_node(1)</code> From a list: <code>G.add_nodes_from([2, 3])</code> From an iterable nbunch (e.g., another graph): <code>H = nx.path_graph(10) # a path graph with 10 nodes G.add_nodes_from(H) # or G.add_nodes_from(H.nodes())</code> Adding a graph as a node: <code>G.add_node(H)</code>

Adding edges: <code>G.add_edge(1, 2) # equivalent to e = (1, 2) G.add_edge(*e)</code> Adding multiple edges from a list: <code>G.add_edges_from([(1,2), (1,3)])</code> Adding edges from an ebunch (any iterable of edge tuples, optionally with weight dictionaries): <code>G.add_edges_from(H.edges()) # cannot use G.add_edges_from(H)</code> Removing elements: <code>G.remove_node(H) G.clear() # removes all nodes and edges</code> Querying size: <code>print(G.number_of_nodes()) print(G.number_of_edges())</code>

Directed graphs and multigraphs: <code>DG = nx.DiGraph() # convert to undirected H = DG.to_undirected() # or H = nx.Graph(DG)</code> NetworkX provides MultiGraph and MultiDiGraph classes that allow multiple edges between the same pair of nodes, each possibly with different attributes.

Graph generators: <code>petersen = nx.petersen_graph() K_5 = nx.complete_graph(5) er = nx.erdos_renyi_graph(100, 0.15) </code> Reading and writing graph files (e.g., GML): <code>nx.write_gml(red, "path.to.file") mygraph = nx.read_gml("path.to.file")</code>

Analyzing graphs: <code>G = nx.Graph() G.add_edges_from([(1,2), (1,3)]) G.add_node("spam") print(list(nx.connected_components(G))) print(sorted(nx.degree(G).values())) print(nx.clustering(G)) </code>

Attributes: Nodes, edges, and the graph itself can store arbitrary key‑value pairs (e.g., weight , label , color ) via add_node , add_edge , or direct dictionary access such as G.nodes[node]["attr"] and G.edges[edge]["attr"] .

Visualization with Matplotlib: <code>import matplotlib.pyplot as plt nx.draw(G) plt.show() # or save to file nx.draw(G) plt.savefig("path.png") </code>

The basic introduction and usage of NetworkX end here; further topics such as advanced algorithms and integration with complex network research will be covered in future articles.

PythonData Structuresnetwork analysisNetworkXgraph theory
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.