Setting Node Attributes in Igraph – Python

Creating a simple graph

python terminal

# load the igraph module
>>> from igraph import *

# Create an empty graph
>>> g=Graph()

# add 3 nodes
>>> g.add_vertices(3)

# Plot it to a file to have a quick look
# and limit it to 200px either way.
>>> plot(g,'pathTo/file.png',(200,200))

It should look something like this.


Set the coordinates

The coordinates can be set using lists.

# Coordinates for x are: 1 for right, 0 for left
>>> xCoord=[1,1,0]
# coordinates for y are: 1 for bottom, 0 for top
>>> yCoord=[0,1,1]

# set coordinates to the graph
>>> g.vs['x']=xCoord
>>> g.vs['y']=yCoord

# Let's check it again
>>> plot(g,'pathTo/file.png',(200,200))


Change Node Size and Colour

# Again, change the size of the node using a list
>>> mySizes=[10,20,30]

# then set the sizes
>>> g.vs['size']=mySizes

# now create a colour list
>>> myColours=['green','blue','grey']

# and set the node colors
>>> g.vs['color']=myColours

# and check the graph one last time
>>> plot(g,'pathTo/file.png',(200,200))

The final product should be this


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s