8

I would like to annotate a patch plot with lines whose start and end points are given in absolute polar and relative polar coordinates, respectively.

The patch plot, based on cartesian data, comes out as a circular shape centred around the origin (0,0) and shall be simulated here by a plain circle.

Lines should start at the circumference of the shape and point away from the shape under a given angle and have a well defined length. Therefore, the use of relative polar coordinates for the line's end point.

What seems to be straightforward with TikZ (left) fails inside the axis environment of pgfplots (right): neither the inclination angle nor the length of the line are correct if relative polar coordinates are used.

enter image description here

What is wrong with the rel. polar coordinates here?

The code:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}

\begin{tikzpicture}
  \draw(0,0) circle[radius=0.01];
  \draw[blue] (0,0) circle[radius=1];
  \draw[thick] (45:1) -- ++(45:0.5);
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[tiny,enlargelimits,
  axis equal,
  xmin=-2,xmax=2,
  ymin=-1,ymax=1,
  xtick={-2,-1,0,1,2},
  ytick={-2,-1,0,1,2},
  grid=major,
  ]
  \draw[blue] (0,0) circle[radius=1];
  \draw[blue, dashdotted] (0,0) -- (45:2);
  \draw[thick] (45:1) -- ++(45:0.5);
%  \draw[thick] (45:1) -- (45:1.5); %absolute coordinates do work
  \end{axis}
\end{tikzpicture}

\end{document}
11

The pgfplots documentation states

As noted in the documentation for axis cs, adding two coordinates by means of the TikZ ++ operator may have unexpected effects. The correct way for ++ operations is axis direction cs:.

Unfortunately, there are not implicit polar coordinates (angle:radius) for the axis direction cs:, which is why you have to compute these yourself (but it's easy). So we end up with

%\draw[thick] (45:1) -- ++(45:0.5);
\draw[thick] (45:1) -- ++(axis direction cs:{0.5*cos(45)},{0.5*sin(45)});

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.