# Using pgfplots, is there a way to overlay multiple stacked plots with different ranges?

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}

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

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}

-
What do you want your y values be when they are not specified? 0? If so, then make it so (0 0 0, 1 0 0, 5 0 0). pgfplots cannot guess your missing values. –  knittl Sep 9 '12 at 11:44
@knittl I don't want to draw anything there at all. If I make them 0, I believe pgfplots will draw lines connecting the 0 values to the part of the data that I actually wanted to draw. I found one solution, however, that I just posted as an answer. (That should also make it clear how I wanted this to look in the first place.) Thanks, though! –  Vegard Sep 9 '12 at 11:49
From the manual v 1.4.1, page 51: "The current implementation for stack plots does not interpolate missing coordinates. That means stacking will fail if the plots have dierent grids." –  Tom Bombadil Sep 9 '12 at 11:52
@Vegard: you can specify filters for coordinates, maybe that would work? (/pgfplots/y filter/.code) –  knittl Sep 9 '12 at 11:55

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}

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

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,
]

\end{axis}
\begin{axis}
[   stack plots=y,
area style,
enlarge x limits=false,
xmin=0,
xmax=5,
ymin=0,
ymax=25,
hide axis,
]

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


## Result

-
Nice, I didn't know this was possible. Thanks! –  Vegard Sep 9 '12 at 12:40
The second axis should use the hide axis option to make sure that the elements aren't drawn twice. –  Jake Sep 9 '12 at 18:45
@jake: Thanks, I didn't know such option existed. I'll update tomorrow. –  Tom Bombadil Sep 9 '12 at 19:35
It should be possible to not have to repeat all the options to the axes by adding them to the options to the tikzpicture, right? Using /pgfplots/.cd or every axis or something? –  Vegard Sep 18 '12 at 19:23
Yes, something like myaxisoptions/.style={...} and \begin{axis}[myaxisoptions] should work. –  Tom Bombadil Sep 18 '12 at 20:30

I found one solution. I decided to ditch the stacked plots altogether and just go for plain filled cycles. It requires laying out the table/data file differently, however. It should be clear from the example below:

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

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

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

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

% Table A
\addplot [draw=none, fill=gray!25, forget plot] table {\tableA} \closedcycle;

% Table B
\addplot [draw=none, fill=gray!25, forget plot] table {\tableB} \closedcycle;

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


Result:

I am still interested in knowing about solutions that don't require changing the tables, though.

-