2

I'm trying to draw a straight line from the top of a graph to its bottom without defining specific y-values but only saying from 0% to 100% of the y-axis. The x-value, however, should be set in the coordinate system of the graph.

I'm running compat=1.15, so I don't need to add axis cs: before the coordinates to let them match the real graph.

I think, that I'd need to use two different coordinate systems. For the x-value the default axis cs comes in handy but for the y-value I'd rather need the axis description cs. Please note that the default graphs in pgfplots do have a little margin on each side, so even if I would enter the y-values manually the line wouldn't be from the very top to the very bottom, which it should be.

As can be seen in the MWE, setting a value that is smaller than the smallest plotting value gives the correct result, so a possible hack could be to just use extreme numbers, but I don't like that idea.

MWE

\documentclass{article}

\usepackage{pgfplots, filecontents}

\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        xlabel = {x},
        ylabel = {y},
    ]
        \addplot+[mark=none, smooth] plot coordinates {
            (200, 20)
            (210, 22)
            (220, 23)
            (230, 25)
            (240, 35)
            (250, 40)
            (260, 36)
            (270, 27)
            (280, 20)
            (290, 19)
            (300, 15)
        };

        \draw (250, 0) -- (250, 40);
    \end{axis}
\end{tikzpicture}

\end{document}
  • 3
    Try \draw (current axis.south-|<x>, 0) -- (current axis.north-|<x>,0); where <x> is your desired x value. – Max Aug 23 '18 at 8:07
  • 3
    Or \draw (250,0 |- {axis description cs:0,0}) -- (250,0 |- {axis description cs:0,1}); – Stefan Pinnow Aug 23 '18 at 8:09
  • 2
    @Sam Sure, the -| means that I want to use the intersection of a horizontal (-) line coming from the first coordinate, and a vertical (|) line coming from the second coordinate. Similarly, the |- means the same, but the other way around. In principle this means that for -| I am using the y coordinate of the first point, and the x coordinate of the second point, discarding the x of the first point and the y of the second point. – Max Aug 23 '18 at 8:15
  • 2
    @Sam With first and second coordinate I mean the coordinates within the parentheses. So (current axis.south-|250,0) uses the y value of current axis.south, which is the bottom of the axis, and the x value of the point (250,0), expressed in the axis cs due to compat=1.15. – Max Aug 23 '18 at 8:17
  • 3
    I'm voting to close this question because it is solved in the comments. – Stefan Pinnow Aug 29 '18 at 5:06
3

This can be done with

\draw (current axis.south-|<x>, 0) -- (current axis.north-|<x>,0);

Or as Stefan Pinnow commented with

\draw (<x>,0 |- {axis description cs:0,0}) -- (<x>,0 |- {axis description cs:0,1});

Which both use the perpendicular coordinate system, which takes two points (let's call them p and q), draws a vertical line from one and a horizontal line from the other, and returns the intersection point of these lines. The implicit syntax can be used as: (p-|q) or (p|-q).

The -| means that I want to use the intersection of a horizontal (-) line coming from p, and a vertical (|) line coming from q. Similarly, the |- means the same, but the other way around. In principle this means that for -| I am using the y coordinate of p, and the x coordinate of q, discarding the x of p and the y of q.

MWE:

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{compat=1.15}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        xlabel = {x},
        ylabel = {y},
    ]
        \addplot+[mark=none, smooth] plot coordinates {
            (200, 20)
            (210, 22)
            (220, 23)
            (230, 25)
            (240, 35)
            (250, 40)
            (260, 36)
            (270, 27)
            (280, 20)
            (290, 19)
            (300, 15)
        };

        \draw (current axis.south-|240, 0) -- (current axis.north-|240,0);

        \draw (260,0 |- {axis description cs:0,0}) -- (260,0 |- {axis description cs:0,1});

    \end{axis}
\end{tikzpicture}

\end{document}

Resulting in

enter image description here

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.