2

I want to plot a small vertical line from y = 0 on certain x coordinates, connected to a node. However, the vertical line does not stick/connect correctly to the x-axis.

\documentclass{scrreprt}
        \usepackage{pgfplots}
        \usepackage{pgfplotstable}
        \usepackage{pdflscape}
        \pgfplotsset{compat=newest}

     \begin{document}
                \begin{landscape}
                    \begin{tikzpicture}
                             \begin{axis}[
                                  width=1.15\textwidth,
                                  height=0.80\textheight,
                                  scale only axis,
                                  xmin=-9,xmax=45,
                                  ymin=0,ymax=2200,
                                  axis y line*=left,
                                  xlabel={x axis},
                                  ylabel={y axis}]

                                  %Step sections
                                  \node[rotate=90,right] (s2) at (axis cs: -6.86,100){\tiny{NODE 0}};
                                  \node(d2) at (axis cs:-6.86,\pgfkeysvalueof{/pgfplots/ymin}) {};
                                  \draw(s2)--(d2);
                                  \node[rotate=90,right] (s3) at (axis cs: 0,100){\tiny{NODE 1}};
                                  \node(d3) at (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) {};
                                  \draw(s3)--(d3);
                                  \node[rotate=90,right] (s6) at (axis cs: 31.4,100){\tiny{NODE 2}};
                                  \node(d6) at (axis cs:31.4,\pgfkeysvalueof{/pgfplots/ymin}) {};
                                  \draw(s6)--(d6);
                               \end{axis}
                        \end{tikzpicture}
                    \end{landscape}
       \end{document}

See below for a impression:

  • 2
    Basically, a node has a size, even if it doesn't have content, and when you draw a line to a node, the line terminates at the border of the node. For a case like this you should use \coordinate (foo) at (x,y); instead of a normal \node. Note also that \tiny is not a macro that takes an argument (like e.g. \textbf{bold}), it is a switch which influences all the following text in the same scope. To limit the effect you should thus use {\tiny ..}, not \tiny{..}, but inside a \node you can leave out the braces altogether. – Torbjørn T. Feb 26 '18 at 21:07
4

It is better idea to draw the lines first with the exact dimensions you want and then place on their "rotated right" the text you want:

\documentclass{report}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pdflscape}
\pgfplotsset{compat=newest}

\begin{document}
\begin{landscape}
\begin{tikzpicture}
\begin{axis}[
             width=1.15\textwidth,
             height=0.80\textheight,
             scale only axis,
             xmin=-9,xmax=45,
             ymin=0,ymax=2200,
             axis y line*=left,
            xlabel={x axis},
            ylabel={y axis}]

            %Step sections
            \draw (axis cs: -6.86,0)--(axis cs:-6.86,100) coordinate (s2);
            \node[rotate=90,right]at (s2) {\tiny NODE 0 };
           \end{axis}
    \end{tikzpicture}
   \end{landscape}
   \end{document}

Output:

enter image description here

Notice that I used coordinate to name the above point. Now... all this can me replaced by one instead of two commands:

    \documentclass{report}
    \usepackage{pgfplots}
    \usepackage{pgfplotstable}
    \usepackage{pdflscape}
    \pgfplotsset{compat=newest}

 \begin{document}
            \begin{landscape}
                \begin{tikzpicture}
                         \begin{axis}[
                              width=1.15\textwidth,
                              height=0.80\textheight,
                              scale only axis,
                              xmin=-9,xmax=45,
                              ymin=0,ymax=2200,
                              axis y line*=left,
                              xlabel={x axis},
                              ylabel={y axis}]

                           %Step sections
                           \draw (axis cs: -6.86,0)--(axis cs:-6.86,100) node[rotate=90,right] {\tiny NODE 0 };
   \end{axis}
   \end{tikzpicture}
   \end{landscape}
   \end{document}

Used node this time. Same output.

3

The main "fault" was that you just used a \node as starting point and thus, when you draw a line from that node it is used an appropriate "outer anchor" of that node to draw the line (which is normally the desired behavior. To make this visible add draw to one of the \node options and maybe in addition some text in that \node, i.e. write something in the empty curly brackets {}).

There are two possible simple solutions if you want to stick to your solution. Either add coordinate to the \node options or add .center to the (d...) coordinate in your draw command, e.g. write \draw(s2)--(d2.center);.

But here I present another solution. I create a custom style Pin for pin and modify it to your needs. Then it can easily be used with a minimum of code to write for that Pins.

For details please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher so TikZ coordinates don't have to be
        % prefixed by `axis cs:'
        compat=1.11,
    }
\begin{document}
\begin{tikzpicture}[
    % create a style to draw the annotations
    Pin/.style={
        % we need only a coordinate as a "base node"
        coordinate,
        % then we draw a pin to the "base node" using the following options
        pin={[%
                pin distance=4ex,
                pin edge={
                    draw=black,
                    thin,
                },
                node font=\tiny,
            ]above:{\rotatebox{90}{#1}}%
        },
    },
]
    \begin{axis}[
        scale only axis,
        xmin=-9,xmax=45,
        ymin=0,ymax=2200,
        xlabel={x axis},
        ylabel={y axis},
    ]
        %Step sections
        \node [Pin=NODE 0] at (-6.86,\pgfkeysvalueof{/pgfplots/ymin}) {};
        \node [Pin=NODE 1] at ( 0   ,\pgfkeysvalueof{/pgfplots/ymin}) {};
        \node [Pin=NODE 2] at (31.4 ,\pgfkeysvalueof{/pgfplots/ymin}) {};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.