18

I have gone through previous questions [1,2,3], but I still couldn't figure out how to draw plots on different layers and change their order of drawing.

For this MWE, I would be grateful if I could figure out how to change the order of appearance of the three plots in such a way that the legend entries have the same order while keeping the plots commands/definitions in the same order as much as possible.

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
            legend style={
                at={([yshift=30pt]0.5,1)},
                anchor=north},
            legend columns=-1,
            xmin=0, xmax=10,
            ymin=0, ymax=10,
        ]
        %
        \addplot [red,      line width = 5pt,   domain=0:10]    {x}; \addlegendentry{$x$}   
        \addplot [black,    line width = 5pt,   domain=0:10]    {(x-5)^2}; \addlegendentry{$(x-5)^2$}
        \addplot [yellow,   line width = 5pt,   domain=0:10]    {10-(x-5)^2}; \addlegendentry{$10-(x-5)^2$}
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

3
  • Sorry, but I am not sure if I understand your question right. Do you want to keep the order of legend entries as they are now and thus want to change the order of drawing of the \addplot commands? Is this a "real" example so we could also provide a solution that doesn't involve using different layers or is it required to use different layers? Commented Jan 26, 2017 at 9:12
  • @StefanPinnow For the first part of your question, yes, I want to keep the legend entries in the same order while manipulating the order in which the plots are overlaid by each other without the need to re-order their codes commands. For the second question part, yes this is a real example. However, I don't quite understand your statement "so we could also provide a solution that doesn't involve using different layers or is it required to use different layers".
    – Diaa
    Commented Jan 26, 2017 at 9:41
  • Sorry, but I thought if this is a real example then simply adding reverse legend to the axis options would also do what you want. But it turned out that this also changed the order of the drawn plots, so that is not an option. Commented Jan 26, 2017 at 9:47

1 Answer 1

20

In case you really should need to plot the \addplots on different layers, have a look at the following code.

For more details on how the solution works, please have a look at the comments in the code.

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % define the layers you need.
        % (Don't forget to add `main' somewhere in that list!!)
        layers/my layer set/.define layer set={
            background,
            main,
            foreground
        }{
            % you could state styles here which should be moved to
            % corresponding layers, but that is not necessary here.
            % That is why we don't state anything here
        },
        % activate the newly created layer set
        set layers=my layer set,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            legend style={
                % (it is better to use `anchor=south' because then the gap
                % between the top axis line and the lower border of the
                % legend is independent of the height of the legend box)
                at={([yshift=10pt]0.5,1)},
                anchor=south,
            },
            legend columns=-1,
            xmin=0, xmax=10,
            ymin=0, ymax=10,
            % moved common `\addplot' options here
            domain=0:10,
            every axis plot post/.append style={
                line width=5pt,
            },
            % change `clip mode' to `individual' to avoid unwanted clipping
            clip mode=individual,
        ]
            \addplot [
                red,
                % and with `on layer' you can state the layer where the
                % plot should be drawn on
                on layer=foreground,
            ] {x};
                \addlegendentry{$x$}
            % because we don't state a layer here, this plot will be drawn
            % on layer `main'
            \addplot [black] {(x-5)^2};
                \addlegendentry{$(x-5)^2$}
            \addplot [
                yellow,
                on layer=background,
            ] {10-(x-5)^2};
                \addlegendentry{$10-(x-5)^2$}
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

10
  • 1
    Thanks for this easy and to-the-point answer. Additionally, if I have 4+ plots, what other layers can be defined beside foreground, background and main?
    – Diaa
    Commented Jan 26, 2017 at 9:49
  • That doesn't seem to do anything though, does it? The on layer key has no effect that I can see, the red curve is at the bottom and the yellow on top. Commented Jan 26, 2017 at 9:51
  • 4
    You can choose any arbitrary names. So you could define e.g. my layer set/.define layer set={b2,b1,main,f1,f2}{}. Also spaces are allowed so also I am a layer below layer main would work. Commented Jan 26, 2017 at 9:52
  • 1
    @StefanPinnow I am sorry, but I switched the keys of on layer of red and yellow curves, and the output is the same as the order they are drawn.
    – Diaa
    Commented Jan 26, 2017 at 9:58
  • 1
    @JeT, you are welcome. Feel free to upvote my answer, if you haven't already. Commented Feb 18, 2021 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .