我需要一些关于此图的帮助:
- 我想合并“meta=”部分中的两个条目。它应该包含 {y*100} 值和标签值。是否可以以某种方式添加它们?最后应该有百分比值和每个条形上方的星号。
- 我想将星星和误差线的颜色改为黑色。
- 是否可以将 y 轴稍微延长一点,以便它不以水平标记线结束?
。
\documentclass{article}
\usepackage[libertine]{newtxmath}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.11}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplotstable}
\usetikzlibrary{positioning}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{arrows}
\usetikzlibrary{patterns}
\begin{document}
\begin{filecontents}{data1.dat}
X Y Y_error Label
a 1 0 \\
b .155 .038 $\star$
c 0.80 0.019 $\star$
d 0.54 0.027 \\
\end{filecontents}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
bar width=1cm, % Width of the bar
x=2cm, % Distance between the centers of the bars
enlarge x limits={abs=1cm}, % The distance between the center of the first bar and the left edge
enlarge y limits=false,
symbolic x coords={a,b,c,d},
ymin=0,
xtick=data,
ybar,
axis x line*=bottom,
axis y line=left,
clip=false,
% point meta={y*100}, %y-Werte mal 100 für Prozent
point meta=explicit symbolic,
yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%} ,
nodes near coords,
nodes near coords align={vertical},
visualization depends on=abs(y)/y*(0.1+\thisrow{Y_error}) \as \myshift,
every node near coord/.append style={
anchor=center,shift={(0,transformdirectiony(\myshift))},
}]
\addplot+[draw=black, fill=white, error bars/.cd, y dir=both, y explicit, ]
table[x=X, y=Y, y error=Y_error, meta=Label,] {data1.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
您可以使用第二个\addplot+
命令来添加第二个元条目。
使用color=black
istead 仅draw=black
可获得黑星和误差线。
用于ymax=1.05
稍微放大 y 轴。
更新代码使用 Christian Feuersänger 的建议(参见评论):
\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{filecontents}
\begin{filecontents}{data1.dat}
X Y Y_error Label
a 1 0 \\
b .155 .038 $\star$
c 0.80 0.019 $\star$
d 0.54 0.027 \\
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
bar width=1cm,
x=2cm,
enlarge x limits={abs=1cm},
symbolic x coords={a,b,c,d},
ymin=0,
ymax=1.05,
xtick=data,
axis x line*=bottom,
axis y line=left,
clip=false,
yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%} ,
nodes near coords,
visualization depends on=abs(y)/y*(0.15+\thisrow{Y_error}) \as \myshift,
every node near coord/.append style={yshift=transformdirectiony(\myshift),inner sep=1pt},
]
\addplot+[
ybar,
color=black,
fill=white,
mark=none,
point meta=explicit symbolic,
every node near coord/.append style={anchor=south},
error bars/.cd,
y dir=both,
y explicit
]
table[x=X, y=Y, y error=Y_error, meta=Label,] {data1.dat};
\addplot+[
color=black,
only marks,
mark=none,
%%% as suggested by Christian Feuersänger (see comments):
nodes near coords={$\pgfmathprintnumber{\pgfplotspointmeta}\%$},
point meta=100*y,
every node near coord/.append style={anchor=north,/pgf/number format/.cd, fixed, fixed zerofill=true, precision=1}
%%%
]
table[x=X, y=Y,forget plot] {data1.dat};
\end{axis}
\end{tikzpicture}
\end{document}
原始代码(不再有效):
\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{filecontents}
\begin{filecontents}{data1.dat}
X Y Y_error Label
a 1 0 \\
b .155 .038 $\star$
c 0.80 0.019 $\star$
d 0.54 0.027 \\
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
bar width=1cm,
x=2cm,
enlarge x limits={abs=1cm},
symbolic x coords={a,b,c,d},
ymin=0,
ymax=1.05,
xtick=data,
axis x line*=bottom,
axis y line=left,
clip=false,
yticklabel={\pgfmathparse{\tick*100}\pgfmathprintnumber{\pgfmathresult}\%} ,
nodes near coords,
visualization depends on=abs(y)/y*(0.15+\thisrow{Y_error}) \as \myshift,
every node near coord/.append style={yshift=transformdirectiony(\myshift),inner sep=1pt},
]
\addplot+[
ybar,
color=black,
fill=white,
mark=none,
point meta=explicit symbolic,
every node near coord/.append style={anchor=south},
error bars/.cd,
y dir=both,
y explicit
]
table[x=X, y=Y, y error=Y_error, meta=Label,] {data1.dat};
\addplot+[
color=black,
only marks,
mark=none,
point meta={TeX code symbolic={\def\pgfplotspointmeta{\pgfmathresult\%}}},
every node near coord/.append style={anchor=north}
]
table[x=X, y=Y,forget plot] {data1.dat};
\end{axis}
\end{tikzpicture}
\end{document}