5

I'm trying to plot two graphs with the area between them filled. This works fine, except for the fact that the \closedcycle does not go down vertically, but rather at some (arbitrary?) slope, circled in red: enter image description here Here is my MWE:

\documentclass{article}

\usepackage{pgfplots,pgfplotstable} 

\begin{document}
\begin{tikzpicture}
    \pgfplotstableread[col sep=semicolon] {data.csv}\data
    \begin{axis}[
        xmin=0.2]
        \addplot[gray,smooth,fill=black!10!white] 
            table[x=xvalues,y=upper]{\data} \closedcycle;
       \addplot[gray,smooth,fill=white] 
            table[x=xvalues,y=lower]{\data} \closedcycle;
    \end{axis}
\end{tikzpicture}
\end{document}

My data.csv looks like this:

xvalues;lower;upper
0.15;949.59;1069.76
0.21;1290.74;1457.88
0.27;1617.32;1822.28
0.33;1928.45;2167.26
0.39;2237.91;2486.17
0.45;2526.9;2776.77
0.51;2750.88;3032.38
0.57;2875.84;3249.56
0.63;2935.11;3326.71
0.69;2854.73;3237.55
0.75;2595.52;3087.41
0.81;2256.67;2756.42
0.87;1681.25;2169.95
0.93;1636.97;1974.47
0.99;1464.91;1773.17
1.05;1307.58;1590.35
1.11;1188.51;1454.49
1.17;1078.54;1317.64
1.23;981.88;1216.65
1.29;902.7;1120.32
1.35;834.92;1015.32
1.41;763.63;924.14
1.47;694.77;843.91

Edit: My workaround for the moment is making up imaginary values at 1.6, and setting xmax=1.5, so the slope is outside the plotting area. This can't be the correct way to do it though, so I'm still interested. Or is my approach for filling wrong in general?

3

I would suggest you use a stacked plot for this. That way you can get a smooth plot, and you don't need to rearrange your data.

For this, you would plot the lower plot first as an invisible plot, by setting stack plots=y in the axis options and then using something like

\addplot[draw=none,smooth,fill=none] 
        table[x=xvalues,y=lower]{\data};

And then you would stack the difference of the upper and lower values on top:

\addplot[black,smooth,fill=gray!25] 
        table[x=xvalues,y expr=\thisrow{upper}-\thisrow{lower}]{\data} \closedcycle;

This will give you

\documentclass{article}

\usepackage{pgfplots,pgfplotstable} 

\begin{document}
\begin{tikzpicture}
    \pgfplotstableread[col sep=semicolon] {
    xvalues;lower;upper
0.15;949.59;1069.76
0.21;1290.74;1457.88
0.27;1617.32;1822.28
0.33;1928.45;2167.26
0.39;2237.91;2486.17
0.45;2526.9;2776.77
0.51;2750.88;3032.38
0.57;2875.84;3249.56
0.63;2935.11;3326.71
0.69;2854.73;3237.55
0.75;2595.52;3087.41
0.81;2256.67;2756.42
0.87;1681.25;2169.95
0.93;1636.97;1974.47
0.99;1464.91;1773.17
1.05;1307.58;1590.35
1.11;1188.51;1454.49
1.17;1078.54;1317.64
1.23;981.88;1216.65
1.29;902.7;1120.32
1.35;834.92;1015.32
1.41;763.63;924.14
1.47;694.77;843.91
}\data
    \begin{axis}[
        xmin=0,
        stack plots=y
        ]
        \addplot[draw=none,smooth,fill=none] 
            table[x=xvalues,y=lower]{\data};
       \addplot[black,smooth,fill=gray!25] 
            table[x=xvalues,y expr=\thisrow{upper}-\thisrow{lower}]{\data} \closedcycle;
    \end{axis}
\end{tikzpicture}
\end{document}
1
  • Thank you for taking the time to answer such an old question +1. I'll take closer look tomorrow.
    – Psirus
    Jun 18 '13 at 6:48
6

Beeing no expert I have a solution to start with. Upside: no funny slopes, downside: smoothing not possible, not really a closed plot

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{datalow.dat}
# xvalues lower
0.15 949.59
0.21 1290.74
0.27 1617.32
0.33 1928.45
0.39 2237.91
0.45 2526.9
0.51 2750.88
0.57 2875.84
0.63 2935.11
0.69 2854.73
0.75 2595.52
0.81 2256.67
0.87 1681.25
0.93 1636.97
0.99 1464.91
1.05 1307.58
1.11 1188.51
1.17 1078.54
1.23 981.88
1.29 902.7
1.35 834.92
1.41 763.63
1.47 694.77
\end{filecontents}
\begin{filecontents}{dataup.dat}
# xvalues upper
0.15 1069.76
0.21 1457.88
0.27 1822.28
0.33 2167.26
0.39 2486.17
0.45 2776.77
0.51 3032.38
0.57 3249.56
0.63 3326.71
0.69 3237.55
0.75 3087.41
0.81 2756.42
0.87 2169.95
0.93 1974.47
0.99 1773.17
1.05 1590.35
1.11 1454.49
1.17 1317.64
1.23 1216.65
1.29 1120.32
1.35 1015.32
1.41 924.14
1.47 843.91
\end{filecontents}

\usepackage{pgfplots,pgfplotstable} 

\begin{document}


\begin{tikzpicture}
\pgfplotstableread{datalow.dat}\datalow
\pgfplotstableread{dataup.dat}\dataup
\pgfplotstablesort[sort cmp={float >}]{\dataupsorted}{\dataup}
\pgfplotstablevertcat{\filledcurve}{\datalow}
\pgfplotstablevertcat{\filledcurve}{\dataupsorted}
 \begin{axis}
%Plot the dataset
\addplot[mark=none,black] table {\dataup};
\addplot[mark=none,black] table {\datalow};
%Plot the filles curve
 \addplot[fill=gray!20,draw=none,forget plot] table {\filledcurve}; 

 \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

1
  • This is cool. Can you explain how this works please? May 11 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.