pgfplot 中的自定义函数在第二次调用时不起作用

pgfplot 中的自定义函数在第二次调用时不起作用

我从我之前的帖子中获得了灵感(绘制具有不确定性的线图) 和另一人 (是否有一种简单的方法可以使用线条粗细作为绘图中的错误指示器?)。

我现在的问题是,剪辑没有像我期望的那样“完全”发挥作用。“不确定性”只出现了一次,而且不是在两个图中都出现。

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}

\usepackage{filecontents}
\begin{filecontents*}{data.dat}
    x median min max
1 -0.9065578605 -0.9161337853 -0.8927911866
2 -0.9858531935 -0.9866290705 -0.9782854714
3 -0.9919112996 -0.9928878322 -0.9858735597
4 -0.9948344182 -0.9952654464 -0.9908276553
5 -0.9918070428 -0.9963889094 -0.9836641772
6 -0.9966039586 -0.9975105424 -0.9924121213
7 -0.9960682419 -0.9974834188 -0.9884081917
8 -0.9973953117 -0.998506092 -0.9849396899
9 -0.9952356047 -0.9981980176 -0.9863541005
10 -0.9970161498 -0.9980514916 -0.9963330354

\end{filecontents*}

\begin{filecontents*}{data1.dat}
    x median min max
1 -0.8240193416 -0.8486281633 -0.7422411442
2 -0.8330658718 -0.8525406122 -0.7090859347
3 -0.8370089186 -0.8604617119 -0.7123441299
4 -0.8289707373 -0.8584667444 -0.7187983394
5 -0.8358648997 -0.8618316054 -0.7326830162
6 -0.8324906487 -0.8590184034 -0.7296211256
7 -0.8305886302 -0.8570415378 -0.1143180106
8 -0.8360601481 -0.863639254 -0.7192661166
9 -0.8386025126 -0.8592355319 -0.7272505893
10 -0.836511685 -0.8599217417 -0.7219019069


\end{filecontents*}

\begin{document}

    \newcommand{\minmaxband}[6][]{ % x column, y column, error column, optional argument for setting style of the area plot
        \pgfplotstableread{#2}\datatable
        % Lower bound (invisible plot)
        \addplot [draw=none, stack plots=y, forget plot] table [
        x={#3},
        y expr=\thisrow{#6}
        ] {\datatable};

        % Stack twice the error, draw as area plot
        \addplot [draw=none, fill=gray!40, stack plots=y, area legend, #1] table [
        x={#3},
        y expr=\thisrow{#5}-\thisrow{#6}
        ] {\datatable} \closedcycle;

        % Reset stack using invisible plot
        \addplot [forget plot, stack plots=y,draw=none] table [x={#3}, y expr=-(\thisrow{#6})] {\datatable};
    }



    \begin{tikzpicture}[font=\sffamily]
    \begin{axis}[grid=major,xlabel=epoch,ylabel=loss,ymin=-1,ymax=-0.50, xmin=1, xmax=10]
    \minmaxband[red, opacity=0.2]{data1.dat}{x}{median}{min}{max}
    \addplot [red,thick] table [x index=0,y index=1] {data1.dat};

    \minmaxband[blue, opacity=0.2]{data.dat}{x}{median}{min}{max}
    \addplot [blue,thick] table [x index=0,y index=1] {data.dat};

    \end{axis}
    \end{tikzpicture}

\end{document}

我正在努力寻找这个错误,非常感谢帮助。

答案1

一种方法是在重置情节中更改#6为。#5

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}

\usepackage{filecontents}
\begin{filecontents*}{data.dat}
    x median min max
1 -0.9065578605 -0.9161337853 -0.8927911866
2 -0.9858531935 -0.9866290705 -0.9782854714
3 -0.9919112996 -0.9928878322 -0.9858735597
4 -0.9948344182 -0.9952654464 -0.9908276553
5 -0.9918070428 -0.9963889094 -0.9836641772
6 -0.9966039586 -0.9975105424 -0.9924121213
7 -0.9960682419 -0.9974834188 -0.9884081917
8 -0.9973953117 -0.998506092 -0.9849396899
9 -0.9952356047 -0.9981980176 -0.9863541005
10 -0.9970161498 -0.9980514916 -0.9963330354

\end{filecontents*}

\begin{filecontents*}{data1.dat}
    x median min max
1 -0.8240193416 -0.8486281633 -0.7422411442
2 -0.8330658718 -0.8525406122 -0.7090859347
3 -0.8370089186 -0.8604617119 -0.7123441299
4 -0.8289707373 -0.8584667444 -0.7187983394
5 -0.8358648997 -0.8618316054 -0.7326830162
6 -0.8324906487 -0.8590184034 -0.7296211256
7 -0.8305886302 -0.8570415378 -0.1143180106
8 -0.8360601481 -0.863639254 -0.7192661166
9 -0.8386025126 -0.8592355319 -0.7272505893
10 -0.836511685 -0.8599217417 -0.7219019069


\end{filecontents*}

\begin{document}

    \newcommand{\minmaxband}[6][]{ % x column, y column, error column, optional argument for setting style of the area plot
        \pgfplotstableread{#2}\datatable
        % Lower bound (invisible plot)
        \addplot [draw=none, stack plots=y, forget plot] table [
        x={#3},
        y expr=\thisrow{#6}
        ] {\datatable};

        % Stack twice the error, draw as area plot
        \addplot [draw=none, fill=gray!40, stack plots=y, area legend, #1] table [
        x={#3},
        y expr=\thisrow{#5}-\thisrow{#6}
        ] {\datatable} \closedcycle;

        % Reset stack using invisible plot
        \addplot [forget plot, stack plots=y,draw=none] table [x={#3}, y
        expr=-(\thisrow{#5})] {\datatable};
    }



    \begin{tikzpicture}[font=\sffamily]
    \begin{axis}[grid=major,xlabel=epoch,ylabel=loss,ymin=-1,ymax=-0.50, xmin=1, xmax=10]
    \minmaxband[red,opacity=0.2]{data1.dat}{x}{median}{min}{max}
    \addplot [red,thick] table [x index=0,y index=1] {data1.dat};
    \minmaxband[blue,
    opacity=0.2]{data.dat}{x}{median}{min}{max}
    \addplot [blue,thick] table [x index=0,y index=1] {data.dat};
    \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容