2

I have a set of filled-plots (MWE: red) and line-plots (MWE: blue) in a graph. Stacking order works fine according to the order of code lines. Additionally, I need to fill the area between two line-plots in front of the filled plot.

The fill between works fine, but only visible behind the filled-plot although drawn after the filled-plot.

(a) Is this a bug or a feature?

(b) How can I force the fill between to be visible on top?

MWE:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot [draw = none, fill=red]
            % \addplot [draw = none, fill=red, opacity=0.5]
                table[row sep=crcr]{%
                    1   1\\
                    1   2\\
                    2   2\\
                    2   1\\
                    }
                --cycle;
            \addplot [blue, name path = A]
                table[row sep=crcr]{%
                    1.4 1.2\\
                    1.2 1.4\\
            };
            \addplot [blue, name path = B]
                table[row sep=crcr]{%
                    1.8 1.6\\
                    1.6 1.8\\
            };
            \addplot[fill=green]
                fill between[ of = A and B ];
        \end{axis}
    \end{tikzpicture}
\end{document}

Result according to MWE: MWE no opacity

Result with opacity to show correct fill between behind the filled plot: MWE with opacity

  • 3
    on behalf of all color blinds, you, sir, are mean :) – percusse Feb 26 '18 at 13:08
  • sorry for that one @percusse, users are free to choose colors of their liking... for their solution posts, of course ;o) – hanshansen Feb 26 '18 at 13:13
1

To question (a):
This is a feature and in the following solution you will find out why.

To question (b):
If you just want everything to appear as per given order you could simply add fill between/on layer={main} to the axis options, as in my presented solution below.

The downside of doing this is, that then your "fill" will partly cover the "draw"s. Please zoom on one of the lines as proof. But that normally is an undesired behavior and the reason why the fill between stuff is by default drawn on the "lower level" pre main.

Thus marmot suggests the right approach to show everything "as desired", i.e. moving either the "red" stuff further back (on layer=pre main technically would have been enough) or move the "blue" stuff more to the front, e.g. add on layer=axis descriptions or higher with /pgfplots/layers/standard layers.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        fill between/on layer={main},
    ]
        \addplot [draw = none, fill=red!25]
        % \addplot [draw = none, fill=red, opacity=0.5]
            table {
                1   1
                1   2
                2   2
                2   1
                }
            -- cycle;
        \addplot [blue,ultra thick,name path = A]
            table {
                1.4 1.2
                1.2 1.4
        };
        \addplot [blue,ultra thick, name path = B]
            table {
                1.8 1.6
                1.6 1.8
        };
        \addplot [fill=green!25] fill between[ of = A and B ];
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • thx for the explanation to (b), understood. In addition, your approach fill between/on layer={main} enables a workaround with invisible lines for fill between and redrawing the lines afterwards. Maybe bad coding style but avoids considering layers, which might be beneficial in more complex graphs/pictures. – hanshansen Feb 27 '18 at 8:20
  • Am I right that this is just a comment of yours and that there is no question? Because I am not sure if I have understood everything right. – Stefan Pinnow Feb 27 '18 at 8:54
  • sorry for the confusion, no more questions. It was just a comment how your solution/information could potentially be (ab)used to avoid multiple layers and still draw correct fill betweens. – hanshansen Feb 27 '18 at 9:43
2

You might want to put the plots on different layers.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}
            \addplot [draw = none, fill=red,on layer=axis background]
            % \addplot [draw = none, fill=red, opacity=0.5]
                table[row sep=crcr]{%
                    1   1\\
                    1   2\\
                    2   2\\
                    2   1\\
                    }
                --cycle;
            \addplot [blue, name path = A]
                table[row sep=crcr]{%
                    1.4 1.2\\
                    1.2 1.4\\
            };
            \addplot [blue, name path = B]
                table[row sep=crcr]{%
                    1.8 1.6\\
                    1.6 1.8\\
            };
            \addplot[fill=green]
                fill between[ of = A and B ];
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  • thx @marmot, that's an option I've looked into, but since the real case involves larger sets of filled and already stacked plots I was wondering if there is another approach. From my point of view, your answer is more like a manual overide, working fine nonetheless. Any idea about question part (a): bug or feature? – hanshansen Feb 26 '18 at 15:10
  • 1
    @hanshansen I can't tell whether this is a bug or feature, all I know is that pgfplot delays the expansion of the plot commands such that your problem arises. On the other hand, this strategy seems to be the only one that allows pgfplots to adjust the plot range etc. Notice that in section 4.27 of the manual there is a long discussion of layers, and you could define your own set which can have as many layers as plots (if I understand the manual correctly). – user121799 Feb 26 '18 at 15:17
  • I'll wait a little more, maybe someone comes up with another idea/information. If not, I'll grant you the accepted answer... – hanshansen Feb 26 '18 at 15:28

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.