使用以下条形图
可以看到标签实际上并没有位于条形图的中央,而是位于右侧。我怎样才能将值居中?对齐必须像这样(旋转并位于条形图上方),因为其他数据值不在最小示例中,如下所示:
\documentclass[11pt,a4paper,german,notitlepage]{report}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{figure}
\pgfplotstableread[col sep=semicolon,header=false]{
Entry;0.008
}\data
\pgfplotsset{
percentage plot/.style={
point meta=explicit,
every node near coord/.append style={
align=center,
text width=1cm
},
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber[fixed,precision=3]{\originalvalue}$\,\%$\\
\fi},
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
ymax=0.5,
enlarge y limits={upper,value=0},
visualization depends on={y \as \originalvalue}
},
percentage series/.style={
table/y expr=\thisrow{#1},table/meta=#1
}
}
\begin{tikzpicture}
\begin{axis}[
width=0.95\textwidth,
ylabel=y axis,
xlabel=x axis,
percentage plot,
ybar,
every node near coord/.append style={rotate=90, anchor=west, font=\scriptsize},
symbolic x coords={Entry,
},
x tick label style={font=\normalsize, rotate=90, anchor=east},
enlarge y limits={upper,value=0.2},
xtick=data
]
\addplot table [percentage series=1] {\data};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
编辑:更正代码。现在应该可以编译了。
答案1
评论太长了
当我发表评论时every node near coord/.append style
(如在问题下方评论)
% used PGFPlots v1.18.1
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
percentage plot/.style={
point meta=explicit,
% every node near coord/.append style={
% align=center,
%% text width=1cm,
% },
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber[fixed,precision=3]{\originalvalue}$\,\%$\\
\fi%
},
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
ymax=0.5,
enlarge y limits={upper,value=0},
visualization depends on={y \as \originalvalue}
},
percentage series/.style={
table/y expr=\thisrow{#1},table/meta=#1
},
}
\pgfplotstableread[col sep=semicolon,header=false]{
Entry;0.008
}\data
\begin{axis}[
width=0.95\textwidth,
ylabel=y axis,
xlabel=x axis,
percentage plot,
ybar,
every node near coord/.append style={rotate=90, anchor=west, font=\scriptsize},
symbolic x coords={Entry},
x tick label style={font=\normalsize, rotate=90, anchor=east},
enlarge y limits={upper,value=0.2},
xtick=data,
]
\addplot table [percentage series=1] {\data};
\end{axis}
\end{tikzpicture}
\end{document}
我明白了
这看起来就像您期望的那样。这适用于 pdfLaTeX 和 LuaLaTeX。
答案2
取消此代码
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
percentage plot/.style={
point meta=explicit,
% every node near coord/.append style={
% align=center,
%% text width=1cm,
% },
nodes near coords={
\pgfmathtruncatemacro\iszero{\originalvalue==0}
\ifnum\iszero=0
\pgfmathprintnumber[fixed,precision=3]{\originalvalue}$\,\%$\\
\fi%
},
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
ymin=0,
ymax=0.5,
enlarge y limits={upper,value=0},
visualization depends on={y \as \originalvalue}
},
percentage series/.style={
table/y expr=\thisrow{#1},table/meta=#1
},
}
\pgfplotstableread[col sep=semicolon,header=false]{
Entry;0.008
Entry2;0.4
Entry3;0.3
}\data
\begin{axis}[
width=0.95\textwidth,
ylabel=y axis,
xlabel=x axis,
percentage plot,
ybar,
every node near coord/.append style={rotate=90, anchor=west, font=\scriptsize},
symbolic x coords={Entry, Entry2, Entry3},
x tick label style={font=\normalsize, rotate=90, anchor=east},
enlarge y limits={upper,value=0.2},
xtick=data,
]
\addplot table [percentage series=1] {\data};
\end{axis}
\end{tikzpicture}
\end{document}
请注意,我使用的是 \pgfplotsset{compat=1.17},因为 1.18 不起作用(在我的配置中。目前还不知道为什么)。我认为它不是真正居中的。也许它可能有点挑剔。您可以在条目 3 上看到它。但目前我是这样使用它的,它可以保持这样。