3

I am trying to adjust the position of a node in a tikz figure but for some reason the dot that I create doesn't show in the appropriate coordinate (0,8/3) that is imposed.

@codeR suggested me to put the line \node[dot, label=left:$\lambda_{E-G}$] at (0,8/3) {}; but despite his help, I still have the same problem.

Code :

\documentclass[margin=3mm,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{angles, arrows.meta,
                quotes}
\usepackage{siunitx}

\begin{document}
\begin{tikzpicture}[scale=1,
the_angle/.style args = {#1/#2}{draw,<->,
                               angle radius=#1,
                               angle eccentricity=#2,
                               %anchor=south west,
                               }, % angle label position
dot/.style = {circle, fill, inner sep=-1.2pt}
]
\pgfplotsset{ticks=none}
  \begin{axis}[axis lines=middle,xmin=0,xmax=15,ymin=0,ymax=7, ultra thick,
    xlabel=$\scriptstyle \log(t)$,
    ylabel=$\scriptstyle \log(R)$,
    xlabel style={at={(1,-0.01)},anchor=north},
        ylabel style={at={(-0.15,1)},anchor=west},
        ]
       %
        \addplot+[no marks,blue,domain=0:8,samples=300, thick] {(1/3)*x} coordinate (A);
        \coordinate (O) at (0,0); % intersection with coordinate axis
        \coordinate (C) at (2,0);
        \addplot+[no marks,dashed,blue,domain=8:14,samples=300, thin] {(1/3)*(x-8)+8/3};
        \addplot+[no marks,green,domain=8:14,samples=300, thick] {(1/2)*(x-8)+8/3};
        \addplot+[no marks,dashed,green,domain=0:8,samples=300, thin] {(1/2)*(x-8)+8/3};

        \draw[gray, thick, dash pattern=on 6pt off 3pt] (0,8/3) -- (8,8/3);
  \end{axis}
      \pic [the_angle=9mm/1.2, "\qty{30}{\degree}"] {angle = C--O--A};
      \node[dot, label=left:$\lambda_{E-G}$] at (0,8/3) {};
\end{tikzpicture}

\end{document}

Any help would be appreciated : thank you !

Sincerely,

enter image description here

P.S : Is there a way to make the axis lines joining exactly in the same point ? Why is there an offset between the x-axis and y-axis despite having (0,0) as origin ? Thank you

1
  • Try adding the clip=true option in the argument list of \begin{axis} the offset issue should be resolved. For the gap at the origin, it is also present in the pgfplot manual itself. Some lousy hacks can be used as a workaround.
    – codeR
    Commented Apr 1 at 12:38

3 Answers 3

3

The problem is that the axis environment has a different "scope", defined by the origin of the coordinate system (try \begin{axis}[..., ymin=-5] to see this). The offset that you observed is due to the annotation at the axes arrows. The solution is to "save" the coordinate of the point from inside the axis environment and use it afterwards. I'm just giving the last few lines

        \begin{axis}[...]
            ...
            \draw[gray, thick, dash pattern=on 6pt off 3pt] (0,8/3) -- (8,8/3);
            \node (abc) at (0,8/3) {};
        \end{axis}
        \pic [the_angle=9mm/1.2, "\qty{30}{\degree}"] {angle = C--O--A};
        \node[dot, label=left:$\lambda_{E-G}$] at (abc) {};
    \end{tikzpicture}

This results in the following plot:

enter image description here

I assume that this is what you wanted?

5
  • You can also use \coordinate (abc) at (0,8/3); as you need only to define a coordinate.
    – jlab
    Commented Apr 1 at 13:27
  • Ture -- that's probably also more consistent since we just need the label of the coordinate, and using \node suggest that there is actual content to be placed.
    – BanDoP
    Commented Apr 1 at 14:01
  • this is awesome : thank you ! but please could you elaborate on this statement The offset that you observed is due to the annotation at the axes arrows. How so ?
    – Waxler
    Commented Apr 1 at 16:59
  • 1
    The axis-block within the tikzpicture has its own coordinate system (lower left corner = (0|0)). Therefore, the label log(t) shifts the "plot coordinate system" a bit upwards relative to the "axis coordinate system" within the tikzpicture (try removing the "log(t)" and observe how the point's position in your original code shifts). This is actually a feature and not a bug, even though it might sound complicated ;)
    – BanDoP
    Commented Apr 1 at 17:57
  • okay !! I didn't know that : thank you again ! and I presume that the same holds for the "log(R)" : this was not intuitive !
    – Waxler
    Commented Apr 1 at 19:59
3

To join the two axis lines, add line cap=rect to the axis config.

The axis coordinate system is set between \begin{axis} and \end{axis}. You can save the position as coordinate to use it outside the axis environment, as proposed by @BanDoP.

An alternative is to move the \node command inside the axis environment. The setting clip mode=individual must be set, in this case, to avoid the clipping of the paths drawn with \draw or \node commands.

Full example (with angle label moved out of the line):

\documentclass[margin=3mm,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{angles, arrows.meta, quotes}
\usepackage{siunitx}

\begin{document}
\begin{tikzpicture}[%
    scale=1,
    the_angle/.style args = {#1/#2}{draw,<->,
      angle radius=#1,
      angle eccentricity=#2,
      % anchor=south west,
    }, % angle label position
  dot/.style = {circle, fill, inner sep=-1.2pt}
]
\pgfplotsset{ticks=none}
\begin{axis}[%
  axis lines=middle, ultra thick,
  line cap=rect,% <-- join axis lines
  xmin=0, xmax=15, ymin=0, ymax=7,
  xlabel=$\scriptstyle \log(t)$,
  ylabel=$\scriptstyle \log(R)$,
  xlabel style={at={(1,-0.01)}, anchor=north},
  ylabel style={at={(-0.15,1)}, anchor=west},
  clip mode=individual,% <-- don't clip tikz commands like \node or \draw
]
    %
    \addplot+[no marks, blue, domain=0:8, samples=300,  thick] {(1/3)*x} coordinate (A);
    \coordinate (O) at (0,0); % intersection with coordinate axis
    \coordinate (C) at (2,0);
    \addplot+[no marks, dashed, blue, domain=8:14, samples=300, thin] {(1/3)*(x-8)+8/3};
    \addplot+[no marks, green, domain=8:14, samples=300, thick] {(1/2)*(x-8)+8/3};
    \addplot+[no marks, dashed, green, domain=0:8, samples=300, thin] {(1/2)*(x-8)+8/3};

    \draw[gray, thick, dash pattern=on 6pt off 3pt] (0,8/3) -- (8,8/3);
    \node[dot, label=left:$\lambda_{E-G}$] at (0,8/3) {};
  \end{axis}
  \pic [the_angle=9mm/1.2, "\qty{30}{\degree}", angle eccentricity=1.4] {angle = C--O--A};
\end{tikzpicture}

\end{document}

solution, with \node command inside axis env

1
  • thank you @jlab : I didn't know that the axis environment was designed like that : I am learning new tips here. Also, if it was possible to validate 2 answers I would have validated yours : thank you again for your detailed explanations. Appreciated !
    – Waxler
    Commented Apr 1 at 20:04
3
  • We can use extra y ticks

  • For these functions, samples=2 is sufficient

  • To mark the point with dot, you can do it in the outline, and add clip mode=individual

    \draw[gray, thick, dash pattern=on 6pt off 3pt] (0,8/3) node[dot,node contents=]-- (8,8/3);
    
  • With siunitx we can use \ang{30} instead of \qty{30}{\degree}

     % \documentclass[margin=3mm,tikz]{standalone}
     \documentclass[margin=3mm]{standalone}%<-pgfplots load tikz
     \usepackage{pgfplots}
     \pgfplotsset{compat=newest}
     \usetikzlibrary{angles, arrows.meta,
                     quotes}
     \usepackage{siunitx}
    
     \begin{document}
     \begin{tikzpicture}[scale=1,
     the_angle/.style args = {#1/#2}{draw,<->,
                                 angle radius=#1,
                                 angle eccentricity=#2,
                                 %anchor=south west,
                                 }, % angle label position
     % dot/.style = {circle, fill, inner sep=-1.2pt}
     dot/.style = {circle, fill=black, inner sep=0pt,minimum size=2mm},
     ]
     % \pgfplotsset{ticks=none}
     \begin{axis}[
         clip mode=individual,%<-- added if dot marker
         axis lines=middle,
         xmin=0,xmax=15,
         ymin=0,ymax=7, 
         ultra thick,
         xlabel=$\scriptstyle \log(t)$,
         ylabel=$\scriptstyle \log(R)$,
         xlabel style={at={(1,-0.01)},anchor=north},
         ylabel style={at={(-0.15,1)},anchor=west},
         %
         %    added
         %
         extra y ticks={8/3},
         extra y tick labels={$\lambda_{E-G}$},
         % ytick style={
         %         /pgfplots/major tick length=2mm,red,very thick},
         xtick=\empty,
         ytick=\empty,
             ]
         %
             \addplot+[no marks,blue,domain=0:8,samples=2, thick] {(1/3)*x} coordinate (A);
             \coordinate (O) at (0,0); % intersection with coordinate axis
             \coordinate (C) at (2,0);
             \addplot+[no marks,dashed,blue,domain=8:14,samples=2, thin] {(1/3)*(x-8)+8/3};
             \addplot+[no marks,green,domain=8:14,samples=2, thick] {(1/2)*(x-8)+8/3};
             \addplot+[no marks,dashed,green,domain=0:8,samples=2, thin] {(1/2)*(x-8)+8/3};
             \draw[gray, thick, dash pattern=on 6pt off 3pt] (0,8/3) node[dot,node contents=]-- (8,8/3);
             %\node [dot] at (axis cs:0,8/3) {};
         \end{axis}
         \pic [the_angle=9mm/1.3, "\ang{30}"] {angle = C--O--A};
     \end{tikzpicture}
    
     \end{document}
    

enter image description here

You must log in to answer this question.

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