# Polar plot with cartesian values, lack of angular precision in the integrated arctan2 calculation?

I noticed that for values close to 0° and 180° the angular values dramatically changes if the values are imported from cartesian data. Here the code (with cartesian coordinates plotted in polar with data cs=cart and the same coordinates in polar coordinates obtained with arctan2 Matlab function) and illustration:

\documentclass{minimal}
\usepackage{eurosym}
\usepackage{pgfplots}
\usepackage{fp}

\begin{document}

\usepgfplotslibrary{polar}
\centering
\begin{tikzpicture}
\begin{polaraxis}[
visualization depends on=x \as \pgfplotspointx,
nodes near coords,
every node near coord/.style={
%font=\small,
rotate=\pgfplotspointx,
append after command={
node [
anchor=south,
%font=\small,
rotate=\pgfplotspointx,
shift={(axis direction cs:0,(12.75-\pgfplotspointmeta))}
] {$\pgfmathprintnumber{\pgfplotspointx}^\circ$}
}
},
width=7\textwidth,
xmin=-2,xmax=1, ymin=12, ymax=16,
title=artctan2 precision problem,
grid=both,
minor x tick num={4},
minor y tick num={1},
]
\addplot+[polar comb, data cs=cart, mark size=1, mark=asterisk, color=blue, dashed] table {
14.370195   -0.304948
14.370195   -0.304948

};
\addplot+[polar comb, mark size=1, mark=asterisk, color=green, solid] table {-1.215683667   14.37343027
-1.215683667    14.37343027
};
\end{polaraxis}
\end{tikzpicture}

\end{document}


Has anyone meet this issue?

-
Yes this is known and there is not much you can do about it. – percusse Aug 6 '14 at 13:49
@percusse: Hm, but \pgfmathparse{atan2(14.37,-0.304948)} \pgfmathresult gives the (almost) correct result of -1.20. Maybe there's something else at play in the conversion from cartesian to polar coordinates in PGFPlots? – Jake Aug 6 '14 at 13:52
@Jake The conversion doesn't use it I think. I had a similar problem before and I guess we need to dive into polar code. Maybe I should say not much without hacking into the code. I think somewhere in the drawing fpu is turned off. – percusse Aug 6 '14 at 13:57
Thank you for your fast comments. Thus, I will directly manipulate polar coordinates instead of using data cs =cart. – Willi Aug 6 '14 at 14:30

This needs to be repaired in pgfplots, I accept this question as bug report.

Details:

The routine pgfplotsmathcarttopol does not make use of atan2 - instead, it computes the angle "manually", apparently with less precision.

\pgfplotsmathcarttopol{14.370195}{-0.304948}\angle\radius



to see that this is, indeed, the root cause.

Steps to be done would be to write an implementation (or adapter) for atan2 for the floating point unit of PGF and to use that one in pgfplotsmathcarttopol.

I will take a note for the todo list of pgfplots (no need to file a separate bug report).

-
OK. Thank you for your fast answer. I leave it like this (no bug report). – Willi Aug 6 '14 at 14:33
Solution has been integrated and will become part of pgfplots 1.12 . – Christian Feuersänger Dec 30 '14 at 15:53
Hi, great! Thank you! – Willi Dec 31 '14 at 17:16

A solution with PSTricks. Run it with xelatex:

\documentclass{minimal}
\usepackage{pst-plot}
\begin{document}

\begin{pspicture}(33,-2)(48,1.1)
\psset{runit=3}\uput[0](16;0){0}
\psarcn(0,0){12}{1}{-2}\psarcn(0,0){16}{1}{-2}
\psline(12;1)(16;1)\psline(12;-2)(16;-2)
\multido{\rA=12.25+0.25}{15}{\psarcn[linecolor=black!15](0,0){\rA}{1}{-2}}
\psline[linecolor=black!15](12;0)(16;0)\psline[linecolor=black!15](12;-1)(16;-1)
\multido{\rA=12.0+0.5}{9}{\uput[-90](\rA;-2.5){\rA}}
\psline[linecolor=blue,linestyle=dashed]{-*}(12;-0.54)(14.37;-0.54)
\psline[linecolor=green]{-*}(12;-1.22)(14.37;-1.22)
\uput[90](12.75;-0.54){\blue$-0.54^\circ$}\uput[90](12.75;-1.22){\green$-1.22^\circ$}
\uput[90](14.37;-0.54){\blue$14.37$}\uput[90](14.37;-1.22){\green$14.37$}
\end{pspicture}

\end{document}


-
Thank you for your answer! It seems to be a complete different package no? I am actually a new user of pgfplots so maybe I don't got all the tricks now! I will study your approach ;-) – Willi Aug 6 '14 at 19:49
Yes, it does all calculations with PostScript, a complete programming language. – Herbert Aug 6 '14 at 19:51