11

Judging from the pgfplots manual I expected the following code to work:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[
      xmin=0,
      xmax=4,
      ymin=0,
      ymax=4,
      ]
      \draw (1,1) -- (3,2);
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

However, it doesn't - meaning that it seems the \draw command does nothing at all. I just get an empty axes with no line.

enter image description here

Can anyone shed some light on this? I guess the solution will be really easy...

Eike P.
  • 699
  • 5
  • 14

1 Answers1

16

You need to add the compat option to tell pgfplots which version you want to use. The most recent version is 1.11, or you can use the keyword newest to always use the newest version. Note that the default behavior of assigning points was changed in v1.11, so you need to set the compatibility equal to or newer than that. Note also that the newest syntax is not recommended according to the package author (or at least, you should know you have a reason to use newest instead of an explicit version).

\documentclass{article}
\usepackage{pgfplots}
%\pgfplotsset{compat=newest} %<------ Here
\pgfplotsset{compat=1.11} %<------ Or use this one
\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[
      xmin=0,
      xmax=4,
      ymin=0,
      ymax=4,
      ]
      \draw (1,1) -- (3,2);
    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

In fact, if you look at the log for the OP MWE (without the compat being set), you find the following message:

Package pgfplots Warning: running in backwards compatibility mode (unsuitable t
ick labels; missing features). Consider writing \pgfplotsset{compat=1.11} into 
your preamble.
 on input line 4.

which tells you most of what you need to know!

darthbith
  • 7,384
  • D'oh... I just removed the line after updating from 1.6 to 1.11 while trying to get it to work. The older version produced an error without the compat line, the newer didn't, so I removed it. So to conclude: The real problem here was with version 1.6 that for some reason did not plot the line. Updating fixed the issue. – Eike P. Nov 06 '14 at 14:28
  • 2
    Version 1.6 probably did plot the line, just outside the bounds of your axes. If you add the explicit (axis cs: 1,1) -- (axis cs: 3,2);, you should see the line in all versions (you can verify by changing the version in the compat setting) – darthbith Nov 06 '14 at 14:30
  • 1
    Ahhh you're right! Good to know and thanks a lot for the help! :) – Eike P. Nov 06 '14 at 14:32
  • @jhin Happy to help! – darthbith Nov 06 '14 at 14:32
  • Good answer. Related question : http://tex.stackexchange.com/questions/139690/dos-and-donts-of-pgfplotssetcompat-newest/139695#139695 – Christian Feuersänger Nov 06 '14 at 22:17
  • @ChristianFeuersänger Thanks for the link. I've added a note to the answer. Feel free to edit if you have any further recommendations! Thanks for an awesome package! – darthbith Nov 06 '14 at 23:08
  • What exactly are we talking about here -- what does versions mean in this context of backward compatibility? Whose versions I mean... The \draw instruction does not seem to go well with \begin{axis} -- what is better then to use inside the axis environment? I cannot understand the differences, it seems like \draw is for "pure" tikz, and axis and other drawing instructions are "native" of pgfplots... So what is the difference between tikz and pgfplots, they seem to be two distinct paradigms, with enough overlapping to confuse a newbie like me... – Diegis Feb 23 '15 at 00:21
  • @Diegis pgfplots is a superset of the tikz, and is built with tikz as the "base" drawing commands. That is to say, pgfplots provides macros like addplot that use draw etc. underneath to actually draw the lines. If you have further questions, you should ask a new question. – darthbith Feb 23 '15 at 02:40