11

I am preparing a bar chart in PGF and it is a pleasure to work with. The only problem is that I have trouble reducing the font size used to indicate the value associated to each bar. Consider the following MWE:

\documentclass{minimal}

\usepackage{pgf,tikz}
\pgfplotsset{compat=1.5.1}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    %small,
    ybar,%=8pt, % configures ‘bar shift’
    enlargelimits=0.15,
    ylabel={index},
    symbolic x coords={1982, 1990, 1999, 2006},
    %xtick=data,
    %tick label style={font=\footnotesize},
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    nodes near coords,
    %every node near coord/.style={font=\footnotesize},
    nodes near coords align={vertical},
    ]
\addplot coordinates {(1982, 1.78) (1990, 1.71) (1999, 1.68) (2006, 1.62)};
\addplot coordinates {(1982,  1.70) (1990, 1.62) (1999, 1.59) (2006, 1.64)};
\addplot coordinates {(1982, 2.04) (1990, 1.96) (1999, 1.95) (2006, 1.91)};
\legend{USA, Netherlands, {(West-)Germany}}

\end{axis}
\end{tikzpicture}

\end{document}

Plot resulting from MWE.

This gives a wonderful plot. Probem is, the bar labels are too large and overlap. I tried changing this using every node near coord/.style={font=\footnotesize},. However, then the labels all appear above each other and are not above the corresponding bars anymore. How can I keep them at their right places? Is this a bug?

  • 1
    You can try every node near coord/.append style={font=\tiny}, – Marco Daniel Apr 7 '12 at 17:03
  • I still gotta learn a lot about TikZ... That did the trick! Thanks! – Ingo Apr 7 '12 at 17:14
16

I switch my comment to an answer.

You are using every node near coord/.style={font=\tiny},. In this case all predefined settings will be lost. pgfkeys has a special handler to add an extra option to a defined style named append style. Based on this information you can use every node near coord/.append style={font=\tiny}.

\documentclass{article}

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    %small,
    ybar,%=8pt, % configures ‘bar shift’
    enlargelimits=0.15,
    ylabel={index},
    symbolic x coords={1982, 1990, 1999, 2006},
    %xtick=data,
    %tick label style={font=\footnotesize},
    legend style={at={(0.5,-0.15)},
    anchor=north,legend columns=-1},
    nodes near coords,
    every node near coord/.append style={font=\tiny},
   nodes near coords align={vertical},
    ]
\addplot coordinates {(1982, 1.78) (1990, 1.71) (1999, 1.68) (2006, 1.62)};
\addplot coordinates {(1982,  1.70) (1990, 1.62) (1999, 1.59) (2006, 1.64)};
\addplot coordinates {(1982, 2.04) (1990, 1.96) (1999, 1.95) (2006, 1.91)};
\legend{USA, Netherlands, {(West-)Germany}}

\end{axis}
\end{tikzpicture}

\end{document}

1

And if you're using ConTeXt, use the \switchtobodyfont command:

every node near coord/.style={font={\switchtobodyfont[8pt]}}

  • Welcome to TeX.SE. It would be helpful if you posted an immediately-compilable full example, rather than just a code fragment. – Mico Apr 26 '17 at 0:19

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.