![pgfmathparse,foreach 在轴内不起作用](https://linux22.com/image/321090/pgfmathparse%EF%BC%8Cforeach%20%E5%9C%A8%E8%BD%B4%E5%86%85%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8.png)
我尝试在里面使用foreach
循环和。但它给了我一个错误。 pgfmathparse
axis
\documentclass[10pt,border=0pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
% Default options for plots
\pgfplotsset{%
compat=1.12,
compat/show suggested version=false,
%tick label style={font=\footnotesize},
%label style={font=\footnotesize},
legend style={%
%draw=none,
font=\footnotesize,
at={(0.5,1.0)}, % position of legend
yshift=3pt,
anchor=south,
legend columns=2
legend cell align=left,
inner xsep=2pt,
/tikz/column 2/.style={%
column sep=4pt, % http://tex.stackexchange.com/questions/80215/two-columns-in-legend-in-pgfplots
},
/tikz/column 1/.style={%
column sep=2pt,
},
/tikz/column 3/.style={%
column sep=2pt,
},
nodes={inner xsep=0pt,inner ysep=0pt,text depth=0.15em},
%nodes={inner xsep=2pt,inner ysep=0pt,text depth=0.15em}, % this added a space after the last text
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
width=20cm,
ybar stacked,
bar width=1cm,
ylabel={YLABEL},
symbolic x coords={a,b,c,d,e,f},
ticks=none,
%xtick={a,b,c,d,e,f}, % adds legend at ticks
%enlarge x limits=0.5,
%enlarge x limits,
enlarge y limits=0.2,
nodes near coords,
font=\footnotesize,
grid=major,
after end axis/.code={% http://tex.stackexchange.com/questions/55261/labeling-the-axes-of-my-plot
\node[below] at (xticklabel cs:0.25) {XXX};
\node[below] at (xticklabel cs:0.75) {YYY};
}
]
\addplot coordinates {(a,5.42) (b,13.4) (c,10.69) (d,10.81) (e,30.31) (f,22.53)};
\addplot coordinates {(a,0.72) (b,17.87) (c,5) (d,1.40) (e,12.39) (f,5.17)};
\legend {Downlink,Uplink};
\foreach \x/\y in {1/a,2/b,3/c,4/a,5/b,6/c)} {%
\pgfmathsetmacro{\coor}{\x*(0.5/6)}
\node[draw,anchor=south west] at (rel axis cs:\coor,0.8) {\y};
};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
pgfplots
手册第 8.1 节中描述了此情况的原因实用程序命令。我引用相关内容:
请记住,在
axis
环境中,所有循环构造(包括自定义循环\foreach
和\pgfplotsforeachungrouped
)都需要小心处理:循环参数只能在立即求值的地方使用;但pgfplots
会推迟许多宏的求值。例如,要循环某些东西并生成形式为的轴描述\node at (axis cs:\i,0.5)....
,循环宏\i
将在中求值\end{axis}
——但此时,循环结束并且其值丢失。处理此类应用程序的正确方法是显式扩展循环变量。例如:\pgfplotsforeachungrouped \i/\j in { 1 / a, 2 / b, 3 / c }{ \edef\temp{\noexpand\node at (axis cs: \i,0.5) {\j};} % \show\temp % lets TeX show you what \temp contains \temp }
该示例生成三个循环迭代:
\i=1
,\j=a
;然后\i=2
,j=b
;然后\i=3
,\j=c
。在循环体内部,它使用“扩展定义” \edef 扩展它们并将结果分配给宏。结果不再包含 或\i
(\j
因为它们已被扩展)。然后,它调用结果宏。有关 TEX 命令\edef
和扩展控制的详细信息可以在 随附的文档 TeX-programming-notes.pdf 中找到pgfplots
。
因此使用上述结构循环就可以工作:
\documentclass[10pt,border=0pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
% Default options for plots
\pgfplotsset{%
compat=1.12,
compat/show suggested version=false,
%tick label style={font=\footnotesize},
%label style={font=\footnotesize},
legend style={%
%draw=none,
font=\footnotesize,
at={(0.5,1.0)}, % position of legend
yshift=3pt,
anchor=south,
legend columns=2
legend cell align=left,
inner xsep=2pt,
/tikz/column 2/.style={%
column sep=4pt, % http://tex.stackexchange.com/questions/80215/two-columns-in-legend-in-pgfplots
},
/tikz/column 1/.style={%
column sep=2pt,
},
/tikz/column 3/.style={%
column sep=2pt,
},
nodes={inner xsep=0pt,inner ysep=0pt,text depth=0.15em},
%nodes={inner xsep=2pt,inner ysep=0pt,text depth=0.15em}, % this added a space after the last text
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
width=20cm,
ybar stacked,
bar width=1cm,
ylabel={YLABEL},
symbolic x coords={a,b,c,d,e,f},
ticks=none,
%xtick={a,b,c,d,e,f}, % adds legend at ticks
%enlarge x limits=0.5,
%enlarge x limits,
enlarge y limits=0.2,
nodes near coords,
font=\footnotesize,
grid=major,
after end axis/.code={% http://tex.stackexchange.com/questions/55261/labeling-the-axes-of-my-plot
\node[below] at (xticklabel cs:0.25) {XXX};
\node[below] at (xticklabel cs:0.75) {YYY};
}
]
\addplot coordinates {(a,5.42) (b,13.4) (c,10.69) (d,10.81) (e,30.31) (f,22.53)};
\addplot coordinates {(a,0.72) (b,17.87) (c,5) (d,1.40) (e,12.39) (f,5.17)};
\legend {Downlink,Uplink};
\foreach \x/\y in {1/a,2/b,3/c,4/a,5/b,6/c} {%
\pgfmathsetmacro{\coor}{\x*(0.5/6)}
% \node[draw,anchor=south west] at (rel axis cs:\coor,0.8) {\y};
\edef\temp{\noexpand\node at (rel axis cs: \coor,0.8) {\y};}
\temp
};
\end{axis}
\end{tikzpicture}
\end{document}