1

Related question (among others): Generic point labels in pgfplots

I wish to set the labels of points in my graph to some given string values (ideally defined by a previous macro). Apparently there are tricks involving the point meta key but if I understand correctly those can only be set to numerical values.

The following returns the labels "Point number 1", "Point number 2", etc. i.e. text expressions but that is limited to expressions that one can evaluate from the \coordindex macro.

Note that a \newcommand{\mynames} such that \mynames{1} expands to "First", \mynames{2} to "Second", etc. should be sufficient but I do not know how to code that.

\documentclass{standalone}
\usepackage{pgfplots}

%\pgfplotsset{compat = 1.14}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}
    \newcommand{\samplepoints}{0,2,4}
    \addplot[samples at={\samplepoints},
    nodes near coords={\pgfmathtruncatemacro\nValue{\coordindex+1} Point number \nValue},
%   nodes near coords/.expanded={First, Second, Third}, % I wish this worked
    ] {x^2};
    \end{axis}
    \end{tikzpicture}
\end{document}
1

If there is a solution to do that I don't know it. But why don't you simply create in inline table with the \samplepoints and directly give them the meta value which than can easily be plotted?

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot+ [
                point meta=explicit symbolic,
                nodes near coords,
            ] table [
                x=x,
                y expr=x^2,
                meta=meta,
            ] {
                % (use empty brackets for "no entry")
                x   meta
                0   First
                2   Second
                3   {}
                4   Third
            };
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

  • You can also add an empty value by means of curly braces, i.e. 3 {} – Christian Feuersänger Oct 23 '16 at 13:01
  • Of course ... If have edited my answer accordingly. Many thanks for the hint. – Stefan Pinnow Oct 27 '16 at 14:46

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.