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.



            legend style={
            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$}


enter image description here

  • 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? – Stefan Pinnow Jan 26 '17 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 Jan 26 '17 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. – Stefan Pinnow Jan 26 '17 at 9:47

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
        % define the layers you need.
        % (Don't forget to add `main' somewhere in that list!!)
        layers/my layer set/.define layer set={
            % you could state styles here which should be moved to
            % corresponding layers, but that is not necessary here.
            % That is why wo don't state anything here
        % activate the newly created layer set
        set layers=my layer set,
            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)
            legend columns=-1,
            xmin=0, xmax=10,
            ymin=0, ymax=10,
            % moved common `\addplot' options here
            every axis plot post/.append style={
                line width=5pt,
            % change `clip mode' to `individual' to avoid unwanted clipping
            clip mode=individual,
            \addplot [
                % and with `on layer' you can state the layer where the
                % plot should be drawn on
                on layer=foreground,
            ] {x};
            % because we don't state a layer here, this plot will be drawn
            % on layer `main'
            \addplot [black] {(x-5)^2};
            \addplot [
                on layer=background,
            ] {10-(x-5)^2};

image showing the result of above code

  • 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 Jan 26 '17 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. – Torbjørn T. Jan 26 '17 at 9:51
  • 3
    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. – Stefan Pinnow Jan 26 '17 at 9:52
  • @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 Jan 26 '17 at 9:58
  • @TorbjørnT., thanks for the hint. I have adapted my solution to work (again). (I had another solution first but while "improving" it I have deleted to much.) – Stefan Pinnow Jan 26 '17 at 11:34

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.