![pgfplots 中的新命令和宏扩展](https://linux22.com/image/472025/pgfplots%20%E4%B8%AD%E7%9A%84%E6%96%B0%E5%91%BD%E4%BB%A4%E5%92%8C%E5%AE%8F%E6%89%A9%E5%B1%95.png)
我想创建一个阴影误差线区域,例如误差线作为阴影区域为了避免在多个 pgfplots 图上复制粘贴相同的代码,我尝试创建一个可以自动执行此操作的新命令。
我成功地用下面的例子绘制了该区域,但是我遇到了宏扩展的问题:定义颜色的宏只在最后扩展,所以我的两个图具有相同的颜色。
我尝试了几种技术:使用\edef
、几种\expandafter
、使用key/.expanded
。但我总是得到相同的颜色。
有没有办法正确扩展我的 \fillcolor 宏?或者有更简洁的方法来做我想做的事情?
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\tikzset{test/.style={
draw/.expanded={#1},
fill/.expanded={#1!50}
}}
\pgfplotstableset{
y name/.estore in=\yname,
y err name/.estore in=\yerrname,
fcolor/.estore in=\fillcolor,
}
\newcommand{\adderrorzone}[2][]{
\pgfplotstableset{#1}
\addplot[test=\expandafter\fillcolor] table[#1, y=\yname] {#2};
\addlegendentryexpanded{\fillcolor}
\addplot[name path=upper2, draw=none, forget plot] table[y expr=(\thisrow{\yname} + \thisrow{\yerrname})] {#2};
\addplot[name path=lower2, draw=none, forget plot] table[y expr=(\thisrow{\yname} - \thisrow{\yerrname})] {#2};
\addplot[test=\fillcolor, semitransparent, forget plot, draw=none] fill between[of=upper2 and lower2];
}
\begin{document}
\pgfplotstableread{
x y yerr
0 1 .5
1 3 1
}\tableA
\pgfplotstableread{
x y yerr
0 4 .5
1 5 .2
}\tableB
\begin{tikzpicture}
\begin{axis}
\adderrorzone[x=x, y name=y, y err name=yerr, fcolor=blue] {\tableA};
\adderrorzone[x=x, y name=y, y err name=yerr, fcolor=red] {\tableB};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
看起来,无论如何操作,\fillcolor
它都未扩展地被存储起来。
也许有更好的方法,但是这个有效。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.14}
\usepgfplotslibrary{fillbetween}
\tikzset{
test/.style={
draw={#1},
fill={#1!50},
},
}
\pgfplotstableset{
y name/.estore in=\yname,
y err name/.estore in=\yerrname,
fcolor/.estore in=\fillcolor,
}
\newcommand{\adderrorzone}[2][]{
\pgfplotstableset{#1}
\expanded{\noexpand\addplot[test=\fillcolor]} table[#1, y=\yname] {#2};
\expanded{\noexpand\addlegendentryexpanded{\fillcolor}}
\addplot[
name path=upper2,
draw=none,
forget plot,
] table[y expr=(\thisrow{\yname} + \thisrow{\yerrname})] {#2};
\addplot[
name path=lower2,
draw=none,
forget plot
] table[y expr=(\thisrow{\yname} - \thisrow{\yerrname})] {#2};
\expanded{\noexpand\addplot[
test=\fillcolor,
semitransparent,
draw=none,
forget plot,
]} fill between[of=upper2 and lower2];
}
\begin{document}
\pgfplotstableread{
x y yerr
0 1 .5
1 3 1
}\tableA
\pgfplotstableread{
x y yerr
0 4 .5
1 5 .2
}\tableB
\begin{tikzpicture}
\begin{axis}
\adderrorzone[x=x, y name=y, y err name=yerr, fcolor=blue] {\tableA}
\adderrorzone[x=x, y name=y, y err name=yerr, fcolor=red] {\tableB}
\end{axis}
\end{tikzpicture}
\end{document}
您可能想要移动图例...
答案2
通过使用参数而不是命令来指定颜色。(肯定有更好的解决方案):
\adderrorzone[x=x, y name=y, y err name=yerr] {\tableA}{blue};
- pgfplots 的最新版本是 1.18
.expanded
没有必要
代码
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}%-<-- load tikz
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}%<-- last version
\usepgfplotslibrary{fillbetween}
\tikzset{
test/.style={
draw={#1},%<-- modif
fill={#1!50},%<-- modif
},
}
\pgfplotstableset{
y name/.estore in=\yname,
y err name/.estore in=\yerrname,
% fcolor/.estore in=#3,
}
\newcommand{\adderrorzone}[3][]{
\pgfplotstableset{#1}
\addplot[test=#3] table[#1, y=\yname] {#2};
\addlegendentryexpanded{#3}
\addplot[name path=upper2, draw=none, forget plot] table[y expr=(\thisrow{\yname} + \thisrow{\yerrname})] {#2};
\addplot[name path=lower2, draw=none, forget plot] table[y expr=(\thisrow{\yname} - \thisrow{\yerrname})] {#2};
\addplot[test=#3,semitransparent, forget plot, draw=none] fill between[of=upper2 and lower2];
}
\begin{document}
\pgfplotstableread{
x y yerr
0 1 .5
1 3 1
}\tableA
\pgfplotstableread{
x y yerr
0 4 .5
1 5 .2
}\tableB
\begin{tikzpicture}
\begin{axis}
\adderrorzone[x=x, y name=y, y err name=yerr] {\tableA}{blue};
\adderrorzone[x=x, y name=y, y err name=yerr] {\tableB}{red};
\end{axis}
\end{tikzpicture}
\end{document}