1

I took inspiration by my previous post (Drawing Line Plot with uncertainty) and another (Is there an easy way of using line thickness as error indicator in a plot?).

My current issue, is that the snipped is nor working 'completely' as I expect it to work. The 'uncertainty' are is only showing up once, and not for both plots.

\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}

I am struggling to find the bug, help is really appreciated.

Styp
  • 349

1 Answers1

2

One way to go is to change #6 to #5 in the reset plot.

\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}

enter image description here

  • That was my initial thought, but then its difficult to plot a legend for example. Would be nice, to get both plots into one axis-env. – Styp Oct 16 '18 at 12:37
  • Never mind, fixed it was a typo: \addplot [forget plot, stack plots=y,draw=none] table [x={#3}, y expr=-(\thisrow{#6})] {\datatable}; needs to be relpaced with \addplot [forget plot, stack plots=y,draw=none] table [x={#3}, y expr=-(\thisrow{#5})] {\datatable}; – Styp Oct 16 '18 at 12:44
  • 1
    @Styp Interesting. I was just about to post % Reset stack using invisible plot \addplot [forget plot, stack plots=y,draw=none] table [x={#3}, y expr=-(\thisrow{#5})] {\datatable}; (#6 replaced by #5), which also works. –  Oct 16 '18 at 12:46
  • somehow I have a feeling that the command is keeping an internal state which needs to be 'reset' by adding this line, so the band is around 0 again... – Styp Oct 16 '18 at 12:49
  • @Styp Yes. This is how stacked plots work, and why the last "reset" plot is needed. It has nothing to do with the minmax per se, but with the fact that it is using stacked plot. What happened is that the second stacked plot just got moved out of the axis because the reset was not entirely correct. –  Oct 16 '18 at 12:57