44

I frequently need to draw a horizontal or vertical line passing through the axis origin that spans the entire width or height of my plot area. I don't want to use axis lines=middle, because the tick labels clutter up the plot area. So far, I've been using something like \addplot [black, no markers] coordinates (-5,0) (5,0); to draw a horizontal line, but the approach doesn't feel right, and it's not very flexible: If I change my axis range, the line might be too short, and I have to explicitly set xmin and xmax because otherwise the line influences the plot limits.

What's the proper way to add a vertical or horizontal line passing through a certain point (typically the axis origin) that spans the entire height/width of a plot?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=-6,xmax=6
]
\addplot {rand};
\addplot [red, no markers] coordinates {(-7,0) (7,0)};
\end{axis}
\end{tikzpicture}
\end{document}
1
  • This is something that pops up a fair bit, and I thought it would be good to have a "detailed, canonical answer" showing how to do this right.
    – Jake
    May 13, 2012 at 20:39

4 Answers 4

36

pgfplots stores the axis limit information in xmin,ymin,xmax,xmin keys (Jake reminded that this was not the case in the earlier versions) and we can access those key values via \pgfkeysvalueof{} command.

The advantage of this is that the coordinates snap to the location of the origin and more importantly it does not disturb the bounding box if the origin is not visible in the particular plots. If this has to be done frequently, one can create a style where the additional commands goes into the

after end axis/.append code={
   \draw[ultra thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
    \draw[ultra thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
}

This would add these commands to the instruction list when the axis processing is finished.

The disadvantage of this method is that it does not work with logarithmic plots. In that case, you can use

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

to draw the lines.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot {rand};
\draw[ultra thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
\draw[ultra thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

3
  • Oh yeah, this is a nice one. This didn't use to work in older versions (<= 1.4.1, I think) if xmin and xmax weren't explicitly supplied, but it seems that it's fine now. That's even better than the one I was going to suggest (\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});), because it's much more readable. Thank you!
    – Jake
    May 14, 2012 at 7:51
  • I would add those lines of code directly after \begin{axis}, otherwise the lines that are drawn will lie on top of the plotted data. Besides, I see now that the lines will lie on top of the frame (I don't know how to put them behind it), so if they are given any other color than the frame color it will be noticeable in the figure. Dec 22, 2012 at 14:53
  • They are on top of the plotted data no matter where i put it. Sep 11, 2019 at 8:57
38

I do this using the extra ticks mechanism:

  \begin{axis}
    [
      extra y ticks       = 0,
      extra y tick labels = ,
      extra y tick style  = { grid = major },
      ...

This generates a relatively 'light' line with the standard settings: in my plots the appropriate style is something which is a guide to the eye but not overly heavily. I'd imagine that a similar approach can be used to create coloured lines by correct choice of style.

5
  • 6
    Nice approach! This also makes it easy to highlight positions other than the origin, as adding a properly positioned label is trivial. The style of the line can be set using extra y tick style = { grid = major, major grid style = <options> }. This is independent of the normal grid style. The only drawback I can see is that you can't use this approach if you're already using extra y ticks for something else.
    – Jake
    May 14, 2012 at 13:10
  • Although, don't the extra ticks appear in bold face, something that might not be desirable? Maybe this can be prevented somehow? (I just posted a question about this: tex.stackexchange.com/questions/58140/…)
    – andreasdr
    Jun 1, 2012 at 14:56
  • @andreasdr: Note that Joseph set extra y tick labels=,, so the extra ticks are not labeled, and therefore not printed twice.
    – Jake
    Jun 1, 2012 at 15:31
  • Sorry, my mistake!
    – andreasdr
    Jun 1, 2012 at 16:03
  • It should be 'before end axis', as otherwise the lines are not clipped and may be drawn outside of the plots, if ymin > 0 or ymax < 0
    – Theo
    Sep 8, 2015 at 13:32
2

Within the axis just plot a line:

\addplot[color=red] coordinates {(-6,0) (6,0)};
1
  • Hi fuzzifikation, thanks for the answer! In my quesiton, I mention the possibility of just using a plot, and give a few reasons why I don't think it's a very good approach in many cases.
    – Jake
    Oct 27, 2015 at 19:32
-1

Another method is to add a new plot after the main plots:

\addplot [color=gray, dashed,line width=0.4pt]

  table[row sep=crcr]{%

0.0  0\\\

1  0\\\

100  0\\\

};

pick x-axis values based on your own data.

1
  • Yes, this is exactly the same suggested in the previous answer.
    – Ajay Singh
    Jan 17, 2021 at 11:22

You must log in to answer this question.

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