Skip to main content

Drawing basics

Draw methods

You can draw the graph using two drawing methods: draw() and draw_networkx(). With draw() you can draw a simple graph with no node labels or edge labels and using the full Matplotlib figure area and no axis labels by default, while draw_networkx() allows you to define more options and customize your graph.

Let's say we defined a complete bipartite graph as below:

import networkx as nx
import matplotlib.pyplot as plt

K33 = nx.complete_bipartite_graph(3, 3)

To draw it with draw() method, we use the following code:


The output:


On the other hand, if we use draw_networkx() method, we need to run the following code for the default options:


The output:



Graph layout will define node position for your graph drawing. There are a bunch of predefined layouts in NetworkX. The default one is called spring_layout() which poistions nodes using Fruchterman-Reingold force-directed algorithm.

Let's show the basic usage of graph layouts on a simple graph example.

import networkx as nx
import matplotlib.pyplot as plt

G = nx.dodecahedral_graph()

Since we did not define any layout above, the default spring_layout() will be used.

If we want to draw the same graph with the shell_layout() that positiones nodes in concentric circles, we would use the following code:

nx.draw(G, pos=nx.shell_layout(G))

For spiral_layout() run:

nx.draw(G, pos=nx.spiral_layout(G))

Here is how these three layouts look like:



You can use the exact positions of the nodes, instead of using predefined layouts. To do that, you need to define a dictionary with nodes as keys and positions as values.


import networkx as nx
import matplotlib.pyplot as plt

K33 = nx.complete_bipartite_graph(3, 3)

positions = {0: [-1, 1], 1: [0, 1], 2: [1, 1], 3: [-1, -1], 4: [0, -1], 5: [1, -1]}
ax = plt.figure().gca()
options = {"node_size": 300, "node_color": "red"}
nx.draw_networkx(K33, positions, with_labels=False, **options)

Graph styling

There are numerous styling options which let you customize your graph. For example, you can define colors of the nodes, draw node and edge labels, change font size, etc. Below you can check out a simple example of graph styling.

Let's say we want to read a graph from the following graph.gaphml file:

<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<key id="d8" for="edge""type" attr.type="string" />
<key id="d7" for="node""followers" attr.type="int" />
<key id="d6" for="node""platform" attr.type="string" />
<key id="d5" for="node""username" attr.type="string" />
<key id="d4" for="node""fraudReported" attr.type="boolean" />
<key id="d3" for="node""compromised" attr.type="boolean" />
<key id="d2" for="node""age" attr.type="int" />
<key id="d1" for="node""name" attr.type="string" />
<key id="d0" for="node""label" attr.type="string" />
<graph edgedefault="directed">
<node id="1">
<data key="d0">Person</data>
<data key="d1">John Doe</data>
<data key="d2">40</data>
<node id="2">
<data key="d0">CreditCard</data>
<data key="d1">Visa</data>
<data key="d3">False</data>
<node id="3">
<data key="d0">Store</data>
<data key="d1">Walmart</data>
<node id="4">
<data key="d0">Category</data>
<data key="d1">Grocery store</data>
<node id="5">
<data key="d0">Pos</data>
<data key="d3">False</data>
<node id="6">
<data key="d0">Transaction</data>
<data key="d4">False</data>
<node id="22">
<data key="d0">SocialMedia</data>
<data key="d5">john_doe</data>
<data key="d6">Facebook</data>
<data key="d7">2000</data>
<edge source="1" target="2">
<data key="d8">OWNS</data>
<edge source="1" target="22">
<data key="d8">HAS_ACCOUNT</data>
<edge source="2" target="6">
<data key="d8">HAS_TRANSACTION</data>
<edge source="3" target="4">
<data key="d8">IS_OF_CATEGORY</data>
<edge source="3" target="5">
<data key="d8">HAS_POS_DEVICE</data>
<edge source="6" target="5">
<data key="d8">TRANSACTION_AT</data>

Other examples

Here is an example of how to draw a simple graph:

import matplotlib.pyplot as plt
import networkx as nx

G = nx.petersen_graph()

nx.draw(G, with_labels=True, font_weight='bold', node_color='orange')

nx.draw_shell(G, nlist=[range(5, 10), range(5)], with_labels=True, font_weight='bold', node_color='orange')

Here are some other options for drawing graphs:

import matplotlib.pyplot as plt
import networkx as nx

options = {
'node_color': 'orange',
'node_size': 100,
'width': 3,

G = nx.petersen_graph()

nx.draw_random(G, **options)

nx.draw_circular(G, **options)

nx.draw_spectral(G, **options)

nx.draw_shell(G, nlist=[range(5,10), range(5)], **options)

Useful tips

How to draw directed graphs using NetworkX in Python?

By using the base class for directed graphs, DiGraph(), we are able to draw a directed graph with arrows to indicate the direction of edges.

import networkx as nx
import matplotlib.pyplot as plt

G = nx.DiGraph()
[('A', 'B'), ('A', 'C'), ('D', 'B'), ('E', 'C'), ('E', 'F'),
('B', 'H'), ('B', 'G'), ('B', 'F'), ('C', 'G')])


How to draw a NetworkX graph with labels?

If you want the node labels to be visible in your drawing, just add with_labels=True to the nx.draw call.

import networkx as nx
import matplotlib.pyplot as plt

G.add_edge("Node1", "Node2")
nx.draw(G, with_labels = True)

How to change the color and width of edges in NetworkX graphs according to edge attributes?

Dictionaries are the underlying data structure used for NetworkX graphs, and as of Python 3.7+ they maintain insertion order. You can use the nx.get_edge_attributes() function to retrieve edge attributes. For every run, we are guaranteed to have the same edge order.

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_edge(1, 2, color='r' ,weight=3)
G.add_edge(2, 3, color='b', weight=5)
G.add_edge(3, 4, color='g', weight=7)

pos = nx.circular_layout(G)

colors = nx.get_edge_attributes(G,'color').values()
weights = nx.get_edge_attributes(G,'weight').values()

nx.draw(G, pos, edge_color=colors, width=list(weights))

How to color nodes in NetworkX graphs?

You need to define a color map that assigns a color to each node. For example, if were to color the first 15 nodes of a graph in orange, and the rest in blue, then the code would be:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.karate_club_graph()
color_map = []
for node in G:
if node < 15:

nx.draw(G, node_color=color_map, with_labels=True)

How to color nodes in NetworkX graphs according to their attributes?

You need to define a color map that assigns a color to each node.

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_node('A', color='red')
G.add_node('B', color='green')
G.add_node('C', color='blue')

[('A', 'B'), ('A', 'C'), ('B', 'C')])

colors = [node[1]['color'] for node in G.nodes(data=True)]

nx.draw(G, node_color=colors, with_labels=True, font_color='white')

Where to next?

If you wish to learn more about drawing graphs with NetworkX, visit the draw_networkx() and Graph Layout sections in the NetworkX reference guide. If you find this kind of drawing complicated and it is not working that well for your scale, check out how to visualise your graphs easy here.