6

The plot is shifted to the right -- it should be centered. Any ideas why? I assume it comes from my coordinate transformation, since I plot nothing, the coordinate system is centered.

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.polar}

\begin{document}

\begin{filecontents*}{test_world.dat}
    13.4050 52.5200
    151.2093 -33.8688
    -74.0060 40.7128
\end{filecontents*}

% Berlin
\def\latO{52.5200}
\def\lonO{13.4050}

\pgfmathdeclarefunction{aeqdr}{4}{%
  \pgfmathsetmacro\dlon{#1-#3}
  \pgfmathsetmacro\argtmp{sin(#4)*sin(#2)+cos(#4)*cos(#2)*cos(\dlon)}
  \pgfmathsetmacro\arg{min(1,max(-1,\argtmp))}
  \pgfmathsetmacro\cdeg{acos(\arg)}
  \pgfmathparse{\cdeg/180}
}

\pgfmathdeclarefunction{aeqdtheta}{4}{%
  \pgfmathsetmacro\dlon{#1-#3}
  \pgfmathparse{mod(atan2(sin(\dlon)*cos(#2),cos(#4)*sin(#2)-sin(#4)*cos(#2)*cos(\dlon)),360)}
}

\centering
\begin{tikzpicture}
    \begin{polaraxis}[
        width=8cm,
        height=8cm,
        y axis line style={draw=none},
        separate axis lines,
        ymin=0,
        ymax=1,
        xtick distance=10,
        xticklabel={%
            \pgfmathparse{%
                int(mod(450-\tick,360))%
            }%
            \pgfmathprintnumber{%
                \pgfmathresult
            }\textdegree%
        },
        yticklabels={},
    ]

    \addplot+[only marks, mark=*, mark size=1.6pt]
    table[
      x expr = {mod(450 - aeqdtheta(\thisrowno{0},\thisrowno{1},\lonO,\latO), 360)},
      y expr = {aeqdr(\thisrowno{0},\thisrowno{1},\lonO,\latO)},
      col sep=space,
    ] {test_world.dat};

  \end{polaraxis}
\end{tikzpicture}

\end{document}

Shows pgfplot which is shifted to the right

0

2 Answers 2

8

The error is subtle, but it is commented here:

piece of manual stating the need of not having side effects

You can't set macros in the body of pgfmathdeclarefunction (by the way, the declare function syntax is handier, in my opinion). In some way, there was a leakage (but see below) in the bounding box computation. So, this works:

\documentclass[margin=10pt, tikz]{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\usepgfplotslibrary{polar}

\begin{filecontents*}{test_world.dat}
    13.4050 52.5200
    151.2093 -33.8688
    -74.0060 40.7128
\end{filecontents*}

% Berlin
\def\latO{52.5200}
\def\lonO{13.4050}

\pgfmathdeclarefunction{aeqdr}{4}{%
  \pgfmathparse{acos(min(1,max(-1,(sin(#4)*sin(#2)+cos(#4)*cos(#2)*cos(#1-#3)))))/180}
}

\pgfmathdeclarefunction{aeqdtheta}{4}{%
  \pgfmathparse{mod(atan2(sin(#1-#3)*cos(#2),cos(#4)*sin(#2)-sin(#4)*cos(#2)*cos(#1-#3)),360)}
}
\begin{document}
\begin{tikzpicture}
    \begin{polaraxis}[
        width=8cm,
        height=8cm,
        scale only axis,
        y axis line style={draw=none},
        separate axis lines,
        ymin=0,
        ymax=1,
        xtick distance=10,
        xticklabel={%
            \pgfmathparse{%
                int(mod(450-\tick,360))%
            }%
            \pgfmathprintnumber{%
                \pgfmathresult
            }\textdegree%
        },
        yticklabels={},
    ]
    \addplot+[only marks, mark=*, mark size=1.6pt]
    table[
      x expr = {mod(450 - aeqdtheta(\thisrowno{0},\thisrowno{1},\lonO,\latO), 360)},
      y expr = {aeqdr(\thisrowno{0},\thisrowno{1},\lonO,\latO)},
      col sep=space,
    ] {test_world.dat};
  \end{polaraxis}
\end{tikzpicture}
\end{document}

Center polar graph

Notice, however, that the problem is not due to the definition of the local variables, but with the spurious spaces added in \pgfmathdeclarefunction, as cunningly found by @kabenyuk.

It is always better in standalone + tikz to keep the body of the document (between \begin{document} and \end{document}) just with one tikzpicture. Still, the leakage of spaces from the declared functions still happens by putting them into the preamble... which could be classified as a bug or, at least, a quite strange feature 😉.

3

Try it like this. It seems to work.
Edit. Actually, I made two key changes that restored the centering: I added preview and removed \centering. Everything else is irrelevant. Apparently, I should have mentioned this right away.

\documentclass[border=24pt, preview]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{pgfplots.polar}
\begin{document}

\begin{filecontents*}{test_world.dat}
    13.4050 52.5200
    151.2093 -33.8688
    -74.0060 40.7128
\end{filecontents*}

% Berlin
\def\latO{52.5200}
\def\lonO{13.4050}

\pgfmathdeclarefunction{aeqdr}{4}{%
  \pgfmathsetmacro\dlon{#1-#3}
  \pgfmathsetmacro\argtmp{sin(#4)*sin(#2)+cos(#4)*cos(#2)*cos(\dlon)}
  \pgfmathsetmacro\arg{min(1,max(-1,\argtmp))}
  \pgfmathsetmacro\cdeg{acos(\arg)}
  \pgfmathparse{\cdeg/180}
}

\pgfmathdeclarefunction{aeqdtheta}{4}{%
  \pgfmathsetmacro\dlon{#1-#3}
  \pgfmathparse{mod(atan2(sin(\dlon)*cos(#2),cos(#4)*sin(#2)-sin(#4)*cos(#2)*cos(\dlon)),360)}
}

%\centering 

\begin{tikzpicture}
    \begin{polaraxis}[
        width=8cm,
        height=8cm,
        y axis line style={draw=none},
        separate axis lines,
        ymin=0,
        ymax=1,
        xtick distance=10,
        xticklabel={%
            \pgfmathparse{%
                int(mod(450-\tick,360))%
            }%
            \pgfmathprintnumber{%
                \pgfmathresult
            }\textdegree%
        },
        yticklabels={},        
    ]

    \addplot+[only marks, mark=*, mark size=1.6pt]
    table[
      x expr = {mod(450 - aeqdtheta(\thisrowno{0},\thisrowno{1},\lonO,\latO), 360)},
      y expr = {aeqdr(\thisrowno{0},\thisrowno{1},\lonO,\latO)},
      col sep=space,
    ] {test_world.dat};
  \end{polaraxis}
\end{tikzpicture}

\end{document} 

enter image description here

Edit 2. The root of the problem lies in whitespace within pgfmath macros. In pgfmath expressions, every space or line break can affect calculations and positioning. Eliminating these spaces resolves all issues, and the code then behaves correctly.

Edit 3.
Moreover, all spaces between the definitions \def\latO, \def\lonO, both \pgfmathdeclarefunction calls, and before \begin{tikzpicture} should be removed (I provide the code without these spaces below) — this ensures full symmetry. Even better, move them all to the preamble. And of course, \centering is unnecessary.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{pgfplots.polar}

\begin{document}

\begin{filecontents*}{test_world.dat}
    13.4050 52.5200
    151.2093 -33.8688
    -74.0060 40.7128
\end{filecontents*}

% Berlin
\def\latO{52.5200}%
\def\lonO{13.4050}%    
\pgfmathdeclarefunction{aeqdr}{4}{%
  \pgfmathsetmacro\dlon{#1-#3}%
  \pgfmathsetmacro\argtmp{sin(#4)*sin(#2)+cos(#4)*cos(#2)*cos(\dlon)}%
  \pgfmathsetmacro\arg{min(1,max(-1,\argtmp))}%
  \pgfmathsetmacro\cdeg{acos(\arg)}%
  \pgfmathparse{\cdeg/180}
}%    
\pgfmathdeclarefunction{aeqdtheta}{4}{%
  \pgfmathsetmacro\dlon{#1-#3}%
  \pgfmathparse{mod(atan2(sin(\dlon)*cos(#2),cos(#4)*sin(#2)-sin(#4)*cos(#2)*cos(\dlon)),360)}
}%    
\begin{tikzpicture}
    \begin{polaraxis}[
        width=8cm,
        height=8cm,
        y axis line style={draw=none},
        separate axis lines,
        ymin=0,
        ymax=1,
        xtick distance=10,
        xticklabel={%
            \pgfmathparse{%
                int(mod(450-\tick,360))%
            }%
            \pgfmathprintnumber{%
                \pgfmathresult
            }\textdegree%
        },
        yticklabels={},
        scale only axis, %%%%%%%%%%%%%%%%%%% 
    ]

    \addplot+[only marks, mark=*, mark size=1.6pt]
    table[
      x expr = {mod(450 - aeqdtheta(\thisrowno{0},\thisrowno{1},\lonO,\latO), 360)},
      y expr = {aeqdr(\thisrowno{0},\thisrowno{1},\lonO,\latO)},
      col sep=space,
    ] {test_world.dat};    
  \end{polaraxis}
\end{tikzpicture}

\end{document} 

enter image description here

12
  • Can you please add to your answer, what changed? Thank you Commented 2 days ago
  • 1
    @MS-SPO I added a bit of text to my answer. To repeat here: I added the preview option and removed \centering. The reason, apparently, is that \centering is not a “center this box” command, but a paragraph-level declaration with all its side effects. Also, I genuinely don’t understand why the question author included this command here at all. Thank you for your comment — it drew my attention to the fact that three leftover lines from my earlier experiments remained in my code; of course, they were completely irrelevant. Commented yesterday
  • 1
    Nice --- but running your code, I have this i.sstatic.net/8MwgVl5T.png , that is, the diagram is fully visible, but the bounding box is still wrong: there is definitely something pesky happening with the side effects... Commented yesterday
  • 2
    @Rmano The root of the problem was whitespace in the pgfmath macros. I have expanded my answer — perhaps this will be helpful for future readers. Commented yesterday
  • 1
    @Rmano it turns out not all whitespace affecting the layout was removed. I’ve added the relevant explanations in my answer. Commented yesterday

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.