1

EDIT : As it seems to be a bug in pgfplots an issue has been opened: https://github.com/pgf-tikz/pgfplots/issues/360

While plotting polar axes, I faced an issue when transforming small quandtities with the y coord trafo/y coord inv trafo options.

My plots are basically sine functions show in the polar domain. As an example take the following cosine cos(3\x). This function has positive and negative values and is plotted as expected in an axis environment.

enter image description here

Now if one wants to plot this very same function inside a polaraxis environment, here is what comes out :

enter image description here

Basically, negative values are handled as a 180° phase shift, instead of being simply shown as a negative value at the defined angular coordinate.

This behavior can be circumvented with some well-chosen y coord trafo/y coord inv trafo. In this very case,

y coord trafo/.code=\pgfmathparse{#1+2},
y coord inv trafo/.code=\pgfmathparse{#1-2},

works just fine.

enter image description here

However, if some small values have to be handled, say around 1e-6 amplitude instead of 1 as in the above examples, the computation fails due to precision errors. These problems should be solvable through the fpu unit, but I can't figure the correct sequence. I tried e.g

y coord trafo/.code={\pgfkeys{/pgf/fpu=true}\pgfmathparse{#1+2e-6}\pgfkeys{/pgf/fpu=false}},
y coord inv trafo/.code={\pgfkeys{/pgf/fpu=true}\pgfmathparse{#1-2e-6}\pgfkeys{/pgf/fpu=false}},

but this leads to illegal unit of measure errors.

Here is the MWE to reproduce the figures and the problem once the commented part is reactivated.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{polar}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
            xmin=0,
            xmax=360,
            ymin=-2,
            ymax=2,
            domain = 0:360,
        ]
        \addplot+ ({\x},{cos(3*\x)});
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{polaraxis}[%
            xmin=0,
            xmax=360,
            ymin=-2,
            ymax=2,
            domain = 0:360,
        ]
        \addplot+ ({\x},{cos(3*\x)});
    \end{polaraxis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{polaraxis}[%
            xmin=0,
            xmax=360,
            ymin=-2,
            ymax=2,
            y coord trafo/.code=\pgfmathparse{#1+2},
            y coord inv trafo/.code=\pgfmathparse{#1-2},
            domain = 0:360,
        ]
        \addplot+ ({\x},{cos(3*\x)});
    \end{polaraxis}
\end{tikzpicture}

% Not working
% \begin{tikzpicture}
%     \begin{polaraxis}[%
%             xmin=0,
%             xmax=360,
%             ymin=-2e-6,
%             ymax=2e-6,
%             y coord trafo/.code={\pgfmathparse{#1+2e-6}},
%             y coord inv trafo/.code={\pgfmathparse{#1-2e-6}},
%             domain = 0:360,
%         ]
%         \addplot+ ({\x},{1e-6*cos(3*\x)});
%     \end{polaraxis}
% \end{tikzpicture}

\end{document}

1 Answer 1

1

Your idea of using fpu for the coordinate transformations is good. You just need to add /pgf/fpu/output format=fixed.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{polar}
\newcommand{\pgfmathparseFPU}[1]{\begingroup%
  \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
  \pgfmathparse{#1}%
  \pgfmathsmuggle\pgfmathresult\endgroup}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
            xmin=0,
            xmax=360,
            ymin=-2,
            ymax=2,
            domain = 0:360,
        ]
        \addplot+ ({\x},{cos(3*\x)});
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{polaraxis}[%
            xmin=0,
            xmax=360,
            ymin=-2,
            ymax=2,
            domain = 0:360,
        ]
        \addplot+ ({\x},{cos(3*\x)});
    \end{polaraxis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{polaraxis}[%
            xmin=0,
            xmax=360,
            ymin=-2,
            ymax=2,
            y coord trafo/.code=\pgfmathparse{#1+2},
            y coord inv trafo/.code=\pgfmathparse{#1-2},
            domain = 0:360,
        ]
        \addplot+ ({\x},{cos(3*\x)});
    \end{polaraxis}
\end{tikzpicture}

% Now working
\begin{tikzpicture}
    \begin{polaraxis}[%
            xmin=0,
            xmax=360,
            ymin=-2e-6,
            ymax=2e-6,
            y coord trafo/.code={\pgfmathparseFPU{#1+2e-6}},
            y coord inv trafo/.code={\pgfmathparseFPU{#1-2e-6}},
            domain = 0:360,
        ]
        \addplot+ ({\x},{1e-6*cos(3*\x)});
    \end{polaraxis}
\end{tikzpicture}

\end{document}

enter image description here

7
  • Thanks for the proposal ! I was totally missing the \pgfmathsmuggle and others. However, The ticklabels should read as -2,-1,0,1,2 which is not the case with your proposal unfortunately
    – BambOo
    Commented Jun 12, 2020 at 17:14
  • 1
    @BambOo Maybe I am saying something stupid, but aren't you shifting the ticks? That is, you have ymin=-2e-6,ymax=2e-6, and then with y coord trafo/.code={\pgfmathparseFPU{#1+2e-6}}, you add 2e-6, so y should vary between 0 and 4e-6. What am I missing?
    – user194703
    Commented Jun 12, 2020 at 17:26
  • No, it's not stupid, but this is why there is the y coord inv trafo. Basically I shift the plot to avoid actual negative amplitude-related problems, but I correct the ticks using the inv trafo
    – BambOo
    Commented Jun 12, 2020 at 18:01
  • 1
    @BambOo You are right. There seems to be some problem with the scaled y ticks. If you add scaled y ticks=false, then the ticks will be correct (but look ugly).
    – user194703
    Commented Jun 12, 2020 at 18:13
  • 3
    @BambOo, since you and the cat agree that this is very likely a bug, would you file an issue in the tracker again!? Thanks in advance again. Commented Jun 14, 2020 at 16:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .