# plot matrix directly from csv with headers

I would like to plot a huge matrix directly from the csv. I have seen examples in the thread Drawing a large binary matrix as colored grid in TikZ. However, they are importing matrix without headers.

time,input,output,neuron1,neuron2,neuron3
1,0,1,0,0,0
2,0,1,0,0,0
3,0,1,1,1,0
4,0,1,0,0,0
5,1,0,1,0,0
6,1,0,1,0,0
7,1,0,1,1,1
8,1,0,0,0,1
9,1,0,0,1,1
10,1,0,0,1,1
11,1,0,1,1,1
12,1,0,0,1,1
13,1,0,1,0,0
14,1,0,0,1,0
15,1,0,0,0,1
16,1,0,0,0,1
17,1,0,0,0,0
18,1,0,1,0,1
19,1,0,1,1,0
20,0,1,0,1,0
21,0,1,1,1,0
22,0,1,0,0,0
23,0,1,0,0,0


It shoud resemble something like this:

But where the y axis shows the headers. In the case of the file proposed, the first plot should have Input in its y axis. The second plot should have neuron1, neuron2 and neuron 3 in its y axis and so on...

I hope it is clear enough. Otherwise do not hesitate to contact me.

I don't understand what determines the shape of the graphs. But the matrix plot can be done very similarly to my earlier answer. If you tell me what determines the plots, I will be happy to give it a shot.

\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{matrix.cvs}
time,input,output,neuron1,neuron2,neuron3
1,0,1,0,0,0
2,0,1,0,0,0
3,0,1,1,1,0
4,0,1,0,0,0
5,1,0,1,0,0
6,1,0,1,0,0
7,1,0,1,1,1
8,1,0,0,0,1
9,1,0,0,1,1
10,1,0,0,1,1
11,1,0,1,1,1
12,1,0,0,1,1
13,1,0,1,0,0
14,1,0,0,1,0
15,1,0,0,0,1
16,1,0,0,0,1
17,1,0,0,0,0
18,1,0,1,0,1
19,1,0,1,1,0
20,0,1,0,1,0
21,0,1,1,1,0
22,0,1,0,0,0
23,0,1,0,0,0
\end{filecontents*}

\tikzset{pics/.cd,
Neuron/.style={
code={\ifnum#1=0
\fill (-0.5,-0.5) rectangle (0.5,0.5);
\else
\fi
}}}
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}

\begin{document}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}
\begin{tikzpicture}
\foreach \X in {1,...,\numrows}
{
\foreach \Y in {1,...,\numcols}
{
\draw (\X-0.5,\Y-0.5) pic {Neuron=\Current};
}
}
\draw (0,0) grid (\numrows,\numcols);
\foreach \Y in {1,...,\numcols}
{ \node[anchor=east] at (0,\Y) {\Y.5};
\node[anchor=east] at (0,\Y-0.5) {\Y};}
\end{tikzpicture}
\end{document}


EDIT: A version that labels the rows according to the columns of the csv file.

\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\begin{filecontents*}{matrix.cvs}
time,input,output,neuron1,neuron2,neuron3
1,0,1,0,0,0
2,0,1,0,0,0
3,0,1,1,1,0
4,0,1,0,0,0
5,1,0,1,0,0
6,1,0,1,0,0
7,1,0,1,1,1
8,1,0,0,0,1
9,1,0,0,1,1
10,1,0,0,1,1
11,1,0,1,1,1
12,1,0,0,1,1
13,1,0,1,0,0
14,1,0,0,1,0
15,1,0,0,0,1
16,1,0,0,0,1
17,1,0,0,0,0
18,1,0,1,0,1
19,1,0,1,1,0
20,0,1,0,1,0
21,0,1,1,1,0
22,0,1,0,0,0
23,0,1,0,0,0
\end{filecontents*}

\tikzset{pics/.cd,
Neuron/.style={
code={\ifnum#1=0
\fill (-0.5,-0.5) rectangle (0.5,0.5);
\else
\fi
}}}
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}

\begin{document}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}
\begin{tikzpicture}
\foreach \X in {1,...,\numrows}
{
\foreach \Y in {1,...,\numcols}
{
\draw (\X-0.5,\Y-0.5) pic {Neuron=\Current};
}
}
\foreach \Y in {1,...,\numcols}
{

• @Fabio You can get rid of the grid by removing \draw (0,0) grid (\numrows,\numcols);. Do I understand you correctly: instead of the number you want input,output,neuron1,neuron2,neuron3? This is easily doable. I do not know how to speed the thing up. (Does compiling with lualatex help?) – user121799 Aug 29 '18 at 17:15
• I have also tried to select one column with the command \pgfplotstablegetcolumn{columname}\of{\datatable}\to\column and plot it with no success. – Fabio Aug 30 '18 at 19:20
• In addition It would be awesome if I could fit the picture inside a pair of axis. I mean inside \begin{axis} \end{axis} – Fabio Aug 30 '18 at 19:22