1

I took a crack at this question earlier but deleted it because I think the subject line was misleading. My apologies for any confusion that might have caused.

My task is to draw a simple vertical line at x=1 (a probability mass function). The following code made sense to me but it hung up the compiler requiring a restart:

\begin{tikzpicture}
    \begin{axis}[axis x line =middle, axis y line= left]
        \addplot[domain=1:1] {0.6*x};
        \addplot[mark=*] coordinates {(1,0.6)};
    \end{axis}
\end{tikzpicture}

If I remove the line \addplot[domain=1:1] ... it compiles fine. If I change the line to \addplot[domain=1] ... it compiles but gives me 17 errors. I'm new to the axis environment and addplots. None of the tutorials, or tips and tricks I could find online addressed my question.

To comply with standards for posting, the following is the complete, minimal code:

\documentclass{amsbook} 
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[axis x line =middle, axis y line= left] 
    \addplot[domain=1:1] {0.6*x};
    \addplot[mark=*] coordinates {(1,0.6)};
   \end{axis}
\end{tikzpicture}
\end{document}
6
  • 1
    Why didn't you simply edit the title then? I already gave you an answer. Jul 31 at 22:49
  • 1
    In general, it is recommended on this site to follow the guidelines from tex.meta.stackexchange.com/questions/228 regarding providing a complete yet minimal code example when you ask questions, see Jasper's provided excellent answer. Jul 31 at 22:57
  • @JasperHabicht. I honestly thought that would be more confusing but it seems I was wrong. And yes you did leave a comment and a suggestion but as my response to it indicates, your solution didn't work. All it did was cause the compiler to hang. Nevertheless, I appreciate you taking the time to try to help.
    – TonyK
    Jul 31 at 23:33
  • @Dr.ManuelKuehner: If by a complete yet minimal code example you mean including the preample etc., I am happy to do so. I will edit the current question to add it. On the other issue, if you read the question above you will see that the suggestion from Jasper DID NOT WORK. I've made this observation in three places but it seems not to be getting traction.
    – TonyK
    Jul 31 at 23:38
  • Okay, so it seems that there are two different questions here: 1) how to draw a vertical line in the plot (for this you don't need the domain option at all); 2) why do you get this strange behaviour when using domain=1:1. As for the first question, you should be able to use \addplot[] coordinates {(1,0) (1,0.6)}; (see my answer below). As for the second question, I am unable to reproduce this, so I cannot really help you in this regard. Maybe delete all auxiliary files and recompile from scratch? Jul 31 at 23:43

2 Answers 2

2

pgfplots has a plot type specifically for drawing vertical lines extending out from the x-axis: ycomb.

There is a corresponding xcomb for horizontal lines.

output of code - axis with vertical line

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        axis x line=middle,
        axis y line= middle, 
        xmin=0, xmax=2,
        ymin=0, ymax=1
        ]
        \addplot [ycomb] coordinates {(1,0.6)};
    \end{axis}
\end{tikzpicture}
\end{document}
1
  • Cool, another new trick. Thanks.
    – TonyK
    Aug 1 at 22:46
3

Thanks to the deletion of my previous answer along with your original question, I am now able to digest and rephrase what I originally wrote:

  • Using domain=1 will lead to a syntax error. You need to use something like domain=1:2, i.e. two numbers denoting the start and end of the domain respectively which are separated by a colon.
  • I can reproduce the problem you have when using domain=1:1. But this problem does not come up if you compile using LuaLaTeX. I don't have an answer to this at the moment. (Actually, this should theoretically draw a plot with zero length which would not be visible.)
  • However, for a vertical line, you don't need domain at all, since you can just plot it using a list of coordinates (only two in this case).

Use this code as an example:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[axis x line=middle, axis y line= middle, 
        xmin=0, xmax=2, ymin=0, ymax=1]
        \addplot[domain=0.5:1.5, blue] {0.6*x};         % <-- 0.6*x would give a diagonal line
        \addplot[red] coordinates {(1,0) (1,0.6)};      % <-- this should be what you are looking for
        \addplot[mark=*] coordinates {(1,0.6)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

4
  • Thank you. That answers my central question and is most helpful. As to why the MikTex engine hangs, it would be nice to know but I'm not that much of a purest. As I said, I'm new to addplot - I didn't know that I could simply `draw' a line segment. That is useful to know.
    – TonyK
    Jul 31 at 23:58
  • You can use coordinate lists to plot and you can use \draw to draw. The same line would be drawn using \draw[red] (1,0) -- (1,0.6); inside the axis environment (which is important, because outside the axis environment, the coordinate system changes). Aug 1 at 0:00
  • Got it. Another useful piece of intel. I had assumed that the \draw command would not work correctly inside the axis environment. I guess I should have tried it. It will prove very useful to be able to use draw inside that environment. Shiny, as the saying goes.
    – TonyK
    Aug 1 at 0:06
  • 2
    @TonyK One caveat about \draw that's nice to be aware of: (x,y) coordinates will only be interpreted as axis coordinates if you have compat=1.11 or higher version number. If you always have compat=1.18 or similar as in your example, then you're fine, but if you forget that then \draw (1,0) -- (1,0.6); won't do what you expect. Aug 1 at 8:16

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.