0

I am trying to plot a ternary heatmap using the ternaryaxis environment. This is supposed to plot a variable over three factors that go from 0 to 100. The code I am using is the following:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12} 
\usepgfplotslibrary{ternary}

\begin{document}
    \small
    \begin{tikzpicture}
        \begin{ternaryaxis}[
            axis on top,
            xlabel=x,
            ylabel=y,
            zlabel=z,
            colorbar,
            ternary limits relative,
            xmin=0,
            xmax=100,
            ymin=0,
            ymax=100,
            zmin=0,
            zmax=100,
            width=0.4\textwidth,
            height=0.4\textwidth,]
            
            \addplot3[
            patch,
            shader=interp,
            point meta=\thisrow{cData}] table [x=xData, y=yData, z=zData, col sep = comma]{data.csv};
        \end{ternaryaxis}
    \end{tikzpicture}
\end{document}

However, instead of a heatmap I get weird lines, as can be seen here: enter image description here

I think its a problem with the way my data is structured, but so far couldn't figure out why. My csv file looks like this:

xData,yData,zData,cData
0,0,100,12.45
0,5,95,12.21666667
0,10,90,12.1
0,15,85,12.05
0,20,80,12.03333333
0,25,75,12.08333333
0,30,70,12.16666667
0,35,65,12.28333333
0,40,60,12.43333333
0,45,55,12.6
0,50,50,12.81666667
0,55,45,13.03333333
0,60,40,13.31666667
0,65,35,13.63333333
0,70,30,13.95
0,75,25,14.3
0,80,20,14.65
0,85,15,15.01666667
0,90,10,15.41666667
0,95,5,15.83333333
0,100,0,16.28333333
5,0,95,12.4
5,5,90,12.16666667
5,10,85,12.03333333
5,15,80,11.96666667
5,20,75,11.96666667
5,25,70,12.01666667
5,30,65,12.1
5,35,60,12.21666667
5,40,55,12.36666667
5,45,50,12.53333333
5,50,45,12.73333333
5,55,40,12.96666667
5,60,35,13.25
5,65,30,13.55
5,70,25,13.88333333
5,75,20,14.21666667
5,80,15,14.56666667
5,85,10,14.93333333
5,90,5,15.33333333
5,95,0,15.73333333
10,0,90,12.35
10,5,85,12.13333333
10,10,80,11.98333333
10,15,75,11.9
10,20,70,11.9
10,25,65,11.95
10,30,60,12.03333333
10,35,55,12.15
10,40,50,12.28333333
10,45,45,12.45
10,50,40,12.66666667
10,55,35,12.88333333
10,60,30,13.18333333
10,65,25,13.48333333
10,70,20,13.8
10,75,15,14.13333333
10,80,10,14.48333333
10,85,5,14.85
10,90,0,15.23333333
15,0,85,12.31666667
15,5,80,12.1
15,10,75,11.91666667
15,15,70,11.83333333
15,20,65,11.83333333
15,25,60,11.86666667
15,30,55,11.95
15,35,50,12.06666667
15,40,45,12.2
15,45,40,12.38333333
15,50,35,12.58333333
15,55,30,12.81666667
15,60,25,13.1
15,65,20,13.4
15,70,15,13.71666667
15,75,10,14.03333333
15,80,5,14.38333333
15,85,0,14.75
20,0,80,12.28333333
20,5,75,12.08333333
20,10,70,11.88333333
20,15,65,11.78333333
20,20,60,11.76666667
20,25,55,11.8
20,30,50,11.88333333
20,35,45,11.98333333
20,40,40,12.13333333
20,45,35,12.3
20,50,30,12.5
20,55,25,12.73333333
20,60,20,13.01666667
20,65,15,13.31666667
20,70,10,13.63333333
20,75,5,13.95
20,80,0,14.28333333
25,0,75,12.26666667
25,5,70,12.05
25,10,65,11.88333333
25,15,60,11.75
25,20,55,11.7
25,25,50,11.73333333
25,30,45,11.8
25,35,40,11.9
25,40,35,12.05
25,45,30,12.21666667
25,50,25,12.4
25,55,20,12.65
25,60,15,12.93333333
25,65,10,13.23333333
25,70,5,13.53333333
25,75,0,13.85
30,0,70,12.25
30,5,65,12.05
30,10,60,11.88333333
30,15,55,11.71666667
30,20,50,11.66666667
30,25,45,11.66666667
30,30,40,11.73333333
30,35,35,11.81666667
30,40,30,11.95
30,45,25,12.11666667
30,50,20,12.31666667
30,55,15,12.56666667
30,60,10,12.83333333
30,65,5,13.13333333
30,70,0,13.43333333
35,0,65,12.26666667
35,5,60,12.05
35,10,55,11.88333333
35,15,50,11.73333333
35,20,45,11.63333333
35,25,40,11.61666667
35,30,35,11.66666667
35,35,30,11.75
35,40,25,11.88333333
35,45,20,12.03333333
35,50,15,12.21666667
35,55,10,12.46666667
35,60,5,12.75
35,65,0,13.03333333
40,0,60,12.31666667
40,5,55,12.08333333
40,10,50,11.91666667
40,15,45,11.75
40,20,40,11.61666667
40,25,35,11.58333333
40,30,30,11.61666667
40,35,25,11.68333333
40,40,20,11.8
40,45,15,11.95
40,50,10,12.13333333
40,55,5,12.36666667
40,60,0,12.65
45,0,55,12.4
45,5,50,12.2
45,10,45,11.98333333
45,15,40,11.8
45,20,35,11.63333333
45,25,30,11.56666667
45,30,25,11.56666667
45,35,20,11.63333333
45,40,15,11.73333333
45,45,10,11.86666667
45,50,5,12.03333333
45,55,0,12.28333333
50,0,50,12.48333333
50,5,45,12.28333333
50,10,40,12.06666667
50,15,35,11.9
50,20,30,11.73333333
50,25,25,11.6
50,30,20,11.56666667
50,35,15,11.6
50,40,10,11.68333333
50,45,5,11.81666667
50,50,0,11.96666667
55,0,45,12.56666667
55,5,40,12.36666667
55,10,35,12.18333333
55,15,30,11.98333333
55,20,25,11.81666667
55,25,20,11.66666667
55,30,15,11.6
55,35,10,11.6
55,40,5,11.66666667
55,45,0,11.78333333
60,0,40,12.63333333
60,5,35,12.45
60,10,30,12.26666667
60,15,25,12.1
60,20,20,11.91666667
60,25,15,11.76666667
60,30,10,11.63333333
60,35,5,11.61666667
60,40,0,11.65
65,0,35,12.71666667
65,5,30,12.53333333
65,10,25,12.35
65,15,20,12.2
65,20,15,12.03333333
65,25,10,11.88333333
65,30,5,11.71666667
65,35,0,11.63333333
70,0,30,12.8
70,5,25,12.61666667
70,10,20,12.45
70,15,15,12.28333333
70,20,10,12.13333333
70,25,5,11.98333333
70,30,0,11.83333333
75,0,25,12.88333333
75,5,20,12.7
75,10,15,12.53333333
75,15,10,12.38333333
75,20,5,12.21666667
75,25,0,12.06666667
80,0,20,13
80,5,15,12.81666667
80,10,10,12.61666667
80,15,5,12.46666667
80,20,0,12.31666667
85,0,15,13.06666667
85,5,10,12.9
85,10,5,12.75
85,15,0,12.55
90,0,10,13.15
90,5,5,12.98333333
90,10,0,12.81666667
95,0,5,13.21666667
95,5,0,13.06666667
100,0,0,13.3

Can anybody give me some tips on how to fix this?

5
  • 1
    Welcome to TeX.SE! Please make your code snippet be compilable, then we do not have to guess what you are doing!
    – Mensch
    Feb 20 at 16:52
  • Thank you! I have edited the post accordingly
    – Kaza123
    Feb 20 at 16:57
  • 1
    patch plots expect data in a series of triangles. All your data lies on a line (that zigzags). You need to explain what your data actually represent and what kind of plot you want/expect. Feb 20 at 20:31
  • x, y and z represent ratios of 3 variables (that sum up to 100) and i want to plot the resulting output which i called c here. I pretty much want exactly the kind of figure that is plotted in this thread: tex.stackexchange.com/questions/276496/… this is also where i got the code from
    – Kaza123
    Feb 20 at 21:30
  • @Kaza123, if you can copy code, you can also refactor it step by step, like so: 1) copy said code+compile. 2) replace those two tupels by some of your data+compile. 3) add more, generalize more+compile ... n-1) arrive at a presentation, which is close to your csv structure+compile. n) move data into your csv+compile. // Think of doing free-climbing with security measures taken ... that's the process ... and it will help you bypassing your current "fatal" error ....
    – MS-SPO
    Feb 21 at 9:01

1 Answer 1

1

Your data is formatted wrong. Either you can make the triangles needed for a patch plot or you can complete a mesh needed for a surf plot - e.g. by repeating the last/first datapoint in a row.

You might also be happy with a colored scatter plot. Here I have created a custom hexagon mark to fill the surface.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{ternary}
\begin{document}
\begin{tikzpicture}
\pgfdeclareplotmark{hexagon}{
  \pgfpathmoveto{\pgfpointpolar{30}{\pgfplotmarksize}}
  \pgfpathlineto{\pgfpointpolar{90}{\pgfplotmarksize}}
  \pgfpathlineto{\pgfpointpolar{150}{\pgfplotmarksize}}
  \pgfpathlineto{\pgfpointpolar{210}{\pgfplotmarksize}}
  \pgfpathlineto{\pgfpointpolar{270}{\pgfplotmarksize}}
  \pgfpathlineto{\pgfpointpolar{330}{\pgfplotmarksize}}
  \pgfpathclose
  \pgfusepathqfill
}
\begin{ternaryaxis}[
xmin=0, xmax=100,
ymin=0, ymax=100,
zmin=0, zmax=100,
axis on top,
xlabel=x, ylabel=y, zlabel=z,
colorbar,
clip marker paths=true,
]
\addplot3[
scatter, only marks, 
mark=hexagon, mark size=5pt,
point meta=\thisrow{cData},
] table [x=xData, y=yData, col sep=comma]{data.csv};
\end{ternaryaxis}
\end{tikzpicture}
\end{document}

mark=*, mark size=4pt

Triangle graph filled with shaded circles

mark=hexagon, mark size=5pt

Triangle graph filled with shaded hexagons

mark=hexagon, mark size=6pt

Triangle graph filled with shaded hexagons

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .