1

I have done a range of experiments, each using 9 sensors. The sensors are physically aligned in 3 by 3, and I would like to also show the data in a mesh or surface plot like this. However, I get the data in a csv file where each row is one experiment with the resulting sensor values, like the following:

Experiment, S0, S1, S2, S3, S4, S5, S6, S7, S8
Test1, 129.0, 82.6, 61.0, 110.6, 113.7, 95.9, 51.9, 98.1, 169.1
Test2, 82.5, 58.5, 51.8, 65.1, 84.1, 80.0, 44.6, 75.5, 88.5

As I want to plot quite a lot of them, I would prefer not to manually need to realign data or to write a script that does.

I already tried using different options but can't figure out what would be the right way to approach it.

This is my MWE for having a 2D plot of this data:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Experiment, S0, S1, S2, S3, S4, S5, S6, S7, S8
Test1, 129.0, 82.6, 61.0, 110.6, 113.7, 95.9, 51.9, 98.1, 169.1
Test2, 82.5, 58.5, 51.8, 65.1, 84.1, 80.0, 44.6, 75.5, 88.5
\end{filecontents*}

\begin{document}
\pgfplotstableread[col sep=comma]{data.csv}\rawtable
\pgfplotstabletranspose[colnames from=Experiment]\data{\rawtable}
\begin{tikzpicture}
\begin{axis}[]
\addplot[mark=*,blue] table [x expr=\coordindex, y=Test1, col sep=comma] {\data};
\addplot[mark=*,red] table [x expr=\coordindex, y=Test2, col sep=comma] {\data};
\end{axis}
\end{tikzpicture}

\end{document}
1

I just found the solution myself, but think that it might be tricky to find, so I share. I am using x expr and y expr to compute the coordinates. Other suggestions are still welcome!

Here see the MWE for 3D plots, only changing the two \addplot lines to some \addplot3 lines was sufficient.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
Experiment, S0, S1, S2, S3, S4, S5, S6, S7, S8
Test1, 129.0, 82.6, 61.0, 110.6, 113.7, 95.9, 51.9, 98.1, 169.1
Test2, 82.5, 58.5, 51.8, 65.1, 84.1, 80.0, 44.6, 75.5, 88.5
\end{filecontents*}

\begin{document}
\pgfplotstableread[col sep=comma]{data.csv}\rawtable
\pgfplotstabletranspose[colnames from=Experiment]\data{\rawtable}
\begin{tikzpicture}
\begin{axis}[]
\addplot3[mesh,mark=*,blue] table [x expr={int(mod(\coordindex,3))}, y expr={int(\coordindex / 3)}, z=Test1, col sep=comma] {\data};
\addplot3[mesh,mark=*,red] table [x expr={int(mod(\coordindex,3))}, y expr={int(\coordindex / 3)}, z=Test2, col sep=comma] {\data};
\end{axis}
\end{tikzpicture}

\end{document}

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.