4

What I want to do is to add a vertical error bar (column ey in the table) on each point. I tried to add:

%error bars/.cd,
%y dir=both,
%y explicit,

But it didn't work.

\documentclass[a4paper,10pt]{scrartcl} 
\usepackage{pgfplots}
\begin{document}

\begin{figure}[t]
    \centering
    \begin{tikzpicture}[scale=0.7]
    \begin{axis}[
    %axis x line=center,
    xtick pos=left,
    ytick pos=left,
    axis y line*=left,
    axis x line*=left,
    ylabel near ticks,
    xlabel near ticks,
    xlabel=Requirement (\#sentences),
    ylabel=mean F1,
    %axis y line=none,
    %hide y axis,
    xmin=-50,xmax=320,
    ymin=40, ymax=100,
    xtick={0, 100, 200, 300},
    xticklabels={0, 100, 100 + 100, highres},
    %xticklabel style={rotate=20},
    ]

    \addplot[
    scatter/classes={a={blue}, b={red}, c={green}, d={black}},
    scatter,
    only marks,%
    %error bars/.cd,
    %y dir=both,
    %y explicit,
    scatter src=explicit symbolic,
    nodes near coords*={\Label},
    visualization depends on={value \thisrow{label} \as \Label},
    ]%
    table[meta=class, x=x, y=y, y error=ey]{
        x   y   ey  class   label
        0   72  3   a   model1
        100 77  2   b   model2
        200 63  5   c   model3
        300 89  7   d   model4
    };
    \end{axis}
    \end{tikzpicture}
    \caption{}
\end{figure}
\end{document}

enter image description here

Now I added statement plot [error bars/.cd, y dir = both, y explicit] and error bars are there but they're into the labels and I'll need to move the labels so that they're clear, any ideas?

\documentclass[a4paper,10pt]{scrartcl} 
\usepackage{pgfplots}
\begin{document}

\begin{figure}[t]
    \centering
    \begin{tikzpicture}[scale=0.7]
    \begin{axis}[
    %axis x line=center,
    xtick pos=left,
    ytick pos=left,
    axis y line*=left,
    axis x line*=left,
    ylabel near ticks,
    xlabel near ticks,
    xlabel=Requirement (\#sentences),
    ylabel=mean F1,
    %axis y line=none,
    %hide y axis,
    xmin=-50,xmax=320,
    ymin=40, ymax=100,
    xtick={0, 100, 200, 300},
    xticklabels={0, 100, 100 + 100, highres},
    %xticklabel style={rotate=20},
    ]

    \addplot[
    scatter/classes={a={blue}, b={red}, c={green}, d={black}},
    scatter,
    only marks,%
    %error bars/.cd,
    %y dir=both,
    %y explicit,
    scatter src=explicit symbolic,
    nodes near coords*={\Label},
    visualization depends on={value \thisrow{label} \as \Label},
    ]%
    plot [error bars/.cd, y dir = both, y explicit]
    table[meta=class, x=x, y=y, y error=ey]{
        x   y   ey  class   label
        0   72  3   a   model1
        100 77  2   b   model2
        200 63  5   c   model3
        300 89  7   d   model4
    };
    \end{axis}
    \end{tikzpicture}
    \caption{}
\end{figure}
\end{document}

enter image description here

  • I had to add plot [error bars/.cd, y dir = both, y explicit] before the table statement but now the error bars are into the labels, so I'll need to move the labels a little big left, any idea what I'll need to do? – Ash Nov 15 '18 at 3:46
1

Welcome to TeX.SE! I could more or less copy the tricks of this answer and it seems to work.

\documentclass[a4paper,10pt]{scrartcl} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{figure}[t]
    \centering
    \begin{tikzpicture}[scale=0.7]
    \begin{axis}[
    %axis x line=center,
    xtick pos=left,
    ytick pos=left,
    axis y line*=left,
    axis x line*=left,
    ylabel near ticks,
    xlabel near ticks,
    xlabel=Requirement (\#sentences),
    ylabel=mean F1,
    %axis y line=none,
    %hide y axis,
    xmin=-50,xmax=320,
    ymin=40, ymax=100,
    xtick={0, 100, 200, 300},
    xticklabels={0, 100, 100 + 100, highres},
    %xticklabel style={rotate=20},
    ]

    \addplot[
    scatter/classes={a={blue}, b={red}, c={green}, d={black}},
    scatter,
    only marks,%
    %error bars/.cd,
    %y dir=both,
    %y explicit,
    visualization depends on=\thisrow{ey} \as \myshift,
    every node near coord/.append style = {shift={(axis direction
    cs:0,\myshift)}},
    scatter src=explicit symbolic,
    nodes near coords*={\Label},
    visualization depends on={value \thisrow{label} \as \Label},
    ]%
    plot [error bars/.cd, y dir = both, y explicit]
    table[meta=class, x=x, y=y, y error=ey]{
        x   y   ey  class   label
        0   72  3   a   model1
        100 77  2   b   model2
        200 63  5   c   model3
        300 89  7   d   model4
    };
    \end{axis}
    \end{tikzpicture}
    \caption{}
\end{figure}
\end{document}

enter image description here

What might be more useful than the actual answer is how one can search and find such posts. All I did was a google picture search for site:tex.stackexchange.com error bars nodes near coords and clicked on promising pictures and tried to select one that appears reasonably elegant.

  • The search method site:tex.stackexchange.com query in Google image was amazing! Thanks a lot. – Ash Nov 18 '18 at 23:29

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.