Tikz - Coordinates error when using draw with axisforeach not behaving in axis environmentGrouped bar chartHow do i get the x axis on top but keep a line on the bottomHow to prevent rounded and duplicated tick labels in pgfplots with fixed precision?How to hide empty (value 0) ybars with pgfplots?Show mark labels near marks and not centered in ybar interaval graphpgfplots: percentage in matrix plotCenter the axes in the coordinate originpgfplots, lengths in axis coordinatesHow to configure axis using characters and draw graphs? (tikzpicture)Draw arc using relative coordinates with tikz

Generate a random point outside a given rectangle within a map

A verb for when some rights are not violated?

The Game of the Century - why didn't Byrne take the rook after he forked Fischer?

Why do cheap flights with a layover get more expensive when you split them up into separate flights?

Is there a way to say "double + any number" in German?

Why private jets such as GulfStream ones fly higher than other civil jets?

Can this rough design show the required message?

Does the length of a password for Wi-Fi affect speed?

Could an areostationary satellite help locate asteroids?

What was the role of Commodore-West Germany?

How can I perform a deterministic physics simulation?

Does a 4 bladed prop have almost twice the thrust of a 2 bladed prop?

Which pronoun to replace an infinitive?

What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?

Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?

“The Fourier transform cannot measure two phases at the same frequency.” Why not?

How and where to get you research work assessed for PhD?

Do some languages mention the top limit of a range first?

What date did Henry Morgan capture his most famous flagship, the "Satisfaction"?

Why do my fried eggs start browning very fast?

What does the ISO setting for mechanical 35mm film cameras actually do?

Not been paid even after reminding the Treasurer; what should I do?

Which genus do I use for neutral expressions in German?

What could prevent players from leaving an island?



Tikz - Coordinates error when using draw with axis


foreach not behaving in axis environmentGrouped bar chartHow do i get the x axis on top but keep a line on the bottomHow to prevent rounded and duplicated tick labels in pgfplots with fixed precision?How to hide empty (value 0) ybars with pgfplots?Show mark labels near marks and not centered in ybar interaval graphpgfplots: percentage in matrix plotCenter the axes in the coordinate originpgfplots, lengths in axis coordinatesHow to configure axis using characters and draw graphs? (tikzpicture)Draw arc using relative coordinates with tikz






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








5















I am using the following code, which is supposed to cut a function in N parts :



documentclass[11pt]article

usepackagetikz
usepackagepgfplots
pgfplotssetcompat=newest

begindocument
resizeboxcolumnwidth!%
begintikzpicture
beginaxis
[
grid,
anchor=origin,
%axis equal=false,
xmin=0, xmax=1,
ymin=0, ymax=10,
minor tick num=1,
axis lines = middle,
tick label style=inner sep=2pt, font=footnotesize,
%
every axis plot post/.append style=thick,
samples=500
]
addplot [domain=0:1, color=blue] -2*ln(x);
endaxis

% Add lines
pgfmathsetmacroN4;

foreach x in N,...,0
draw [line width=0.15mm, red] (x/N, 0) -- (x/N, 10);

endtikzpicture

enddocument


But, instead of having 5 lines cutting my function from 0 to 1, this is what I have :



enter image description here



Do you have any idea of why this issue appears ?



P.S : Do you have a better way to declare variables than pgfmathsetmacro ?










share|improve this question
































    5















    I am using the following code, which is supposed to cut a function in N parts :



    documentclass[11pt]article

    usepackagetikz
    usepackagepgfplots
    pgfplotssetcompat=newest

    begindocument
    resizeboxcolumnwidth!%
    begintikzpicture
    beginaxis
    [
    grid,
    anchor=origin,
    %axis equal=false,
    xmin=0, xmax=1,
    ymin=0, ymax=10,
    minor tick num=1,
    axis lines = middle,
    tick label style=inner sep=2pt, font=footnotesize,
    %
    every axis plot post/.append style=thick,
    samples=500
    ]
    addplot [domain=0:1, color=blue] -2*ln(x);
    endaxis

    % Add lines
    pgfmathsetmacroN4;

    foreach x in N,...,0
    draw [line width=0.15mm, red] (x/N, 0) -- (x/N, 10);

    endtikzpicture

    enddocument


    But, instead of having 5 lines cutting my function from 0 to 1, this is what I have :



    enter image description here



    Do you have any idea of why this issue appears ?



    P.S : Do you have a better way to declare variables than pgfmathsetmacro ?










    share|improve this question




























      5












      5








      5








      I am using the following code, which is supposed to cut a function in N parts :



      documentclass[11pt]article

      usepackagetikz
      usepackagepgfplots
      pgfplotssetcompat=newest

      begindocument
      resizeboxcolumnwidth!%
      begintikzpicture
      beginaxis
      [
      grid,
      anchor=origin,
      %axis equal=false,
      xmin=0, xmax=1,
      ymin=0, ymax=10,
      minor tick num=1,
      axis lines = middle,
      tick label style=inner sep=2pt, font=footnotesize,
      %
      every axis plot post/.append style=thick,
      samples=500
      ]
      addplot [domain=0:1, color=blue] -2*ln(x);
      endaxis

      % Add lines
      pgfmathsetmacroN4;

      foreach x in N,...,0
      draw [line width=0.15mm, red] (x/N, 0) -- (x/N, 10);

      endtikzpicture

      enddocument


      But, instead of having 5 lines cutting my function from 0 to 1, this is what I have :



      enter image description here



      Do you have any idea of why this issue appears ?



      P.S : Do you have a better way to declare variables than pgfmathsetmacro ?










      share|improve this question
















      I am using the following code, which is supposed to cut a function in N parts :



      documentclass[11pt]article

      usepackagetikz
      usepackagepgfplots
      pgfplotssetcompat=newest

      begindocument
      resizeboxcolumnwidth!%
      begintikzpicture
      beginaxis
      [
      grid,
      anchor=origin,
      %axis equal=false,
      xmin=0, xmax=1,
      ymin=0, ymax=10,
      minor tick num=1,
      axis lines = middle,
      tick label style=inner sep=2pt, font=footnotesize,
      %
      every axis plot post/.append style=thick,
      samples=500
      ]
      addplot [domain=0:1, color=blue] -2*ln(x);
      endaxis

      % Add lines
      pgfmathsetmacroN4;

      foreach x in N,...,0
      draw [line width=0.15mm, red] (x/N, 0) -- (x/N, 10);

      endtikzpicture

      enddocument


      But, instead of having 5 lines cutting my function from 0 to 1, this is what I have :



      enter image description here



      Do you have any idea of why this issue appears ?



      P.S : Do you have a better way to declare variables than pgfmathsetmacro ?







      tikz-pgf pgfplots foreach axis






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 26 at 9:28









      Torbjørn T.

      164k13 gold badges268 silver badges454 bronze badges




      164k13 gold badges268 silver badges454 bronze badges










      asked Jul 26 at 8:43









      graillegraille

      1284 bronze badges




      1284 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          6














          The coordinate system of the axis is not the same as that of the underlying tikzpicture, so when you add a draw outside the axis you cannot expect the coordinates to correspond to those inside the axis.



          Instead, use



          pgfmathsetmacroN4;
          pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
          draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);



          before endaxis. (Or an ycomb plot, as in the example below.)



          documentclass[11pt]article
          usepackagepgfplots
          pgfplotssetcompat=1.16 % it's recommended to use an explicit version number

          begindocument
          begintikzpicture
          beginaxis
          [
          width=columnwidth, % I would generally not use resizebox
          grid,
          anchor=origin,
          %axis equal=false,
          xmin=0, xmax=1,
          ymin=0, ymax=10,
          minor tick num=1,
          axis lines = middle,
          tick label style=inner sep=2pt, font=footnotesize,
          %
          every axis plot post/.append style=thick,
          samples=500
          ]
          addplot [domain=0:1, color=blue] -2*ln(x);


          pgfmathsetmacroN4;
          pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
          draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);


          % alternative: an ycomb plot
          addplot [line width=0.15mm, red, forget plot, ycomb, samples at=4,...,0] (x/4,10);
          endaxis

          endtikzpicture
          enddocument





          share|improve this answer
































            5














            Welcome to TeX.SE!



            (Torbjørn T. has been a bit faster than me...)



            The problem is that your foreach loop is run in a place where coordinates don't correspond to what you think. You can try adding:



            draw[green] (1,0) circle (1pt);


            after your loop and you should understand that abscissa 1 in the TikZ xyz coordinate system doesn't correspond to abscissa 1 for the plot.



            Inside the axis environment, however, coordinates use the axis coordinate system starting from pgfplots compatibility level 1.11. In older compatibility levels, one can use this coordinate system like this: (axis cs:1,0) for point (1,0) in the axis coordinate system. But let's assume for simplicity of the code that we can afford compatibility level 1.11 or later (otherwise, just add two axis cs:).



            In order to use the axis coordinate system to draw your vertical lines, you need to move the foreach loop inside the axis environment. This brings a new difficulty, as things inside that environment are often expanded late: see foreach not behaving in axis environment. Fortunately, both solutions given there work fine. With the simpler solution, the full example becomes:



            documentclassarticle
            usepackagepgfplots
            pgfplotssetcompat=1.11% or later

            begindocument
            begintikzpicture
            beginaxis
            [ grid,
            anchor=origin,
            %axis equal=false,
            xmin=0, xmax=1,
            ymin=0, ymax=10,
            minor tick num=1,
            axis lines = middle,
            tick label style=inner sep=2pt, font=footnotesize,
            %
            every axis plot post/.append style=thick,
            samples=500 ]
            addplot [domain=0:1, color=blue] -2*ln(x);

            % Add lines (alternatively: defN4). No semi-colon is needed in
            % either case: these are not TikZ statements.
            pgfmathsetmacroN4

            pgfplotsinvokeforeach N,...,0
            draw[line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);

            endaxis
            endtikzpicture
            enddocument


            As said, there is a second way to write the loop:



            foreach x in N,...,0 
            edeftempnoexpanddraw [line width=0.15mm, red]
            (x/N, 0) -- (x/N, 10);
            temp



            Both give the same outcome.



            Screenshot






            share|improve this answer





























              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              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%2f501512%2ftikz-coordinates-error-when-using-draw-with-axis%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6














              The coordinate system of the axis is not the same as that of the underlying tikzpicture, so when you add a draw outside the axis you cannot expect the coordinates to correspond to those inside the axis.



              Instead, use



              pgfmathsetmacroN4;
              pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
              draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);



              before endaxis. (Or an ycomb plot, as in the example below.)



              documentclass[11pt]article
              usepackagepgfplots
              pgfplotssetcompat=1.16 % it's recommended to use an explicit version number

              begindocument
              begintikzpicture
              beginaxis
              [
              width=columnwidth, % I would generally not use resizebox
              grid,
              anchor=origin,
              %axis equal=false,
              xmin=0, xmax=1,
              ymin=0, ymax=10,
              minor tick num=1,
              axis lines = middle,
              tick label style=inner sep=2pt, font=footnotesize,
              %
              every axis plot post/.append style=thick,
              samples=500
              ]
              addplot [domain=0:1, color=blue] -2*ln(x);


              pgfmathsetmacroN4;
              pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
              draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);


              % alternative: an ycomb plot
              addplot [line width=0.15mm, red, forget plot, ycomb, samples at=4,...,0] (x/4,10);
              endaxis

              endtikzpicture
              enddocument





              share|improve this answer





























                6














                The coordinate system of the axis is not the same as that of the underlying tikzpicture, so when you add a draw outside the axis you cannot expect the coordinates to correspond to those inside the axis.



                Instead, use



                pgfmathsetmacroN4;
                pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
                draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);



                before endaxis. (Or an ycomb plot, as in the example below.)



                documentclass[11pt]article
                usepackagepgfplots
                pgfplotssetcompat=1.16 % it's recommended to use an explicit version number

                begindocument
                begintikzpicture
                beginaxis
                [
                width=columnwidth, % I would generally not use resizebox
                grid,
                anchor=origin,
                %axis equal=false,
                xmin=0, xmax=1,
                ymin=0, ymax=10,
                minor tick num=1,
                axis lines = middle,
                tick label style=inner sep=2pt, font=footnotesize,
                %
                every axis plot post/.append style=thick,
                samples=500
                ]
                addplot [domain=0:1, color=blue] -2*ln(x);


                pgfmathsetmacroN4;
                pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
                draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);


                % alternative: an ycomb plot
                addplot [line width=0.15mm, red, forget plot, ycomb, samples at=4,...,0] (x/4,10);
                endaxis

                endtikzpicture
                enddocument





                share|improve this answer



























                  6












                  6








                  6







                  The coordinate system of the axis is not the same as that of the underlying tikzpicture, so when you add a draw outside the axis you cannot expect the coordinates to correspond to those inside the axis.



                  Instead, use



                  pgfmathsetmacroN4;
                  pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
                  draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);



                  before endaxis. (Or an ycomb plot, as in the example below.)



                  documentclass[11pt]article
                  usepackagepgfplots
                  pgfplotssetcompat=1.16 % it's recommended to use an explicit version number

                  begindocument
                  begintikzpicture
                  beginaxis
                  [
                  width=columnwidth, % I would generally not use resizebox
                  grid,
                  anchor=origin,
                  %axis equal=false,
                  xmin=0, xmax=1,
                  ymin=0, ymax=10,
                  minor tick num=1,
                  axis lines = middle,
                  tick label style=inner sep=2pt, font=footnotesize,
                  %
                  every axis plot post/.append style=thick,
                  samples=500
                  ]
                  addplot [domain=0:1, color=blue] -2*ln(x);


                  pgfmathsetmacroN4;
                  pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
                  draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);


                  % alternative: an ycomb plot
                  addplot [line width=0.15mm, red, forget plot, ycomb, samples at=4,...,0] (x/4,10);
                  endaxis

                  endtikzpicture
                  enddocument





                  share|improve this answer













                  The coordinate system of the axis is not the same as that of the underlying tikzpicture, so when you add a draw outside the axis you cannot expect the coordinates to correspond to those inside the axis.



                  Instead, use



                  pgfmathsetmacroN4;
                  pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
                  draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);



                  before endaxis. (Or an ycomb plot, as in the example below.)



                  documentclass[11pt]article
                  usepackagepgfplots
                  pgfplotssetcompat=1.16 % it's recommended to use an explicit version number

                  begindocument
                  begintikzpicture
                  beginaxis
                  [
                  width=columnwidth, % I would generally not use resizebox
                  grid,
                  anchor=origin,
                  %axis equal=false,
                  xmin=0, xmax=1,
                  ymin=0, ymax=10,
                  minor tick num=1,
                  axis lines = middle,
                  tick label style=inner sep=2pt, font=footnotesize,
                  %
                  every axis plot post/.append style=thick,
                  samples=500
                  ]
                  addplot [domain=0:1, color=blue] -2*ln(x);


                  pgfmathsetmacroN4;
                  pgfplotsinvokeforeachN,...,0 % special looping defined by pgfplots
                  draw [line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);


                  % alternative: an ycomb plot
                  addplot [line width=0.15mm, red, forget plot, ycomb, samples at=4,...,0] (x/4,10);
                  endaxis

                  endtikzpicture
                  enddocument






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 26 at 9:28









                  Torbjørn T.Torbjørn T.

                  164k13 gold badges268 silver badges454 bronze badges




                  164k13 gold badges268 silver badges454 bronze badges


























                      5














                      Welcome to TeX.SE!



                      (Torbjørn T. has been a bit faster than me...)



                      The problem is that your foreach loop is run in a place where coordinates don't correspond to what you think. You can try adding:



                      draw[green] (1,0) circle (1pt);


                      after your loop and you should understand that abscissa 1 in the TikZ xyz coordinate system doesn't correspond to abscissa 1 for the plot.



                      Inside the axis environment, however, coordinates use the axis coordinate system starting from pgfplots compatibility level 1.11. In older compatibility levels, one can use this coordinate system like this: (axis cs:1,0) for point (1,0) in the axis coordinate system. But let's assume for simplicity of the code that we can afford compatibility level 1.11 or later (otherwise, just add two axis cs:).



                      In order to use the axis coordinate system to draw your vertical lines, you need to move the foreach loop inside the axis environment. This brings a new difficulty, as things inside that environment are often expanded late: see foreach not behaving in axis environment. Fortunately, both solutions given there work fine. With the simpler solution, the full example becomes:



                      documentclassarticle
                      usepackagepgfplots
                      pgfplotssetcompat=1.11% or later

                      begindocument
                      begintikzpicture
                      beginaxis
                      [ grid,
                      anchor=origin,
                      %axis equal=false,
                      xmin=0, xmax=1,
                      ymin=0, ymax=10,
                      minor tick num=1,
                      axis lines = middle,
                      tick label style=inner sep=2pt, font=footnotesize,
                      %
                      every axis plot post/.append style=thick,
                      samples=500 ]
                      addplot [domain=0:1, color=blue] -2*ln(x);

                      % Add lines (alternatively: defN4). No semi-colon is needed in
                      % either case: these are not TikZ statements.
                      pgfmathsetmacroN4

                      pgfplotsinvokeforeach N,...,0
                      draw[line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);

                      endaxis
                      endtikzpicture
                      enddocument


                      As said, there is a second way to write the loop:



                      foreach x in N,...,0 
                      edeftempnoexpanddraw [line width=0.15mm, red]
                      (x/N, 0) -- (x/N, 10);
                      temp



                      Both give the same outcome.



                      Screenshot






                      share|improve this answer































                        5














                        Welcome to TeX.SE!



                        (Torbjørn T. has been a bit faster than me...)



                        The problem is that your foreach loop is run in a place where coordinates don't correspond to what you think. You can try adding:



                        draw[green] (1,0) circle (1pt);


                        after your loop and you should understand that abscissa 1 in the TikZ xyz coordinate system doesn't correspond to abscissa 1 for the plot.



                        Inside the axis environment, however, coordinates use the axis coordinate system starting from pgfplots compatibility level 1.11. In older compatibility levels, one can use this coordinate system like this: (axis cs:1,0) for point (1,0) in the axis coordinate system. But let's assume for simplicity of the code that we can afford compatibility level 1.11 or later (otherwise, just add two axis cs:).



                        In order to use the axis coordinate system to draw your vertical lines, you need to move the foreach loop inside the axis environment. This brings a new difficulty, as things inside that environment are often expanded late: see foreach not behaving in axis environment. Fortunately, both solutions given there work fine. With the simpler solution, the full example becomes:



                        documentclassarticle
                        usepackagepgfplots
                        pgfplotssetcompat=1.11% or later

                        begindocument
                        begintikzpicture
                        beginaxis
                        [ grid,
                        anchor=origin,
                        %axis equal=false,
                        xmin=0, xmax=1,
                        ymin=0, ymax=10,
                        minor tick num=1,
                        axis lines = middle,
                        tick label style=inner sep=2pt, font=footnotesize,
                        %
                        every axis plot post/.append style=thick,
                        samples=500 ]
                        addplot [domain=0:1, color=blue] -2*ln(x);

                        % Add lines (alternatively: defN4). No semi-colon is needed in
                        % either case: these are not TikZ statements.
                        pgfmathsetmacroN4

                        pgfplotsinvokeforeach N,...,0
                        draw[line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);

                        endaxis
                        endtikzpicture
                        enddocument


                        As said, there is a second way to write the loop:



                        foreach x in N,...,0 
                        edeftempnoexpanddraw [line width=0.15mm, red]
                        (x/N, 0) -- (x/N, 10);
                        temp



                        Both give the same outcome.



                        Screenshot






                        share|improve this answer





























                          5












                          5








                          5







                          Welcome to TeX.SE!



                          (Torbjørn T. has been a bit faster than me...)



                          The problem is that your foreach loop is run in a place where coordinates don't correspond to what you think. You can try adding:



                          draw[green] (1,0) circle (1pt);


                          after your loop and you should understand that abscissa 1 in the TikZ xyz coordinate system doesn't correspond to abscissa 1 for the plot.



                          Inside the axis environment, however, coordinates use the axis coordinate system starting from pgfplots compatibility level 1.11. In older compatibility levels, one can use this coordinate system like this: (axis cs:1,0) for point (1,0) in the axis coordinate system. But let's assume for simplicity of the code that we can afford compatibility level 1.11 or later (otherwise, just add two axis cs:).



                          In order to use the axis coordinate system to draw your vertical lines, you need to move the foreach loop inside the axis environment. This brings a new difficulty, as things inside that environment are often expanded late: see foreach not behaving in axis environment. Fortunately, both solutions given there work fine. With the simpler solution, the full example becomes:



                          documentclassarticle
                          usepackagepgfplots
                          pgfplotssetcompat=1.11% or later

                          begindocument
                          begintikzpicture
                          beginaxis
                          [ grid,
                          anchor=origin,
                          %axis equal=false,
                          xmin=0, xmax=1,
                          ymin=0, ymax=10,
                          minor tick num=1,
                          axis lines = middle,
                          tick label style=inner sep=2pt, font=footnotesize,
                          %
                          every axis plot post/.append style=thick,
                          samples=500 ]
                          addplot [domain=0:1, color=blue] -2*ln(x);

                          % Add lines (alternatively: defN4). No semi-colon is needed in
                          % either case: these are not TikZ statements.
                          pgfmathsetmacroN4

                          pgfplotsinvokeforeach N,...,0
                          draw[line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);

                          endaxis
                          endtikzpicture
                          enddocument


                          As said, there is a second way to write the loop:



                          foreach x in N,...,0 
                          edeftempnoexpanddraw [line width=0.15mm, red]
                          (x/N, 0) -- (x/N, 10);
                          temp



                          Both give the same outcome.



                          Screenshot






                          share|improve this answer















                          Welcome to TeX.SE!



                          (Torbjørn T. has been a bit faster than me...)



                          The problem is that your foreach loop is run in a place where coordinates don't correspond to what you think. You can try adding:



                          draw[green] (1,0) circle (1pt);


                          after your loop and you should understand that abscissa 1 in the TikZ xyz coordinate system doesn't correspond to abscissa 1 for the plot.



                          Inside the axis environment, however, coordinates use the axis coordinate system starting from pgfplots compatibility level 1.11. In older compatibility levels, one can use this coordinate system like this: (axis cs:1,0) for point (1,0) in the axis coordinate system. But let's assume for simplicity of the code that we can afford compatibility level 1.11 or later (otherwise, just add two axis cs:).



                          In order to use the axis coordinate system to draw your vertical lines, you need to move the foreach loop inside the axis environment. This brings a new difficulty, as things inside that environment are often expanded late: see foreach not behaving in axis environment. Fortunately, both solutions given there work fine. With the simpler solution, the full example becomes:



                          documentclassarticle
                          usepackagepgfplots
                          pgfplotssetcompat=1.11% or later

                          begindocument
                          begintikzpicture
                          beginaxis
                          [ grid,
                          anchor=origin,
                          %axis equal=false,
                          xmin=0, xmax=1,
                          ymin=0, ymax=10,
                          minor tick num=1,
                          axis lines = middle,
                          tick label style=inner sep=2pt, font=footnotesize,
                          %
                          every axis plot post/.append style=thick,
                          samples=500 ]
                          addplot [domain=0:1, color=blue] -2*ln(x);

                          % Add lines (alternatively: defN4). No semi-colon is needed in
                          % either case: these are not TikZ statements.
                          pgfmathsetmacroN4

                          pgfplotsinvokeforeach N,...,0
                          draw[line width=0.15mm, red] (#1/N, 0) -- (#1/N, 10);

                          endaxis
                          endtikzpicture
                          enddocument


                          As said, there is a second way to write the loop:



                          foreach x in N,...,0 
                          edeftempnoexpanddraw [line width=0.15mm, red]
                          (x/N, 0) -- (x/N, 10);
                          temp



                          Both give the same outcome.



                          Screenshot







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 26 at 11:47

























                          answered Jul 26 at 9:33









                          frougonfrougon

                          6,9351 gold badge13 silver badges22 bronze badges




                          6,9351 gold badge13 silver badges22 bronze badges






























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f501512%2ftikz-coordinates-error-when-using-draw-with-axis%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                              Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                              Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림