How Do You Create a Graph in Python Data Structure?


Scott Campbell

Creating a graph is a fundamental task in Python data structure. A graph consists of a set of vertices or nodes connected by edges.

It is used to represent relationships between entities, such as social networks, road maps, and computer networks. In this tutorial, we will explore how to create a graph in Python using various methods and libraries.

Using the NetworkX Library

To create a graph in Python, we can utilize the NetworkX library. NetworkX provides an extensive set of tools for working with graphs. To get started, you need to install the library using pip:

pip install networkx

Once installed, you can import the library in your Python script:

import networkx as nx

To create a basic graph, you can initialize an empty graph object:

G = nx.Graph()

This creates an undirected graph without any nodes or edges. You can add nodes to the graph using the add_node() method:


The above code adds three nodes named ‘A’, ‘B’, and ‘C’ to the graph. To add edges between nodes, you can use the add_edge() method:

G.add_edge('A', 'B')
G.add_edge('B', 'C')

This code adds two edges: one between node ‘A’ and node ‘B’, and another between node ‘B’ and node ‘C’. The resulting graph would look like this:

Graph Visualization

You can visualize the created graph using the draw() or draw_networkx() functions:

import matplotlib.pyplot as plt

nx.draw(G, with_labels=True)

This code will display a graphical representation of the graph with labeled nodes.

Using the Graph Tool Library

Another powerful library for creating graphs in Python is Graph Tool. Graph Tool provides a fast and efficient way to work with large-scale graphs. To install Graph Tool, you can use the following command:

pip install python-graph-tool

After installation, you can import the library in your Python script:

import graph_tool.all as gt

To create an empty graph, you can use the Graph() constructor:

G = gt.Graph()

This creates an undirected graph similar to NetworkX. To add vertices and edges, you can use the add_vertex() and add_edge() methods:

v1 = G.add_vertex()
v2 = G.add_vertex()
v3 = G.add_vertex()

e1 = G.add_edge(v1, v2)
e2 = G.add_edge(v2, v3)

In this example, we added three vertices (v1, v2, and v3) and two edges (e1 and e2) between them.

Drawing the Graph

To visualize the graph using Graph Tool’s built-in drawing capabilities, you can use the graph_draw() function:

gt.graph_draw(G, vertex_text=G.vertex_index, vertex_font_size=18, output_size=(500, 500), output="graph.png")

This code will render the graph and save it as an image file named “graph.png”. The vertex_text parameter displays the indices of the vertices as labels.


In this tutorial, we explored two libraries for creating graphs in Python: NetworkX and Graph Tool. Both libraries provide powerful tools for working with graphs and offer various methods to create and manipulate graphs. By using these libraries, you can easily represent complex relationships between entities in your Python programs.

Now that you have learned the basics of creating a graph in Python data structure using NetworkX and Graph Tool, you can explore more advanced features and algorithms provided by these libraries to analyze and visualize your graph data.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy