4

I have a lot of csv-files with this format:

-,x0,x1,x2,x3,x4
y0,z00,z10,z20,z30,z40
y1,z01,z11,z21,z31,z41
y2,z02,z12,z22,z32,z42

I want to make a 3D Plot with LaTeX. Unfortunately, I was not able to read the data in this format. All examples I found have the format:

x,y,z
x0,y0,z00
x0,y1,z01
x0,y2,z02
x1,y0,z10
x1,y1,z11
x1,y2,z12

This format has redundant information in it and is, in my opinion, not suitable for large data-sets.

I need something like this:

 \begin{filecontents*}{data.csv}
    Value,0,1,2,3,4,5
    0,4,5,6,7,8,9
    1,3,4,5,6,7,8
    2,2,3,4,5,6,7
    3,1,2,3,4,5,6
    4,0,1,2,3,4,5
\end{filecontents*}

\documentclass[border=10pt]{standalone} \usepackage{pgfplots} \usepackage{tikz} \usepackage{tikz-3dplot} \usepackage{csvsimple}

\begin{document} \begin{tikzpicture} \begin{axis}

\addplot3[line width=1pt,solid,color=blue] % table[x=row[0][1:],y=col[0][1:],z=[x+1,y+2],col sep=semicolon]{data.csv};

\end{axis}

\end{tikzpicture} \end{document}

sunrein
  • 43

1 Answers1

2

Gnuplot must be installed and it must be compiled with shell escape.

\begin{filecontents*}{data.csv}
    Value,0,1,2,3,5,6
    0,4,6,6,5,8,9
    1,3,4,5,6,7,8
    2,2,3,8,5,6,7
    3,1,2,3,4,3,6
    5,0,1,4,3,4,5
\end{filecontents*}

\documentclass[tikz, border=1cm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[view={10}{40}] \addplot3[surf] gnuplot[raw gnuplot] {set datafile separator comma; splot "data.csv" nonuniform matrix}; \end{axis} \end{tikzpicture} \end{document}

3D surface plot

Edit: Here the same plot are again with one x-value changed to emphasize the non uniformity:

\begin{filecontents*}[overwrite]{data.csv}
Value,0,1,2,3,5,60
0,4,6,6,5,8,9
1,3,4,5,6,7,8
2,2,3,8,5,6,7
3,1,2,3,4,3,6
5,0,1,4,3,4,5
\end{filecontents*}

\documentclass[tikz, border=1cm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[view={10}{40}] \addplot3[surf] gnuplot[raw gnuplot] {set datafile separator comma; splot "data.csv" nonuniform matrix}; \end{axis} \end{tikzpicture} \end{document}

Non uniform matrix surface plot

With matrix columnheaders rowheader the plot will be wrong (not have the data format given by OP).

Uniform matrix surface plot

  • Thanks for the answer. I was able to build it with your help. But I needed to replace nonuniform matrix with matrix columnheaders rowheaders. In the documentation of gnuplot I found that on the csv the very first value (0,0) needs to be N+1. Since we have Text here, I get an error. – sunrein Feb 24 '22 at 08:23
  • The above code compiles fine for me. You were asking about a nonuniform matrix (even though your sample data are uniform). If you change nonuniform matrix to matrix columnheaders rowheaders in my code with my sample data, the plot will be wrong. – hpekristiansen Feb 24 '22 at 08:36