3

I have the following input file (sample.txt):

A 1 2
B 23 2
C 2 34
D 12 22
E 23 3
F 34 12

I want to create a scatter plot where the second column is the x-axis, the third column is the y-axis, and the first column is the label for each point.

However, I want to only label points that exist in a list L = [B,D,E]

The list which should be defined in the source code (i.e. not in a file), can be changed and should reflect in the plot.

I'm having a difficult time getting this done in PGF plots. Any ideas?

Here's my attempt so far

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}

\begin{tikzpicture}
\begin{axis}
    \addplot+ [mark = x, 
        only marks,
        visualization depends on={value \thisrowno{0}}
    ] table[x index = 1, y index = 2] {annot_no_align.out};

\end{axis}
\end{tikzpicture}
\end{document}

Note: as you can probably tell, I'm a beginner in PGFPlots.

alguru
  • 271

1 Answers1

3

So this is a combination of node near coords and some if-exercise.

one can find the definition of \pgfutil@in@ in pgfutil-common.tex. Also one can look up LaTeX's \includeonly and BEAMER's \includeonlyframes.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}

\makeatletter
\def\labelonly{BDF}
\def\labelcheck#1{
    \edef\pgfmarshal{\noexpand\pgfutil@in@{#1}{\labelonly}}
    \pgfmarshal
    \ifpgfutil@in@[#1]\fi
}
\makeatother

\begin{tikzpicture}
\begin{axis}
    \addplot[
        mark=*,
        only marks,
        point meta=explicit symbolic,
        nodes near coords={
            \labelcheck{\pgfplotspointmeta}
        }
    ]
        table[header=false,meta index=0,x index=1,y index=2]{
            A 1 2
            B 23 2
            C 2 34
            D 12 22
            E 23 3
            F 34 12
        };
\end{axis}
\end{tikzpicture}
\makeatletter

\end{document}

Update

Here is a better solution when you prefer CSV. I am using native \pgfkeys features so you probably already know what is dangerous and what is not. (e.g. comma is dangerous.)

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}

\pgfkeys{
    /prepare label/.style={
        /print label/\detokenize{#1}/.code={\ttfamily\detokenize{#1}}
    },
    /prepare label/.list={^_^,@_@,T_T}
}
\begin{tikzpicture}
\begin{axis}
    \addplot[
        mark=*,
        only marks,
        point meta=explicit symbolic,
        nodes near coords={
            \pgfkeys{/print label/\pgfplotspointmeta/.try}
        }
    ]
        table[header=false,meta index=0,x index=1,y index=2]{
            ^_^   1  2
            >_<  23  2
            @_@   2 34
            Q_Q  23  3
            T_T  12 22
            =_=  34 12
        };
\end{axis}
\end{tikzpicture}

\end{document}
Symbol 1
  • 36,855
  • This might help as well http://tex.stackexchange.com/questions/26870/check-if-a-string-contains-a-given-character. – Symbol 1 Sep 30 '16 at 13:41
  • Brilliant. I didn't know about pgfutil-common.tex. It'll take me a while to fully understand how your code works, but it seems to do what I want. I do have one final question: is it easy to support labels made up of several characters (including underscores)? i.e. instead of "A","B","C",...,"F", I'd like to have "First_Label", "Second_Label", ... – alguru Sep 30 '16 at 17:57
  • @alguru That is the reason why I mentioned \includeonly. This command eat a list of filenames and then whenever you issue a \inclulde{filename} it checks if the filename is in the list. – Symbol 1 Sep 30 '16 at 19:40
  • @alguru place check out the update. – Symbol 1 Sep 30 '16 at 20:21
  • Haha very nice ! – percusse Oct 01 '16 at 13:22
  • @Symbol1 Thanks that's just what I needed. I wouldn't have figured out in a million years. – alguru Oct 04 '16 at 23:53
  • Why in your first plot points A, C, E are not marked ? Is it possible to mark all the points? On a related note, when I add an extra column to the table and properly select columns, all markings disappear. Any idea why this can happen? – yarchik Feb 06 '18 at 08:42
  • @yarchik because \def\labelonly{BDF} defines the labels that will be shown. Would you like to ask a new question with a handy MWE, so potentially there are more people can help? (Also, [tag:pgfplots] is now 1.15 so something might be obsolete. – Symbol 1 Feb 07 '18 at 02:24