[Tex/LaTex] Using pgfplots, is there a way to overlay multiple stacked plots with different ranges

pgfplots

I am using the accepted answer to the question “Is there an easy way of using line thickness as an error indicator in a plot?” to draw error bands around my regular line plots.

However, I find myself wanting to plot two lines with slightly different x ranges, which makes pgfplots complain:

Package pgfplots Error: Sorry, pgfplots expects stacked plots to have exactly the same number of coordinates. Unfortunately, I encountered at plot with DIFFERENT NUMBERS OF COORDINATES. Please verify that 1. no point has been dropped by coordinate filters (for example log(0) or so) and 2. all plots have the same number of coordinates..

Is there a way to start the stacked plot over "from scratch" within the same axis/scale so that this error does not occur?

For example, in the example below, commenting out either the code for plotting table A or B will make it work, but as it is, it will give the error above:

\documentclass[a4paper]{article}
\usepackage{fullpage}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotstableread{
0 10 12
1 10 11
2 8  9
3 15 16
4 13 15
5 20 21
}\tableA

\pgfplotstableread{
2 20 23
3 19 21
4 20 22
}\tableB

\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}

% Table A
\addplot [draw=none, stack plots=y, forget plot] table [x={0}, y expr={\thisrowno{1}}] {\tableA};
\addplot [draw=none, stack plots=y, forget plot, fill=gray!25] table [x={0}, y expr={\thisrowno{2}-\thisrowno{1}}] {\tableA} \closedcycle;

% Table B
\addplot [draw=none, stack plots=y, forget plot] table [x={0}, y expr={\thisrowno{1}}] {\tableB};
\addplot [draw=none, stack plots=y, forget plot, fill=gray!25] table [x={0}, y expr={\thisrowno{2}-\thisrowno{1}}] {\tableB} \closedcycle;

\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

Best Answer

You can use two different axis, if you make sure they have the same xmin/xmax/ymin/ymax. As Jake pointed out, the second (and all that probably follow) axis should use the hide axis key. I would recommend adding it only when you're otherwise done, it should overlay nicely, but will probably cause artifacts for certain zoom levels of your PDF viewer or when printing.

Code

\documentclass[a4paper]{article}
\usepackage{fullpage}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\pgfplotstableread{
0 10 12
1 10 11
2 8  9
3 15 16
4 13 15
5 20 21
}\tableA

\pgfplotstableread{
2 20 23
3 19 21
4 20 22
}\tableB

\begin{document}
    \begin{center}
        \begin{tikzpicture}
            \begin{axis}
            [   stack plots=y,
                area style,
                enlarge x limits=false,
                xmin=0,
                xmax=5,
                ymin=0,
                ymax=25,
            ]
                \addplot[fill=none,draw=none] table[y expr={\thisrowno{1}}] {\tableA} \closedcycle;
                \addplot[fill=blue!30!,draw=none] table[y expr={\thisrowno{2}-\thisrowno{1}}] {\tableA} \closedcycle;

            \end{axis}
            \begin{axis}
            [   stack plots=y,
                area style,
                enlarge x limits=false,
                xmin=0,
                xmax=5,
                ymin=0,
                ymax=25,
                hide axis,
            ]
                \addplot[fill=none,draw=none] table[y expr={\thisrowno{1}}] {\tableB} \closedcycle;
                \addplot[fill=blue!30!,draw=none] table[y expr={\thisrowno{2}-\thisrowno{1}}] {\tableB} \closedcycle;

            \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}

Result

enter image description here