5

From the pgfplots manual (sec. 8.4):

\pgfplotsunitxlength,\pgfplotsunitylength,\pgfplotsunitzlength,...
Macros which expand to the vector length ||xi|| of the respective unit vector xi...

There is no information about the unit of measurement, but I suppose it's the point (pt) from elsewhere.
What I understand is that, if I draw a segment from the point (0,0) to the point (1,0), its length is \pgfplotsunitxlength and, if I draw a segment from the point (1,0) to the point (1,1), its length is \pgfplotsunitylength. But this is not true. See the following example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot+ [no marks] coordinates {(0,0) (1,0) (1,1)};
\draw [<->, dashed] (axis cs:0,0.5) -- +(0:\pgfplotsunitxlength pt);
\draw [<->, dashed] (axis cs:0.8,0) -- +(90:\pgfplotsunitylength pt);
\end{axis}
\end{tikzpicture}
\end{document}

It's quite odd that, if I multiply both the two quantities by 100, I get the actual unit vectors length, as in the following example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot+ [no marks] coordinates {(0,0) (1,0) (1,1)};
\draw [<->, dashed] (axis cs:0,0.5) -- +(0:100*\pgfplotsunitxlength pt); %<--
\draw [<->, dashed] (axis cs:0.8,0) -- +(90:100*\pgfplotsunitylength pt); %<--
\end{axis}
\end{tikzpicture}
\end{document}

Moreover, if the axis length is greater than 10 units and less than 100, I need to multiply by 10 only to get the actual unit vector length, as in the following example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis} [ymax=11] %<--
\addplot+ [no marks] coordinates {(0,0) (1,0) (1,1)};
\draw [<->, dashed] (axis cs:0,0.5) -- +(0:100*\pgfplotsunitxlength pt);
\draw [<->, dashed] (axis cs:0.8,0) -- +(90:10*\pgfplotsunitylength pt); %<--
\end{axis}
\end{tikzpicture}
\end{document}

So the questions are:

  1. what is the actual meaning of the \pgfplotsunit[...]length macro?
  2. how can I get the actual unit vectors length, meaning the length of a unitary segment?

Edit

Suggested answer for question 2

I see that one can get the correct unit vector length by multiplying the \pgfmathresult from \pgfplotstransformcoordinatex{1} or \pgfplotstransformdirectionx{1} by \pgfplotsunitxlength. See the following example:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis}
\addplot+ [no marks] coordinates {(0,0) (1,0) (1,1)};
\pgfplotsextra{
  \pgfplotstransformdirectionx{1}
  \pgfmathsetlengthmacro{\ex}{\pgfmathresult*\pgfplotsunitxlength}
  \pgfplotstransformdirectiony{1}
  \pgfmathsetlengthmacro{\ey}{\pgfmathresult*\pgfplotsunitylength}
}
\draw [<->, dashed] (axis cs:0,0.5) --  +(0:\ex);
\draw [<->, dashed] (axis cs:0.8,0) --  +(90:\ey);
\end{axis}
\end{tikzpicture}
\end{document}

Update 2015-01-22 from the bug section of the development page

Thank you for your feedback. It's true that those macros seem to be unuseful, but I'm telling you what I used them for.

Suppose I have a coordinate system with two different unit lengths for the axes. I want to draw an arc between two lines by means of the arc instruction provided by TikZ. So I need to compute the angle between the two lines. I cannot use the two slopes directly, because the two units of measurement are different, so I have to scale them using the two unit vector lenghts. If I use the \pgfplotsunit[xy]length macro as it is now, the result is incorrect because of the behaviour I told you in the first post.

Finally, I think that in some rare cases those macro are useful and should be improved. For instance, I'd convert them into lenght macros.

5
  • It's the unit in terms of the current axis. So if xmin=-5, xmax=5 it should be different what the distance from [0-1] then what it would be in the case xmin=-20 and xmax=20 – percusse Nov 25 '14 at 10:54
  • @percusse I understand what you are saying but I think it should be obvious because, as the size of the plot is the same and the axis length changes, the unit vector length will change accordingly. So why relate this macro to the axis length? – Luigi Nov 25 '14 at 11:07
  • @percusse you can see that the multiplicative factor is not proportional to the axis length by setting ymax=20. Than you'll see that, until ymax is less than 100, the factor is still 10. – Luigi Nov 25 '14 at 11:24
  • I think we are having some differences due to v1.11. I'll check this properly in the evening. In case usual suspects handle this ;) – percusse Nov 25 '14 at 11:28
  • I filed a bug report in the support section. – Luigi Dec 4 '14 at 9:17
3

This is a weakness of pgfplots up to and including version 1.11 .

The version 1.12 (which will be released within the next weeks at the time of this writing) will result in

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}
\begin{tikzpicture}
\begin{axis} [ymax=11]
\addplot+ [no marks] coordinates {(0,0) (1,0) (1,1)};
\draw [<->, dashed] (0,0.5) --  +(0:1);
\draw [<->, dashed] (0.8,0) --  +(90:1);
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1
  • I've added a new post in the bug section. I'm copying its content at the bottom of the question to let the other users to contribute the discussion. – Luigi Jan 22 '15 at 9:24

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.