2

I want to create a 3D plot from previously calculated coordinates saved in a file, like "xz.dat".

This contains the data as :

x z
0.5 0
0.54 0.01
0.58 0.01
0.62 0.02
0.66 0.03
0.7 0.03
0.74 0.04
0.78 0.05
etc.

The minimum working example :

\documentclass{article}

\usepackage{datatool}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[x={(210:3cm)},y={(330:3cm)},z={(90:3cm)}]
\draw (0,0,0)--(1,0,0);
\draw (0,0,0)--(0,1,0);
\draw (0,0,0)--(0,0,3);
\DTLsetseparator{ }
\DTLloaddb[noheader=false]{coordinates}{./xz.dat}
\DTLforeach*{coordinates}{\x=x,\z=z}{\filldraw (\x,0,\z) circle (0.02);}
\end{tikzpicture}

\end{document}

Which results in:

enter image description here

Is it possible to connect the succesive coordinates using a smooth line instead of these ugly squashed points?

I know how to do that using "plot file", but this only works for 2D plots.

Petoetje59
  • 1,051

1 Answers1

2

There are many ways to plot a file in 3d even though it has only two-dimensional coordinates. One way is to add a column with y=0. Of course, you do not have to do that by hand.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{data2.dat}
x z
0.5 0
0.54 0.01
0.58 0.01
0.62 0.02
0.66 0.03
0.7 0.03
0.74 0.04
0.78 0.05
\end{filecontents*}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.16}
\pgfplotstableset{
    create on use/new y/.style={
        create col/expr={0}}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=-0.2,ymax=1]
\addplot3[no marks,smooth] table[x=x,y=new y,z=z] {data2.dat};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Of course, you can also add a smooth plot in the framework you are using.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{xz.dat}
x z
0.5 0
0.54 0.01
0.58 0.01
0.62 0.02
0.66 0.03
0.7 0.03
0.74 0.04
0.78 0.05
\end{filecontents*}
\usepackage{datatool}
\newcounter{mycoord}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[x={(-25.98mm,-15mm)},y={(25.98mm,-15mm)},z={(0,30mm)}]
\draw (0,0,0)--(1,0,0);
\draw (0,0,0)--(0,1,0);
\draw (0,0,0)--(0,0,3);
\DTLsetseparator{ }
\DTLloaddb[noheader=false]{coordinates}{./xz.dat}
\DTLforeach*{coordinates}{\x=x,\z=z}{\stepcounter{mycoord}
\filldraw (\x,0,\z) coordinate(X-\themycoord) circle (0.02);}
\draw plot[smooth,samples at={1,...,\themycoord},variable=\x] (X-\x);
\end{tikzpicture}

\end{document}

Using your data file (and getting rid of the bullets) yields

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{filecontents}
\usepackage{datatool}
\newcounter{mycoord}
\usepackage{tikz}
%\usetikzlibrary{decorations.markings}
\begin{document}

\begin{tikzpicture}[x={(-25.98mm,-15mm)},y={(25.98mm,-15mm)},z={(0,30mm)}]
\draw (0,0,0)--(1,0,0);
\draw (0,0,0)--(0,1,0);
\draw (0,0,0)--(0,0,3);
\DTLsetseparator{ }
\DTLloaddb[noheader=false]{coordinates}{./xz.dat}
\DTLforeach*{coordinates}{\x=x,\z=z}{\stepcounter{mycoord}
\path (\x,0,\z) coordinate(X-\themycoord);}
\draw plot[smooth,tension=0.5,samples at={1,...,\themycoord},variable=\x] (X-\x);
\end{tikzpicture}

\end{document}

enter image description here

The little wiggles come from the data, not from the smoothening.

  • Inserting coordinates of thousands of datapoints within the body of the LaTeX file isn't really feasible; these data have to remain in external files. And - the axisses are no longer isometric in the first proposal using pgfplots. – Petoetje59 Feb 23 '19 at 22:28
  • @Petoetje59 Yes, I am not disputing this. I only describe the reason why I do not provide a screen shot: it is an almost empty plot. –  Feb 23 '19 at 22:30
  • The full data file can be found at: https://app.box.com/s/1h9hujklhxb3exsxzx8zbezjv13fcbhr – Petoetje59 Feb 23 '19 at 22:39
  • @Petoetje59 I added a plot. –  Feb 23 '19 at 22:50
  • I'll look into this more carefully tomorrow, and play with the parameters. The wiggles are caused by the smooth parameter causing the line NOT to pass through the data points (like Bézier curves do) however, maybe straight lines will make things look better. – Petoetje59 Feb 23 '19 at 23:13
  • @Petoetje59 If you replace the smooth curve by straight lines you will get little zigzags. (To see this, just remove smooth.) Some ideas to smoothen out some data with wiggles can be found under this question. Given that you produce the data with some software, I feel that the most efficient way will be to do it there, and not with LaTeX, which is not a computer algebra system. –  Feb 24 '19 at 04:09
  • Found another way to solve it without the need of datatool nor pgfplots - simply by integrating the data generating equations in the code and using \draw ... plot ... Anyway, thanks for the info, this way I discovered the interesting pgfplots package! – Petoetje59 Feb 24 '19 at 21:07