4

I have this stacked ybar plot and having trouble to proper align the nodes near the coords. It works fine on all bar but the one with offset 65. I already tried \ifnum as @Jake suggested here: How can I move a single node in a tikz plot? This works, but throws me this error twice:

Extra \else. \end{axis}

When I add the \ifnum to my main document, I get even more of them. Any suggestions how to fix this, or why the label is missplaced in the first place?

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{migration.tsv}
index   Kategorie   data    offset
5   five    1573    6
6   six 270 7
7   seven   497 65
\end{filecontents*}
\begin{document}

\begin{tikzpicture}
     \pgfplotstableread{migration.tsv}\migrationtable 
     \pgfplotsset{%
             width=\textwidth,
             height=.4\textheight,
             every tick label/.append style={font=\footnotesize}, %
             every node near coord/.append style={font=\footnotesize}, %
             compat=1.13%
                 }%
      \begin{axis}[%
          ybar stacked,
          scale only axis,
          x tick label style={rotate=45,anchor=north east}, 
          xtick=data,
          every node near coord/.append style={%
              anchor=south}
              ]
      \addplot[draw=black,fill=blue!50!white] table[x=index,y=data] from \migrationtable;
      \addplot[draw=black,fill=red,nodes near coords] table[x=index,y=offset] from \migrationtable;
      \legend{values,offset}
      \end{axis}
\end{tikzpicture}
\end{document}

This is the \ifnum block I used:

 every node near coord/.append style={%
       yshift={%
         \ifnum\coordindex=2 2ex\else 0pt\fi
       }%
 }

Also tried \newcommand{\equals}{=} to wrap the = as @egreg pointed out in question that can't find anymore.

I guess this is somehow similar to Center nodes near coords in a stacked ybar plot but I couldn't figure out how to use this to solve my problem. enter image description here

6

Remove this style option every node near coord/.append style={anchor=south}, and replace it with the following options:

nodes near coords align={anchor=south},
nodes near coords bar offset=1

Now, even if you increase the offset in your table by random amounts, the nodes will always appear nicely on top, without the need for you to manually shift them.

Output

enter image description here

Code

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{compat=1.13}
\begin{filecontents*}{migration.tsv}
index   Kategorie   data    offset
5   five    1573    6
6   six 270 7
7   seven   497 65
\end{filecontents*}
\begin{document}

\begin{tikzpicture}
\pgfplotstableread{migration.tsv}\migrationtable 
\pgfplotsset{%
    width=\textwidth,
    height=.4\textheight,
    every tick label/.append style={font=\footnotesize}, %
    every node near coord/.append style={font=\footnotesize}, %
    compat=1.13%
}%
\begin{axis}[%
    ybar stacked,
    scale only axis,
    x tick label style={rotate=45,anchor=north east}, 
    xtick=data,
    nodes near coords align={anchor=south},
    nodes near coords bar offset=1
]
\addplot[draw=black,fill=blue!50!white] table[x=index,y=data] from \migrationtable;
\addplot[draw=black,fill=red,nodes near coords] table[x=index,y=offset] from \migrationtable;
\legend{values,offset}
\end{axis}
\end{tikzpicture}
\end{document}

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.