[Tex/LaTex] How to draw a color map on a ternary axis plot

pgfplots

I have a function defined on the 3-dimensional simplex that I would like to display as a color map on a ternary axis plot.

\begin{filecontents*}{dirichlet.dat}
x y z density
0.0 0.0 1.0 0.0034743774733111514
0.0 0.25 0.75 0.015673856116184083
0.0 0.5 0.5 0.012098485065977549
0.0 0.75 0.25 0.015673856116184083
0.0 1.0 0.0 0.0034743774733111514
0.25 0.0 0.75 0.015673856116184083
0.25 0.25 0.5 0.07861618789696313
0.25 0.5 0.25 0.07861618789696313
0.25 0.75 0.0 0.015673856116184083
0.5 0.0 0.5 0.012098485065977549
0.5 0.25 0.25 0.07861618789696313
0.5 0.5 0.0 0.012098485065977549
0.75 0.0 0.25 0.015673856116184083
0.75 0.25 0.0 0.015673856116184083
1.0 0.0 0.0 0.0034743774733111514
\end{filecontents*}

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{ternary}
\usepackage{tikz}

\begin{document}

 \begin{figure}[ht]
\centering
\begin{tikzpicture}
\begin{ternaryaxis} [colorbar, colorbar style={ylabel=Density},
   ternary limits relative=false,
   xmin=0, xmax=1, xlabel = $x_1$,
   ymin=0, ymax=1, ylabel = $x_2$,
   zmin=0, zmax=1, zlabel = $x_3$,
   label style={sloped}]
  \addplot3+[only marks,
  point meta=\thisrow{density},
  nodes near coords*={\tiny{\pgfmathprintnumber\density}},
  visualization depends on={\thisrow{density} \as \density}] table {dirichlet.dat};
\end{ternaryaxis}
\end{tikzpicture}
\caption{Dirichlet Distribution}
\end{figure}

\end{document}

What I have here just plots the points, but I would like to use pgfplots to interpolate the values between them and fill out the color map like when you use the smooth option in a 2d axis plot. (Using smooth in place of only marks here just draws a line connecting the points.)

enter image description here

Best Answer

Smoothly interpolated color maps are supported for plot types mesh, surf, and patch. These, however, require either a mesh (=matrix) of data points on input or a sequence of patches. I suppose a sequence of triangular patches would do the job in your case. You would need to determine this triangulation on your own; pgfplots cannot do it based on your input file.

Here is a small example with three triangles. Note that this here is merely intended to be used as demonstration; it has actually nothing to do with your input file.

enter image description here

\begin{filecontents*}{dirichlet.dat}
x y z density
0.0 0.0 1.0 0.0034743774733111514
0.0 0.25 0.75 0.015673856116184083
0.0 0.5 0.5 0.012098485065977549
0.0 0.75 0.25 0.015673856116184083
0.0 1.0 0.0 0.0034743774733111514
0.25 0.0 0.75 0.015673856116184083
0.25 0.25 0.5 0.07861618789696313
0.25 0.5 0.25 0.07861618789696313
0.25 0.75 0.0 0.015673856116184083
0.5 0.0 0.5 0.012098485065977549
0.5 0.25 0.25 0.07861618789696313
0.5 0.5 0.0 0.012098485065977549
0.75 0.0 0.25 0.015673856116184083
0.75 0.25 0.0 0.015673856116184083
1.0 0.0 0.0 0.0034743774733111514
\end{filecontents*}

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{ternary}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\begin{ternaryaxis} [colorbar, colorbar style={ylabel=Density},
   ternary limits relative=false,
   xmin=0, xmax=1, xlabel = $x_1$,
   ymin=0, ymax=1, ylabel = $x_2$,
   zmin=0, zmax=1, zlabel = $x_3$,
   label style={sloped},
   point meta rel=per plot,% I added this such that my patch plot does NOT affect the second plot
   ]

    \addplot3[patch,patch type=triangle,shader=interp,point meta=\thisrow{density}] table {
    X Y Z density
    0 1 0 0
    1 0 0 0
    0.33333333 0.3333333333 0.3333333333 1
    %
    1 0 0 0
    0 0 1 0
    0.33333333 0.3333333333 0.3333333333 1
    %
    0 0 1 0
    0 1 0 0
    0.33333333 0.3333333333 0.3333333333 1
    };

  \addplot3[scatter,only marks, point meta=\thisrow{density}] table {dirichlet.dat};
\end{ternaryaxis}
\end{tikzpicture}
\end{document}

I have minimized the example a bit; feel free to re-add the nodes near coords stuff etc.