3

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}
Vegard
  • 2,651
  • 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 09 '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 09 '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 09 '12 at 11:52
  • @Vegard: you can specify filters for coordinates, maybe that would work? (/pgfplots/y filter/.code) – knittl Sep 09 '12 at 11:55

2 Answers2

6

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

Tom Bombadil
  • 40,123
1

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}

\pgfplotstableread{
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

\pgfplotstableread{
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:

enter image description here

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

Vegard
  • 2,651