2

I'm trying to create vertical lines in a tikzpicture, but they're being drawn at very small scales.

My code:

\documentclass [10pt] {article}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
    \begin{center}
        \begin{tikzpicture}
        \begin{axis}[
        axis lines=center,
        xmin=-0.25,
        xmax=3.5,
        ymin=-0.25,
        ymax=1.75,
        axis line style={<->},
        ]
        \addplot [<->,thick,domain=0.88:3]{x^(-3)};
        \draw (1,-0.25) -- (1,1.75)[dashed,gray];
        \draw (2,-0.25) -- (2,1.75)[dashed,gray];
        \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}

And this is the problem:

enter image description here

If you can see it, all the way in the bottom left corner, there are the two tiny vertical lines that I wanted to span from (1,-0.25) to (1,1.75) and from (2,-0.25) to (2,175). If I set the ranges to something massive like 500, I can see the lines, but I don't understand why they're being drawn this way.

2

You must specify that you want to use the axis coordinate system (axis cs):

\documentclass [10pt] {article}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
    \begin{center}
        \begin{tikzpicture}
        \begin{axis}[
        axis lines=center,
        xmin=-0.25,
        xmax=3.5,
        ymin=-0.25,
        ymax=1.75,
        axis line style={<->},
        ]
        \addplot [<->,thick,domain=0.88:3]{x^(-3)};
        \draw (axis cs:1,-0.25) -- (axis cs:1,1.75)[dashed,gray];
        \draw (axis cs:2,-0.25) -- (axis cs:2,1.75)[dashed,gray];
        \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}
  • 1
    From version 1.11 it is done automatically. You don't need to add. – percusse Jan 28 '16 at 1:43
  • This fixes it, thanks. I've never had to do this before though, I can't understand why I suddenly need to add it. – Emilio Garcia Jan 28 '16 at 1:46
  • @EmilioGarcia you don't need to. See my comment in the other answer. – percusse Jan 28 '16 at 1:54
  • @percusse I see, the version declaration also fixes the problem. I've never had to do either though. I don't understand what about this graph requires compat. – Emilio Garcia Jan 28 '16 at 1:59
  • The default axis coordinates for pgfplots before 1.11 was the normalized one (from 0,0 to 1,1). It has been changed to the coordinates in the plot, but for compatibility with older sources you need to ask specifically for it. (Thanks ${DEITY})You should have a compat key anyway, because without it there are very old and buggy behaviour s active. – Rmano Jan 28 '16 at 6:51
1

You should specify the coordinates relative to your axes using (axis cs:x,y), or just use @percusse's advice and put \pgfplotsset{compat=1.12} in the preamble without changing your original code.

\documentclass [10pt] {article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{tikz}
\begin{document}
    \begin{center}
        \begin{tikzpicture}
        \begin{axis}[
        axis lines=center,
        xmin=-0.25,
        xmax=3.5,
        ymin=-0.25,
        ymax=1.75,
        axis line style={<->},
        ]
        \addplot [<->,thick,domain=0.88:3]{x^(-3)};
        \draw (axis cs:1,-0.25) -- (axis cs:1,1.75)[dashed,gray];
        \draw (axis cs:2,-0.25) -- (axis cs:2,1.75)[dashed,gray];
        \end{axis}
        \end{tikzpicture}
    \end{center}
\end{document}

enter image description here

  • I believe that shouldn't be the problem as you can see the lines are now off – percusse Jan 28 '16 at 1:38
  • I think there's something else wrong, since I've never had to specify a unit of length before, and this doesn't really fix the problem. – Emilio Garcia Jan 28 '16 at 1:41
  • 3
    @EmilioGarcia The problem should be about adding \pgfplotsset{compat=1.12} – percusse Jan 28 '16 at 1:43
  • @percusse (+1) You're right, I added your suggestion to the answer. – AboAmmar Jan 28 '16 at 2:11

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.