4

I have the following scatter plot done with pgfplots:

enter image description here

The meta value of each point represents its "importance", hence red points are more important of blue ones. The problem is that red and orange points are buried among all the blue points, especially around (0,0). Is it possible to draw points with a z-order proportional to their meta value, i.e., having red and orange points in front of the blue ones?

Here's my code:

\documentclass{article}

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

\begin{document}

\pgfplotsset{
ytick={0,0.2,...,1},
xtick={0,0.2,...,1},
xmajorgrids=true,
ymajorgrids=true,
major grid style={dashed},
}

\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,colorbar horizontal]
        \addplot[scatter,
                 only marks,
                 mark size=1pt,
                 point meta=explicit,
                 point meta min={0},
                 point meta max={100}] table [col sep=comma, 
                                              x index=0, 
                                              y index=1, 
                                              meta index=2] {csv/scatter.csv};
    \end{axis}
\end{tikzpicture}

\end{document}

An example scatter.csv:

0.000000,0.000000,53
0.000000,0.000213,1
0.000000,0.007653,1
0.000000,0.017226,1
0.000000,0.030485,1
0.000000,0.032558,1
0.000000,0.034453,1
0.000000,0.039656,1
0.000000,0.050874,1
0.000000,0.061787,1
0.000000,0.065450,1
0.000000,0.151315,1
0.000000,0.184418,1
0.000000,0.201284,1
0.000000,0.217143,1
0.000000,0.250000,4
0.000000,0.255159,1
0.000000,0.257401,1
0.000000,0.268999,1
0.000000,0.331126,1
0.000000,0.333333,3
  • 1
    Can you post your code so we can work on it? – percusse Nov 4 '14 at 14:24
  • I've added my code – Filippo Bistaffa Nov 4 '14 at 14:42
  • Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Benedikt Bauer Nov 4 '14 at 14:58
  • Yes you are right... done – Filippo Bistaffa Nov 4 '14 at 15:06
  • How about sorting the data based on third column (using e.g., datatool) and plot in the same fashion? – Pouya Nov 4 '14 at 15:10
5

As Pouya suggested you can order the data. He proposed datatool but I'll use \pgfplotstablesort command from \pgfplotstable package.

I've changed some of your data to make more evident how the code works:

\documentclass{article}
\usepackage{pgfplotstable} 
\usepackage{pgfplots}   

\begin{document}
\pgfplotstableread[col sep=comma]{
X,Y, C
0.000000,0.000000,53
0.000000,0.000213,1
0.000000,0.007653,1
0.000000,0.017226,1
0.000000,0.030485,1
0.000000,0.032558,60
0.000000,0.034453,1
0.000000,0.039656,1
0.000000,0.050874,1
0.000000,0.061787,45
0.000000,0.065450,1
0.000000,0.151315,32
0.000000,0.184418,1
0.000000,0.201284,1
0.000000,0.217143,1
0.000000,0.250000,4
0.000000,0.255159,1
0.000000,0.257401,1
0.000000,0.268999,80
0.000000,0.331126,1
0.000000,0.333333,3
}\data

%get the data and sort by column 'C'
\pgfplotstablesort[sort key={C}]{\sorted}{\data} 

%Comment out next command to type the ordered table
%\pgfplotstabletypeset{\sorted}

\pgfplotsset{
ytick={0,0.2,...,1},
xtick={0,0.2,...,1},
xmajorgrids=true,
ymajorgrids=true,
major grid style={dashed},
}

\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,colorbar horizontal]
        \addplot[scatter,
                 only marks,
                 mark size=1pt,
                 point meta=explicit,
                 point meta min={0},
                 point meta max={100}] table [col sep=comma, 
                                              x index=0, 
                                              y index=1, 
                                              meta index=2] {\sorted};
    \end{axis}
\end{tikzpicture}
\end{document}

A little detail of the resulting figure looks like

enter image description here

  • Confession: I proposed datatool because I didn't know about \pgfplotstablesort :D – Pouya Nov 4 '14 at 17:30
4

You can (ab)use the builtin method of z buffer=sort: why not using the color data as third dimension combined with view={0}{90} (which means "view from top", i.e. from z = + infinity)?

The key z buffer=sort will sort keys such that those which are "far away" from the viewer (i.e. with small z value in our case) will be on the bottom and those which are "close" to the viewer (i.e. large z value in this picture) will be drawn on top of them.

Example:

\documentclass{standalone}

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

\begin{document}

\pgfplotsset{
ytick={0,0.2,...,1},
xtick={0,0.2,...,1},
xmajorgrids=true,
ymajorgrids=true,
major grid style={dashed},
}

\begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=1,ymin=0,ymax=1,colorbar horizontal,view={0}{90}]
        \addplot3[scatter,
                 only marks,
                 mark size=5pt,
                 z buffer=sort,
                 point meta=z,
                 point meta min={0},
                 point meta max={100}] 
         table[col sep=comma]
    {
0.000000,0.000000,53
0.000000,0.000213,1
0.000000,0.007653,1
0.000000,0.017226,1
0.000000,0.030485,1
0.000000,0.032558,1
0.000000,0.034453,1
0.000000,0.039656,1
0.000000,0.050874,1
0.000000,0.061787,1
0.000000,0.065450,1
0.000000,0.151315,1
0.000000,0.184418,1
0.000000,0.201284,1
0.000000,0.217143,1
0.000000,0.250000,4
0.000000,0.255159,1
0.000000,0.257401,1
0.000000,0.268999,1
0.000000,0.331126,1
0.000000,0.333333,3
0.5,0.5,100
0.51,0.51,0
0.49,0.51,50
    };
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I added three points in the middle and increased the mark size to verify that I sorted in the correct order. Turn off z buffer=sort to see that they are drawn in the order of appearance (i.e. wrong). The z buffer implicitly results in the correct ordering, and it has minimal overhead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.