# How to automatically ask tikz to produce the coordinates and plot the graph from adjacency matrix

I am curious about the following:

I have an adjacency matrix of a graph of order 36. It is given by

[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
[1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
[1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1],
[1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0],
[1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
[1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0],
[1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,0,0],
[1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1],
[1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0],
[1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,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1,0,1,1,0,1,1,0],
[1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1],
[1,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
[1,1,0,1,1,0,1,1,0,1,0,0,1,1,0,1,1,0,1,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,1,0],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0],
[1,1,1,1,0,1,0,1,1,0,1,1,1,0,0,1,1,1,1,0,0,1,1,1,0,1,1,0,1,0,1,1,1,1,0,1],
[1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0],


If I plot the graph in SageMath I get the following graph

I want to know if its possible to plot the graph in Tikz without specifying the coordinates automatically from the adjacency matrix.

It will be very difficult to specify the coordinates manually. I tried it once but could not succeed. Its looking cumbersome.

Can someone please help to draw the graph from the adjacency matrix?

The following question does not answer my question How to plot a graph from its adjacency matrix and coordinates of vertices?

because I don't have my coordinates specified. I want TeX to automatically fix the co-ordinates as SageMath does.

Is it possible??

## 1 Answer

It sounds like you're trying to do something I've been messing around with for awhile: Push SAGE results into LaTeX and get better looking output. My answer here does this for the Cantor function, plotting primes, 3D plots, and here. Search the site for sagetex for more examples. The link mentioned by js bibra above shows some of how it can be done with graphs as well but you have to dig a little bit.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0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1,0),
(1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0),
(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1),
(1,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0),
(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1),
(1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0),
(1,1,0,1,1,0,1,1,0,1,0,0,1,1,0,1,1,0,1,1,0,0,1,0,1,1,0,1,1,0,1,1,0,1,1,0),
(1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0),
(1,1,1,1,0,1,0,1,1,0,1,1,1,0,0,1,1,1,1,0,0,1,1,1,0,1,1,0,1,0,1,1,1,1,0,1),
(1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0)])
g = Graph(M)
g.set_pos(g.layout_circular())
g.set_latex_options(graphic_size=(20,20))
\end{sagesilent}
\begin{tikzpicture}[node distance = 10mm and 10mm]
\tikzset{EdgeStyle/.append style = {color = blue!60, line width=1pt}}
\sage{g}
\end{tikzpicture}
\end{document}


The output from Cocalc is shown below:

Some things to mention:

1. Sage can take care of the positioning for you. That's covered in the documentation here. See plot options and followed by layout. I chose g.set_pos(g.layout_circular()) for a circular layout.
2. I've set the size of the graphic (which will affect the size of the circle) with g.set_latex_options(graphic_size=(20,20)). You can experiment with the size as you see fit.
3. Notice that Sage has the vertices listed starting with vertex 0. In another post you seem to be asking a similar question but you have the vertices starting with 1. You can relabel the vertices in Sage first. Add the line g.relabel(lambda x: x+1) in sagesilent and the vertices will go from 1 to 36 as shown below:

In general, it helps to set up the graph in sagesilent and you can tweak some of the settings in tikz. The line \tikzset{EdgeStyle/.append style = {color = blue!60, line width=1pt}} sets the color and width of the line. However, sometimes you just need to fiddle with what parts of the graph should be set in Sage and what in LaTeX. See other posts such as here, here. Note if the graph has a name, you can use that as well. Sage recognizes a variety of graphs given here. LaTeX options for graphs with Sage are given here.

• Thanks a lot for the answer' – Math_Freak Jul 4 '20 at 14:18