我正在用 PGF 制作条形图,用起来很愉快。唯一的问题是我无法缩小用于指示与每个条形相关的值的字体大小。考虑以下 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}
这给出了一个很棒的图。问题是,条形标签太大且重叠。我尝试使用 更改此设置every node near coord/.style={font=\footnotesize},
。但是,标签全部出现在彼此上方,不再位于相应的条形上方。我怎样才能将它们保持在正确的位置?这是一个错误吗?
答案1
我将我的评论转为答案。
您正在使用every node near coord/.style={font=\tiny},
。在这种情况下,所有预定义设置都将丢失。pgfkeys
有一个特殊的处理程序,用于向名为的定义样式添加额外的选项append style
。根据此信息,您可以使用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}
答案2
如果您使用 ConTeXt,请使用以下\switchtobodyfont
命令:
every node near coord/.style={font={\switchtobodyfont[8pt]}}