I am having trouble controlling the axes in pgfplots. I have defined an evironment for graphs

\def\tempa{#1} %Saves caption since \end cannot take arguments
\draw node at (7,0) { \Large     $ #2 $ }; %variable on x-axis
\draw node at (0,6.1) { \Large $ #3 $}; %variable on y-axis
      axis lines=left,
      axis equal,
      restrict y to domain=0:10.5

\ifdefempty{\tempa}{}{\caption{\tempa}} %Creates caption if argument is present.


As you can see, I have been experimenting a bit with the axes. My problem is, that they are not of equal length. I would like them to have the same fixed domain 0:10.5, and be of the same exact length (cm). Usually the y-axis is shorter than the x-axis. In line 5 and 6 I define the names of the axes. As you can see the variables are placed at 6.1 instead of something like 10.5 which I defined the dimension of the axis to be. I would like be able to use the the coordinates of the axis when i use the draw command.


If I set the position to 10, the nodes are totally misplaced.

\draw node at (10,0) { \Large    $ #2 $ }; %variable on x-axis


The last problem is that the draw function does not work within the environment

\draw[dotted] (0,0) -- (2,2);

This gives me nothing:


Thanks in advance.

  • 1
    The tikzpicture coordinate system is not the same as the axis coordinate system. You can place nodes inside the axis, read in the manual about the axis cs coordinate system. Why not use xlabel/ylabel though? The axis equal option may also be of interest. – Torbjørn T. Mar 14 '14 at 13:34
  • Is it possible to force the tikz and axis coordinate systems to use the same coordinates? I dont like how xlabel and ylabel places the variables. – user1067911 Mar 14 '14 at 14:07
  • As I said, if you place your nodes inside the axis you can use the coordinate system of the axis, e.g. \node at (axis cs:0,10.5) {$y$};. But you can of course modify the location of xlabel/ylabel, see chapter 4.9.3 Labels in the manual. (I may write an answer later, but if anyone else wants to, please don't hesitate.) – Torbjørn T. Mar 14 '14 at 18:49

Here is a suggestion. I used unit vector ratio=1 1 1 instead of axis equal because axis equal seems to extend the x-axis a bit, no idea why. The axis labels are placed using xlabel/ylabel, and the position modified by changing the every axis x label style, similar for y. Note that the coordinates are relative to the axis, so for example (0,1) is at the top of the y-axis.

(I removed the \caption stuff because I don't know where \ifdefempty comes from, so that part wouldn't work.)

If you want to use \draw and similar in the graph environment, use the axis cs coordinate system, as you can see in the examples below.

      axis lines=middle,
      unit vector ratio=1 1 1,
      every axis x label/.style={at={(1,0)},font=\Large,right},
      every axis y label/.style={at={(0,1)},font=\Large,above},
      no marks
\addplot {x};
\addplot {x+1};
\addplot {x-1};
\draw [dotted] (axis cs:0,10.5) -- (axis cs:10.5,10.5) -- (axis cs:10.5,0);

\addplot {8*sin(deg(x))*sin(deg(x))};
\draw [thick,latex-latex] (axis cs:1.57,8) to[out=40,in=140] node[above]{random} (axis cs:3.14+1.57,8);

enter image description here

  • Thanks a lot. That completely answered my question. Unfortunately I have stumbled upon a new problem: How can I write something underneath the x-axis? – user1067911 Mar 27 '14 at 15:38
  • @user1067911 \node [below=1cm] at (rel axis cs:0.5,0) {something}; should work I suppose. – Torbjørn T. Mar 27 '14 at 22:58
  • It doesnt seem to work. No text is created. – user1067911 Mar 29 '14 at 14:48
  • 1
    @user1067911 Ah, sorry, my mistake. By default everything outside axis boundaries is clipped, add clip mode=individual to the axis options. – Torbjørn T. Mar 29 '14 at 15:51

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.