我正在尝试绘制一个包含多个数据行和误差带的图,如下所示:
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread[col sep=space]{
0 128 1.13137 128.058 2.26326 117.25 10.8282
0.261799 127.046 1.12715 130.626 2.28583 132.992 11.5322
0.523599 124.249 1.11467 126.131 2.24616 133.772 11.566
0.785398 119.799 1.09453 121.163 2.20148 124.264 11.1474
1.0472 114 1.06771 112.967 2.12572 113.615 10.6591
1.309 107.247 1.0356 105.166 2.05101 112.295 10.597
1.5708 100 1 98.8555 1.98852 82.6604 9.09178
1.8326 92.7531 0.963084 90.31 1.90063 86.6324 9.30765
2.0944 86 0.927362 88.2214 1.87853 99.0388 9.95182
2.35619 80.201 0.89555 79.8697 1.7874 85.4422 9.24349
2.61799 75.7513 0.870352 75.4327 1.73704 83.2534 9.12433
2.87979 72.9541 0.854132 72.8971 1.7076 63.5106 7.96935
3.14159 72 0.848528 73.2027 1.71117 69.531 8.33853
}\datatable
\tikzset{
error band/.style={fill=orange},
error band style/.style={
error band/.append style=#1
},
}
\pgfplotsset{
tick label style={scale=0.3},
line and fill/.style={legend image code/.code={%
\fill[##1,draw=none] (0mm,-1ex)--(0mm,1ex)--(6mm,1ex) -- (6mm,-1ex) \closedcycle;
\draw[##1] (0mm,0mm)--(6mm,0mm);
}
}
}
\newcommand{\errorband}[6][]{
\addplot [draw=none, stack plots=y, forget plot] table [
x={#3},
y expr=\thisrow{#4}-\thisrow{#5}
] {#2};
\addplot [fill=gray!40, stack plots=y, area legend, opacity=0.5, #1, draw=none] table [
x={#3},
y expr=2*\thisrow{#5}
] {#2} \closedcycle;
\addlegendimage{line and fill,#1}
\addlegendentry{#6}
\addplot [forget plot, stack plots=y,draw=none] table [x={#3}, y expr=-(\thisrow{#4}+\thisrow{#5})] {#2};
\addplot [thick] table [
x={#3},
y expr=\thisrow{#4},
] {#2};
}
\begin{tikzpicture}
\begin{axis}[
no markers,
width=.5\textwidth,
ylabel near ticks,
xlabel near ticks,
enlarge x limits=false,
xlabel=Observable,
ylabel=Events,
ytick=\empty,
xtick=\empty,
ymin=50,ymax=150,
legend cell align=left,
legend image post style={scale=.3},
legend style={draw=none,fill=none,align=left,nodes={scale=0.3, transform shape}},
]
\errorband[draw=black,fill=green!50!black ]{\datatable}{0}{5}{6}{Morphing output (Input Set B)}
\errorband[draw=black,fill=black!50!white ]{\datatable}{0}{3}{4}{Morphing output (Input Set A)}
\errorband[draw=black,fill=violet!50!white]{\datatable}{0}{1}{2}{True Template}
\end{axis}
\end{tikzpicture}
\end{document}
不幸的是,图例条目完全乱了。我希望图例图片显示一个填充区域,填充颜色为用于波段的颜色,并通过该区域显示一条线,线的线条样式为用于平均值的线条样式,但我真的不明白这里发生了什么,也不明白为什么它看起来这么乱……
有什么建议么?
答案1
你几乎做对了。三处小改动,我想这就是想要的结果,对吧?
- 替换
\closedcycle
为-- cycle
中legend image code
的 - 添加
forget plot
到每个\addplot
中\errorband
,并 - 移动
\addlegendimage
并在\addlegendentry
最后一个之后。\addplot
errorband
我认为我已经完成了所有重大更改。如果我遗漏了某些内容,您会在下面的代码中看到它,我还在其中注释了一些此示例中不需要的内容。
% used PGFPlots v1.14
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableread[col sep=space]{
0 128 1.13137 128.058 2.26326 117.25 10.8282
0.261799 127.046 1.12715 130.626 2.28583 132.992 11.5322
0.523599 124.249 1.11467 126.131 2.24616 133.772 11.566
0.785398 119.799 1.09453 121.163 2.20148 124.264 11.1474
1.0472 114 1.06771 112.967 2.12572 113.615 10.6591
1.309 107.247 1.0356 105.166 2.05101 112.295 10.597
1.5708 100 1 98.8555 1.98852 82.6604 9.09178
1.8326 92.7531 0.963084 90.31 1.90063 86.6324 9.30765
2.0944 86 0.927362 88.2214 1.87853 99.0388 9.95182
2.35619 80.201 0.89555 79.8697 1.7874 85.4422 9.24349
2.61799 75.7513 0.870352 75.4327 1.73704 83.2534 9.12433
2.87979 72.9541 0.854132 72.8971 1.7076 63.5106 7.96935
3.14159 72 0.848528 73.2027 1.71117 69.531 8.33853
}\datatable
% \tikzset{ % <-- not needed here
% error band/.style={fill=orange},
% error band style/.style={
% error band/.append style=#1
% },
% }
\pgfplotsset{
% tick label style={scale=0.3}, % <-- not needed
line and fill/.style={
legend image code/.code={%
% you have to use `-- cycle' instead of `\closedcycle'
% (`\closedcycle' appends the path after the last coordinate
% `-- (x of last coordinate,0)
% -- (xmin,0)
% -- (xmin,y of first coordinate)'
% which isn't what you wanted.)
\fill [
##1,
% added opacity so the look in the legend is the same
% as in the plot
opacity=0.5,
draw=none,
] (0mm,-1ex) -- (0mm,1ex) -- (6mm,1ex) -- (6mm,-1ex) -- cycle;
\draw [##1,fill=none] (0mm,0mm) -- (6mm,0mm);
},
},
}
\newcommand{\errorband}[6][]{
% here all the plots need to be "forgotten" ...
\addplot [
draw=none,
stack plots=y,
forget plot,
] table [x={#3},y expr=\thisrow{#4}-\thisrow{#5}] {#2};
\addplot [
fill=gray!40,
stack plots=y,
% area legend, % <-- not needed
opacity=0.5,
#1,
draw=none,
forget plot,
] table [x={#3},y expr=2*\thisrow{#5}] {#2} \closedcycle;
\addplot [
stack plots=y,
draw=none,
forget plot,
] table [x={#3},y expr=-(\thisrow{#4}+\thisrow{#5})] {#2};
\addplot [
forget plot,
thick,
#1,
fill=none,
] table [x={#3},y expr=\thisrow{#4}] {#2};
% ... and the legend image and entry have to be given at the end
\addlegendimage{line and fill,#1}
\addlegendentry{#6}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% no markers, % <-- not needed
width=.5\textwidth,
ylabel near ticks,
xlabel near ticks,
enlarge x limits=false,
xlabel=Observable,
ylabel=Events,
ytick=\empty,
xtick=\empty,
ymin=50,
ymax=150,
legend cell align=left,
% (changed (both) scalings from 0.3 to 0.5 so they can be
% better seen in the screenshot)
legend image post style={scale=0.5},
legend style={
draw=none,
fill=none,
% align=left, % <-- not needed
nodes={
scale=0.5,
% transform shape, % <-- not needed
},
},
]
\errorband [%
draw=black,
fill=green!50!black,
]{\datatable}{0}{5}{6}{Morphing output (Input Set B)}
\errorband [%
draw=black,
fill=black!50!white,
]{\datatable}{0}{3}{4}{Morphing output (Input Set A)}
\errorband [%
draw=black,
fill=violet!50!white,
] {\datatable}{0}{1}{2}{True Template}
\end{axis}
\end{tikzpicture}
\end{document}