我正在尝试绘制存储在 csv 文件中的神经元尖峰,方式如下:
time, neuron1, neuron2, neuron3,...
1,1,0,1,...
2,1,0,0,...
3,0,0,1,...
等等...
因此,数据提供了某一时刻是否发生了峰值。1 表示发生了峰值,而 0 表示未发生峰值。
我已经关注过这个话题:将栅格点添加到另一个绘图中这会生成我想要的样式。类似于图片中显示的那样。
但是在该线程中,他们随机生成了尖峰模式。简而言之,我想要类似的东西,但使用 csv 文件提供的数据。
答案1
我希望我正确理解了这个问题。这个答案处理的只是包含数据的表格,也就是说,我不明白为什么要放neuron 1
, neuron 2
...,但这当然可以改变。
\documentclass[border=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{neurondata.csv}
0,0,0,0,0,0,0,0
0,0,0,0,0,1,0,0
0,0,0,0,0,1,0,0
0,0,0,0,1,1,0,0
0,0,0,0,1,1,0,0
0,0,0,1,0,1,0,0
0,1,0,0,0,1,0,0
0,0,0,0,0,0,0,0
\end{filecontents*}
\newcommand*{\ReadOutElement}[4]{%
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}
\newsavebox\Flat
\sbox\Flat{\tikz{\path[use as bounding box](-0.5,0) rectangle (0.5,1);
\draw[blue] (-0.5,0.1) -- ++ (1,0);}}
\newsavebox\Spike
\sbox\Spike{\tikz{\path[use as bounding box](-0.5,0) rectangle (0.5,1);
\draw[blue] plot[variable=\x,smooth,samples=50,domain=-0.5:0.5]
(\x,{0.07+0.8*exp(-15*\x*\x)});}}
\begin{document}
\pgfplotstableread[header=false,col sep=comma]{neurondata.csv}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\begin{tikzpicture}
\foreach \Y [evaluate=\Y as \PrevY using {int(\Y-1)}] in {1,...,\numrows}
{\node[anchor=east] at (-0.1,-\Y+0.5) {neuron \Y};
\foreach \X [evaluate=\X as \PrevX using {int(\X-1)}] in {1,...,\numcols}
{
\ReadOutElement{\datatable}{\PrevY}{\PrevX}{\Current}
\ifnum\Current=1
\node at (\X-0.5,-\Y+0.5) {\usebox\Spike};
\else
\node at (\X-0.5,-\Y+0.5) {\usebox\Flat};
\fi
}
}
\foreach \X in {1,...,\numcols}
{\node[anchor=south] at (\X-0.5,0.1) {\X};}
\draw (0,0) grid (\numcols,-\numrows);
\end{tikzpicture}
\end{document}