# Displaying Chessboard Graphs

I'm looking at various graphs and their plotting methods. For the code

G = graphs.KnightGraph([3,3])
G.show()


Sage displays: I'd like the vertices to be arranged in a 3 by 3 grid so that the chessboard nature of the graph is more apparent. From the documentation here I tried specifying the position by using a dictionary:

G = graphs.KnightGraph([3,3])
pos_dict = {}
for i in range(0,2):
for j in range(0,2):
pos_dict[G.vertices()[3*i+j]] = [i*.5,j*.5]
pl = G.graphplot(pos=pos_dict)
pl.show()


This doesn't give the intended result. Even stranger, running the code multiple times shows the position of only some of the vertices is fixed while others change position.

How can I display an n by n chessboard graph so that it's vertices form an n by n square?

edit retag close merge delete

Sort by » oldest newest most voted The range $[0,1,2]$ is obtained by range(0,3) or range(3).

Your pos_dict was too small because you wrote range(0,2) instead, which explains why only a subset of the vertices were fixed.

You can also use set_pos on the graph like this:

sage: G.set_pos(pos_dict)
sage: G.show(vertex_size=1000) more