Put grid below plot in pgfplots

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












In a plot with pgfplots I would like to draw, from background to foreground, in the following order:



  1. (background) grid;

  2. (intermediate layer) plot;

  3. (foreground) axis.

As specified in this answer,




Adding axis on top to the axis options means axis lines, ticks and
grids are printed on top of the stuff inside the environment.




So, this is not the correct option for this. Trying to follow the suggestion in this answer, I produced the following code:



documentclassarticle
usepackagepgfplots
usetikzlibraryshapes.geometric,arrows,arrows.meta

pgfdeclarelayerbackground
pgfsetlayersbackground,main

begindocument

begintikzpicture
beginaxis[width=textwidth,
xmin=-0.5, xmax=6,
ymin=-0.5, ymax=4,
axis on top,
axis line style = thin,
axis lines=middle,
axis line style=-Stealth[length=2.5mm],
thick,
grid=major, grid style=dashed,gray!30]

beginpgfonlayermain
addplot[samples = 100] x^(1/2);
endpgfonlayer

endaxis
endtikzpicture

enddocument


But it doesn't work (grid is still in foreground). How to properly set the layers?










share|improve this question

















  • 1




    The easiest way is probably to use axis on top but draw the grid by hand, behind the plot.
    – Henri Menke
    19 mins ago










  • @HenriMenke As far as I understood, the fact that axis and grid go together, complicates the problem. Having several plots with different axis max and min values would make quite troublesome also to draw by hand the grid each time.
    – BowPark
    15 mins ago















up vote
3
down vote

favorite












In a plot with pgfplots I would like to draw, from background to foreground, in the following order:



  1. (background) grid;

  2. (intermediate layer) plot;

  3. (foreground) axis.

As specified in this answer,




Adding axis on top to the axis options means axis lines, ticks and
grids are printed on top of the stuff inside the environment.




So, this is not the correct option for this. Trying to follow the suggestion in this answer, I produced the following code:



documentclassarticle
usepackagepgfplots
usetikzlibraryshapes.geometric,arrows,arrows.meta

pgfdeclarelayerbackground
pgfsetlayersbackground,main

begindocument

begintikzpicture
beginaxis[width=textwidth,
xmin=-0.5, xmax=6,
ymin=-0.5, ymax=4,
axis on top,
axis line style = thin,
axis lines=middle,
axis line style=-Stealth[length=2.5mm],
thick,
grid=major, grid style=dashed,gray!30]

beginpgfonlayermain
addplot[samples = 100] x^(1/2);
endpgfonlayer

endaxis
endtikzpicture

enddocument


But it doesn't work (grid is still in foreground). How to properly set the layers?










share|improve this question

















  • 1




    The easiest way is probably to use axis on top but draw the grid by hand, behind the plot.
    – Henri Menke
    19 mins ago










  • @HenriMenke As far as I understood, the fact that axis and grid go together, complicates the problem. Having several plots with different axis max and min values would make quite troublesome also to draw by hand the grid each time.
    – BowPark
    15 mins ago













up vote
3
down vote

favorite









up vote
3
down vote

favorite











In a plot with pgfplots I would like to draw, from background to foreground, in the following order:



  1. (background) grid;

  2. (intermediate layer) plot;

  3. (foreground) axis.

As specified in this answer,




Adding axis on top to the axis options means axis lines, ticks and
grids are printed on top of the stuff inside the environment.




So, this is not the correct option for this. Trying to follow the suggestion in this answer, I produced the following code:



documentclassarticle
usepackagepgfplots
usetikzlibraryshapes.geometric,arrows,arrows.meta

pgfdeclarelayerbackground
pgfsetlayersbackground,main

begindocument

begintikzpicture
beginaxis[width=textwidth,
xmin=-0.5, xmax=6,
ymin=-0.5, ymax=4,
axis on top,
axis line style = thin,
axis lines=middle,
axis line style=-Stealth[length=2.5mm],
thick,
grid=major, grid style=dashed,gray!30]

beginpgfonlayermain
addplot[samples = 100] x^(1/2);
endpgfonlayer

endaxis
endtikzpicture

enddocument


But it doesn't work (grid is still in foreground). How to properly set the layers?










share|improve this question













In a plot with pgfplots I would like to draw, from background to foreground, in the following order:



  1. (background) grid;

  2. (intermediate layer) plot;

  3. (foreground) axis.

As specified in this answer,




Adding axis on top to the axis options means axis lines, ticks and
grids are printed on top of the stuff inside the environment.




So, this is not the correct option for this. Trying to follow the suggestion in this answer, I produced the following code:



documentclassarticle
usepackagepgfplots
usetikzlibraryshapes.geometric,arrows,arrows.meta

pgfdeclarelayerbackground
pgfsetlayersbackground,main

begindocument

begintikzpicture
beginaxis[width=textwidth,
xmin=-0.5, xmax=6,
ymin=-0.5, ymax=4,
axis on top,
axis line style = thin,
axis lines=middle,
axis line style=-Stealth[length=2.5mm],
thick,
grid=major, grid style=dashed,gray!30]

beginpgfonlayermain
addplot[samples = 100] x^(1/2);
endpgfonlayer

endaxis
endtikzpicture

enddocument


But it doesn't work (grid is still in foreground). How to properly set the layers?







tikz-pgf pgfplots






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









BowPark

45228




45228







  • 1




    The easiest way is probably to use axis on top but draw the grid by hand, behind the plot.
    – Henri Menke
    19 mins ago










  • @HenriMenke As far as I understood, the fact that axis and grid go together, complicates the problem. Having several plots with different axis max and min values would make quite troublesome also to draw by hand the grid each time.
    – BowPark
    15 mins ago













  • 1




    The easiest way is probably to use axis on top but draw the grid by hand, behind the plot.
    – Henri Menke
    19 mins ago










  • @HenriMenke As far as I understood, the fact that axis and grid go together, complicates the problem. Having several plots with different axis max and min values would make quite troublesome also to draw by hand the grid each time.
    – BowPark
    15 mins ago








1




1




The easiest way is probably to use axis on top but draw the grid by hand, behind the plot.
– Henri Menke
19 mins ago




The easiest way is probably to use axis on top but draw the grid by hand, behind the plot.
– Henri Menke
19 mins ago












@HenriMenke As far as I understood, the fact that axis and grid go together, complicates the problem. Having several plots with different axis max and min values would make quite troublesome also to draw by hand the grid each time.
– BowPark
15 mins ago





@HenriMenke As far as I understood, the fact that axis and grid go together, complicates the problem. Having several plots with different axis max and min values would make quite troublesome also to draw by hand the grid each time.
– BowPark
15 mins ago











2 Answers
2






active

oldest

votes

















up vote
3
down vote













This is discussed at length in section 4.27 of the pgfplots manual. All I did is to define a new layer set based on axis on top in which I flip the order of axis grid and main.



documentclassarticle
usepackagepgfplots
usetikzlibraryshapes.geometric,arrows,arrows.meta
pgfplotsset
/pgfplots/layers/Bowpark/.define layer set=
axis background,axis grid,main,axis ticks,axis lines,axis tick labels,
axis descriptions,axis foreground
/pgfplots/layers/standard,


begindocument

begintikzpicture
beginaxis[/pgfplots/layers/Bowpark,
width=textwidth,
xmin=-0.5, xmax=6,
ymin=-0.5, ymax=4,
axis line style = thin,
axis lines=middle,
axis line style=-Stealth[length=2.5mm],
thick,
grid=major, grid style=dashed,gray!30]

addplot[samples = 100] x^(1/2);

endaxis
endtikzpicture

enddocument


enter image description here






share|improve this answer



























    up vote
    1
    down vote













    From my code now the grid is in background using dotted instead of dashed.



    enter image description here



    documentclassarticle
    usepackagepgfplots
    usetikzlibraryshapes.geometric,arrows,arrows.meta
    pgfdeclarelayerbackground
    pgfsetlayersbackground,main
    %for the grid
    pgfplotssetmajor grid style=dotted,gray!30

    begindocument

    begintikzpicture
    beginaxis[width=textwidth,
    xmin=-0.5, xmax=6,
    ymin=-0.5, ymax=4,
    axis on top,
    axis line style = thin,
    axis lines=middle,
    axis line style=-Stealth[length=2.5mm],
    thick,
    grid = major,
    grid style=dashed, gray!30]

    beginpgfonlayermain
    addplot[samples = 100] x^(1/2);
    endpgfonlayer

    endaxis
    endtikzpicture

    enddocument





    share|improve this answer


















    • 1




      Grid must be below the graph (explicitly stated in the question)
      – fontgeologist
      25 mins ago











    • Before of a downvote, I think that an user colud wait.
      – Sebastiano
      23 mins ago






    • 1




      Why wait if the answer does not address the problem? Why not wait before answering to make sure to understand the problem?
      – fontgeologist
      22 mins ago











    • I'm using Papeeria and I have a light source was very bright and you could not see the image produced.
      – Sebastiano
      13 mins ago










    • NO! The dots are still above the graph. This is wrong! If you would dissect the resulting pdf you would notice this.
      – fontgeologist
      9 mins ago











    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "85"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f456859%2fput-grid-below-plot-in-pgfplots%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    This is discussed at length in section 4.27 of the pgfplots manual. All I did is to define a new layer set based on axis on top in which I flip the order of axis grid and main.



    documentclassarticle
    usepackagepgfplots
    usetikzlibraryshapes.geometric,arrows,arrows.meta
    pgfplotsset
    /pgfplots/layers/Bowpark/.define layer set=
    axis background,axis grid,main,axis ticks,axis lines,axis tick labels,
    axis descriptions,axis foreground
    /pgfplots/layers/standard,


    begindocument

    begintikzpicture
    beginaxis[/pgfplots/layers/Bowpark,
    width=textwidth,
    xmin=-0.5, xmax=6,
    ymin=-0.5, ymax=4,
    axis line style = thin,
    axis lines=middle,
    axis line style=-Stealth[length=2.5mm],
    thick,
    grid=major, grid style=dashed,gray!30]

    addplot[samples = 100] x^(1/2);

    endaxis
    endtikzpicture

    enddocument


    enter image description here






    share|improve this answer
























      up vote
      3
      down vote













      This is discussed at length in section 4.27 of the pgfplots manual. All I did is to define a new layer set based on axis on top in which I flip the order of axis grid and main.



      documentclassarticle
      usepackagepgfplots
      usetikzlibraryshapes.geometric,arrows,arrows.meta
      pgfplotsset
      /pgfplots/layers/Bowpark/.define layer set=
      axis background,axis grid,main,axis ticks,axis lines,axis tick labels,
      axis descriptions,axis foreground
      /pgfplots/layers/standard,


      begindocument

      begintikzpicture
      beginaxis[/pgfplots/layers/Bowpark,
      width=textwidth,
      xmin=-0.5, xmax=6,
      ymin=-0.5, ymax=4,
      axis line style = thin,
      axis lines=middle,
      axis line style=-Stealth[length=2.5mm],
      thick,
      grid=major, grid style=dashed,gray!30]

      addplot[samples = 100] x^(1/2);

      endaxis
      endtikzpicture

      enddocument


      enter image description here






      share|improve this answer






















        up vote
        3
        down vote










        up vote
        3
        down vote









        This is discussed at length in section 4.27 of the pgfplots manual. All I did is to define a new layer set based on axis on top in which I flip the order of axis grid and main.



        documentclassarticle
        usepackagepgfplots
        usetikzlibraryshapes.geometric,arrows,arrows.meta
        pgfplotsset
        /pgfplots/layers/Bowpark/.define layer set=
        axis background,axis grid,main,axis ticks,axis lines,axis tick labels,
        axis descriptions,axis foreground
        /pgfplots/layers/standard,


        begindocument

        begintikzpicture
        beginaxis[/pgfplots/layers/Bowpark,
        width=textwidth,
        xmin=-0.5, xmax=6,
        ymin=-0.5, ymax=4,
        axis line style = thin,
        axis lines=middle,
        axis line style=-Stealth[length=2.5mm],
        thick,
        grid=major, grid style=dashed,gray!30]

        addplot[samples = 100] x^(1/2);

        endaxis
        endtikzpicture

        enddocument


        enter image description here






        share|improve this answer












        This is discussed at length in section 4.27 of the pgfplots manual. All I did is to define a new layer set based on axis on top in which I flip the order of axis grid and main.



        documentclassarticle
        usepackagepgfplots
        usetikzlibraryshapes.geometric,arrows,arrows.meta
        pgfplotsset
        /pgfplots/layers/Bowpark/.define layer set=
        axis background,axis grid,main,axis ticks,axis lines,axis tick labels,
        axis descriptions,axis foreground
        /pgfplots/layers/standard,


        begindocument

        begintikzpicture
        beginaxis[/pgfplots/layers/Bowpark,
        width=textwidth,
        xmin=-0.5, xmax=6,
        ymin=-0.5, ymax=4,
        axis line style = thin,
        axis lines=middle,
        axis line style=-Stealth[length=2.5mm],
        thick,
        grid=major, grid style=dashed,gray!30]

        addplot[samples = 100] x^(1/2);

        endaxis
        endtikzpicture

        enddocument


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 11 mins ago









        marmot

        68.6k476148




        68.6k476148




















            up vote
            1
            down vote













            From my code now the grid is in background using dotted instead of dashed.



            enter image description here



            documentclassarticle
            usepackagepgfplots
            usetikzlibraryshapes.geometric,arrows,arrows.meta
            pgfdeclarelayerbackground
            pgfsetlayersbackground,main
            %for the grid
            pgfplotssetmajor grid style=dotted,gray!30

            begindocument

            begintikzpicture
            beginaxis[width=textwidth,
            xmin=-0.5, xmax=6,
            ymin=-0.5, ymax=4,
            axis on top,
            axis line style = thin,
            axis lines=middle,
            axis line style=-Stealth[length=2.5mm],
            thick,
            grid = major,
            grid style=dashed, gray!30]

            beginpgfonlayermain
            addplot[samples = 100] x^(1/2);
            endpgfonlayer

            endaxis
            endtikzpicture

            enddocument





            share|improve this answer


















            • 1




              Grid must be below the graph (explicitly stated in the question)
              – fontgeologist
              25 mins ago











            • Before of a downvote, I think that an user colud wait.
              – Sebastiano
              23 mins ago






            • 1




              Why wait if the answer does not address the problem? Why not wait before answering to make sure to understand the problem?
              – fontgeologist
              22 mins ago











            • I'm using Papeeria and I have a light source was very bright and you could not see the image produced.
              – Sebastiano
              13 mins ago










            • NO! The dots are still above the graph. This is wrong! If you would dissect the resulting pdf you would notice this.
              – fontgeologist
              9 mins ago















            up vote
            1
            down vote













            From my code now the grid is in background using dotted instead of dashed.



            enter image description here



            documentclassarticle
            usepackagepgfplots
            usetikzlibraryshapes.geometric,arrows,arrows.meta
            pgfdeclarelayerbackground
            pgfsetlayersbackground,main
            %for the grid
            pgfplotssetmajor grid style=dotted,gray!30

            begindocument

            begintikzpicture
            beginaxis[width=textwidth,
            xmin=-0.5, xmax=6,
            ymin=-0.5, ymax=4,
            axis on top,
            axis line style = thin,
            axis lines=middle,
            axis line style=-Stealth[length=2.5mm],
            thick,
            grid = major,
            grid style=dashed, gray!30]

            beginpgfonlayermain
            addplot[samples = 100] x^(1/2);
            endpgfonlayer

            endaxis
            endtikzpicture

            enddocument





            share|improve this answer


















            • 1




              Grid must be below the graph (explicitly stated in the question)
              – fontgeologist
              25 mins ago











            • Before of a downvote, I think that an user colud wait.
              – Sebastiano
              23 mins ago






            • 1




              Why wait if the answer does not address the problem? Why not wait before answering to make sure to understand the problem?
              – fontgeologist
              22 mins ago











            • I'm using Papeeria and I have a light source was very bright and you could not see the image produced.
              – Sebastiano
              13 mins ago










            • NO! The dots are still above the graph. This is wrong! If you would dissect the resulting pdf you would notice this.
              – fontgeologist
              9 mins ago













            up vote
            1
            down vote










            up vote
            1
            down vote









            From my code now the grid is in background using dotted instead of dashed.



            enter image description here



            documentclassarticle
            usepackagepgfplots
            usetikzlibraryshapes.geometric,arrows,arrows.meta
            pgfdeclarelayerbackground
            pgfsetlayersbackground,main
            %for the grid
            pgfplotssetmajor grid style=dotted,gray!30

            begindocument

            begintikzpicture
            beginaxis[width=textwidth,
            xmin=-0.5, xmax=6,
            ymin=-0.5, ymax=4,
            axis on top,
            axis line style = thin,
            axis lines=middle,
            axis line style=-Stealth[length=2.5mm],
            thick,
            grid = major,
            grid style=dashed, gray!30]

            beginpgfonlayermain
            addplot[samples = 100] x^(1/2);
            endpgfonlayer

            endaxis
            endtikzpicture

            enddocument





            share|improve this answer














            From my code now the grid is in background using dotted instead of dashed.



            enter image description here



            documentclassarticle
            usepackagepgfplots
            usetikzlibraryshapes.geometric,arrows,arrows.meta
            pgfdeclarelayerbackground
            pgfsetlayersbackground,main
            %for the grid
            pgfplotssetmajor grid style=dotted,gray!30

            begindocument

            begintikzpicture
            beginaxis[width=textwidth,
            xmin=-0.5, xmax=6,
            ymin=-0.5, ymax=4,
            axis on top,
            axis line style = thin,
            axis lines=middle,
            axis line style=-Stealth[length=2.5mm],
            thick,
            grid = major,
            grid style=dashed, gray!30]

            beginpgfonlayermain
            addplot[samples = 100] x^(1/2);
            endpgfonlayer

            endaxis
            endtikzpicture

            enddocument






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 15 mins ago

























            answered 32 mins ago









            Sebastiano

            7,85041654




            7,85041654







            • 1




              Grid must be below the graph (explicitly stated in the question)
              – fontgeologist
              25 mins ago











            • Before of a downvote, I think that an user colud wait.
              – Sebastiano
              23 mins ago






            • 1




              Why wait if the answer does not address the problem? Why not wait before answering to make sure to understand the problem?
              – fontgeologist
              22 mins ago











            • I'm using Papeeria and I have a light source was very bright and you could not see the image produced.
              – Sebastiano
              13 mins ago










            • NO! The dots are still above the graph. This is wrong! If you would dissect the resulting pdf you would notice this.
              – fontgeologist
              9 mins ago













            • 1




              Grid must be below the graph (explicitly stated in the question)
              – fontgeologist
              25 mins ago











            • Before of a downvote, I think that an user colud wait.
              – Sebastiano
              23 mins ago






            • 1




              Why wait if the answer does not address the problem? Why not wait before answering to make sure to understand the problem?
              – fontgeologist
              22 mins ago











            • I'm using Papeeria and I have a light source was very bright and you could not see the image produced.
              – Sebastiano
              13 mins ago










            • NO! The dots are still above the graph. This is wrong! If you would dissect the resulting pdf you would notice this.
              – fontgeologist
              9 mins ago








            1




            1




            Grid must be below the graph (explicitly stated in the question)
            – fontgeologist
            25 mins ago





            Grid must be below the graph (explicitly stated in the question)
            – fontgeologist
            25 mins ago













            Before of a downvote, I think that an user colud wait.
            – Sebastiano
            23 mins ago




            Before of a downvote, I think that an user colud wait.
            – Sebastiano
            23 mins ago




            1




            1




            Why wait if the answer does not address the problem? Why not wait before answering to make sure to understand the problem?
            – fontgeologist
            22 mins ago





            Why wait if the answer does not address the problem? Why not wait before answering to make sure to understand the problem?
            – fontgeologist
            22 mins ago













            I'm using Papeeria and I have a light source was very bright and you could not see the image produced.
            – Sebastiano
            13 mins ago




            I'm using Papeeria and I have a light source was very bright and you could not see the image produced.
            – Sebastiano
            13 mins ago












            NO! The dots are still above the graph. This is wrong! If you would dissect the resulting pdf you would notice this.
            – fontgeologist
            9 mins ago





            NO! The dots are still above the graph. This is wrong! If you would dissect the resulting pdf you would notice this.
            – fontgeologist
            9 mins ago


















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f456859%2fput-grid-below-plot-in-pgfplots%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke