4

Long time has passed - based on this and this question I would like to know if pgfplots does already support surface smoothening of coordinate based 3D plots in 2019?


Minimum Working Example (MWE):

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\usepackage{filecontents}

\begin{filecontents}{testdata.csv}
    x;y;z
    0;0;4
    0;1;4
    0;2;4
    1;0;2
    1;1;2
    1;2;2
    2;0;3
    2;1;3
    2;2;3
    3;0;0
    3;1;0
    3;2;0
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[table/col sep = semicolon]
            \addplot3[mesh, mesh/cols=3,surf, shader=interp] table[x=x, y=y, z=z] {testdata.csv};
        \end{axis}
    \end{tikzpicture}
\end{document}

Screenshot of the result:

Screenshot of the result


Screenshot of the desired state:

Screenshot of the desired state


As you can see, the desired state represents some beautiful grid lines as well as smoothened curves instead of rough peaks. Is this purely possible with pgfplots nowadays?

Unfortunately combining [surf] and [smooth] of a plot does not work.

Dave
  • 3,758

1 Answers1

3

I do not think that, as of now, there is a smooth option for surface plots. However, you can smoothen out the table data yourself. As any smoothening, this comes with a prescription and with parameters. If you draw a 1D curve you can specify the tension, so you may want to think of the \mysigmax and \mysigmay parameters in the following code as serving a similar purpose.

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=1.16}
\usepackage{filecontents}

\begin{filecontents*}{testdata.csv}
    x;y;z
    0;0;4
    0;1;4
    0;2;4
    1;0;2
    1;1;2
    1;2;2
    2;0;3
    2;1;3
    2;2;3
    3;0;0
    3;1;0
    3;2;0
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
  \pgfplotstableread[col sep=semicolon]{testdata.csv}\datatable
  \pgfplotstablegetrowsof{\datatable}
  \pgfmathtruncatemacro{\Xmax}{\pgfplotsretval-1}
  \pgfmathsetmacro\mysum{0}
  \pgfmathsetmacro\mysigmax{0.5}
  \pgfmathsetmacro\mysigmay{0.3}
  \pgfplotsforeachungrouped \X in {0,...,\Xmax}{
     \pgfplotstablegetelem{\X}{x}\of{\datatable}
     \edef\myx{\pgfplotsretval}
     \pgfplotstablegetelem{\X}{y}\of{\datatable}
     \edef\myy{\pgfplotsretval}
     \pgfplotstablegetelem{\X}{z}\of{\datatable}
     \edef\myz{\pgfplotsretval}
     \edef\mysum{\mysum+\myz*exp(-((x-\myx)^2/\mysigmax+(y-\myy)^2/\mysigmay))}
  }
  %\typeout{\mysum}
        \begin{axis}[table/col sep = semicolon]
            %\addplot3[mesh, mesh/cols=3,surf, shader=interp] table[x=x, y=y, z=z] {testdata.csv};
            \addplot3[surf,domain=0:3,domain y=0:2] {\mysum};
        \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

If you set \mysigmay to 3 instead you get

enter image description here