2

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:

enter image description here

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.

1

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
    }}}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}

\begin{document}
\pgfplotstableread[header=false,col sep=comma]{matrix.cvs}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}
\begin{tikzpicture}
\foreach \X in {1,...,\numrows}
{
\foreach \Y in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\X}{\Y}{\Current}
\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}

enter image description here

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
    }}}
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
    \let#4\pgfplotsretval
}

\begin{document}
\pgfplotstableread[header=false,col sep=comma]{matrix.cvs}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval-1}
\begin{tikzpicture}
\foreach \X in {1,...,\numrows}
{
\foreach \Y in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\X}{\Y}{\Current}
\draw (\X-0.5,\Y-0.5) pic {Neuron=\Current};
}
}
\foreach \Y in {1,...,\numcols}
{ 
\ReadOutElement{\datatable}{0}{\Y}{\Current}
\node[anchor=east] at (0,\Y-0.5) {\Current};}
\end{tikzpicture}
\end{document}

enter image description here

  • The form is determined to look like a raster plot. It is meant to show when the neurons spike. These would be perfect if the names of the header in the file appeared in the y axis were automatically and it was faster because it is a pretty large csv. The problem with your previous answer is that it is too slow if the file is large, which is. Though waiting could be an option, when I insert it as a figure in an article with the IJNS journal's style it does not set the width I demmand so I am looking for an alternative to see if it works. I look forward to hearing from you. – Fabio Aug 29 '18 at 16:45
  • @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
  • Yes you understood it well, thank you very much. I have tweaked few things so that the whole csv file could be depicted, but in the end the appearance is perfect. Compiling with lualatex helps because with pdflatex it reaches the maximum number of words (5000000). However, the speed does not improve. In the thread I cited on the question they represent the whole matrix without a loop in which every element is analyzed, which could improve the speed. – Fabio Aug 30 '18 at 19:10
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.