# issue plotting polar data using tikz

I m having a trouble to plot properly a polar data file into a polar plot using TikZ, below the code. Thanks in advance for the help (I would rather avoid to use \begin{axis} in order to be consistent with all document plot).

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{figure}
\begin{tikzpicture}[x=.11cm,y=0.11cm]
% %Circles
\foreach \r in {0, 3,...,42}
\draw[black!20, thick] (0,0) circle (\r);

%Circles
\foreach \r in {0, 6,...,42}
\draw[black!40, thick] (0,0) circle (\r);

%5° Rays
\foreach \a in {0, 5,...,359}
\draw[black!20] (\a:41.5) -- (\a:42);

\foreach \a in {0, 15,...,359}
\draw[black!20] (\a:0) -- (\a:42);

%Main rays
\foreach \a in {0, 90,...,359}
\draw[black!50, thick] (0, 0) -- (\a:42);

%Angle labels
\foreach \a in {0, 15,...,359}
\draw (\a: 45) node {$\a^\circ$};

\draw[color=blue, domain=0:360, line width=1.5pt] plot[smooth] file {data.dat};

\end{tikzpicture}
%\caption
\end{figure}

\end{document}


The data file consists of:

%data.dat
0   7.820634
1   8.381467
2   9.090974
3   9.877479
4   10.68895
5   11.49293
6   12.27133
7   13.0153
8   13.72148
9   14.38954
10  15.0208
11  15.61735
12  16.18157
13  16.71593
14  17.22276
15  17.70429
16  18.16255
17  18.59938
18  19.01647
19  19.4153
20  19.79726
21  20.16356
22  20.5153
23  20.85347
24  21.17899
25  21.49264
26  21.79518
27  22.08727
28  22.36951
29  22.64245
30  22.9066
31  23.16241
32  23.4103
33  23.65067
34  23.88385
35  24.11017
36  24.32993
37  24.5434
38  24.75084
39  24.95247
40  25.14851
41  25.33916
42  25.5246
43  25.70499
44  25.8805
45  26.05127
46  26.21743
47  26.37911
48  26.53643
49  26.68948
50  26.83834

• Do you consider to use pgfplots? – Zarko Jun 22 '15 at 11:06
• @Zarko, with pgfplots i should use begin{axis} .. if there are no other ways to do it yes, i would consider it – mark Jun 22 '15 at 11:25
• with pgfplots is simpler task. Of corse, you can all this do with "pure" TikZ. By the way, with your MWE I can't reproduce your image. – Zarko Jun 22 '15 at 11:34
• yes, that is my option do it with tikz. about reproducing, perhaps it s due data are truncated, so you should be able to visualize a aprt of that branch. BTW the issue is that my code tells to latex to plot data in Cartesian instead of polar.. – mark Jun 22 '15 at 11:38
• @mark your data doesn't look like polar coordinates. – Kpym Jun 22 '15 at 12:17

You can use a nonlinear transform to produce the polar plot from file. Here is the code :

\documentclass[tikz]{standalone}
\usepgfmodule{nonlineartransformations}
\usepackage{datatool}

\begin{filecontents}{data.dat}
0   7.820634
1   8.381467
2   9.090974
3   9.877479
4   10.68895
5   11.49293
6   12.27133
7   13.0153
8   13.72148
9   14.38954
10  15.0208
11  15.61735
12  16.18157
13  16.71593
14  17.22276
15  17.70429
16  18.16255
17  18.59938
18  19.01647
19  19.4153
20  19.79726
21  20.16356
22  20.5153
23  20.85347
24  21.17899
25  21.49264
26  21.79518
27  22.08727
28  22.36951
29  22.64245
30  22.9066
31  23.16241
32  23.4103
33  23.65067
34  23.88385
35  24.11017
36  24.32993
37  24.5434
38  24.75084
39  24.95247
40  25.14851
41  25.33916
42  25.5246
43  25.70499
44  25.8805
45  26.05127
46  26.21743
47  26.37911
48  26.53643
49  26.68948
50  26.83834
\end{filecontents}

\makeatletter
\def\polartransformation{% from the manual 103.4.2 Installing Nonlinear Transformation
\pgfmathsincos@{\pgf@x}
\pgf@x=\pgfmathresultx\pgf@y%
\pgf@y=\pgfmathresulty\pgf@y%
}
\makeatother

\begin{document}

\begin{tikzpicture}[x=.11cm,y=0.11cm, main1/.style = {black}]
% Circles
\foreach[count=\i, evaluate={\j=20*(1+mod(\i,2));}, ] \r in {0, 3,...,42}
\draw[black!\j, thick] (0,0) circle (\r);

% Rays and labels
\foreach[evaluate={\i=mod(\a,15)>0;\j=\i*41.5;\k=mod(\a,90)==0;\l=\i?"":"$\a^\circ$";}] \a in {0, 5,...,359}
\draw[black!20,main\k/.try] (\a:\j) -- (\a:42) node[black,label={[black]\a:\l}]{};

\pgftransformnonlinear{\polartransformation}
\draw[x=1pt, color=blue, line width=1.5pt] plot[smooth] file {data.dat};

\end{tikzpicture}

\end{document}


Note: I have reduced the number of foreach to 2 in the axis drawing.

EDIT: Following the comments of @mark I added x=1pt to avoid polartransformation convert error. The \polartransformation is sensitive to all prior transformations. If for example you add xscale=.5 all angles will be divided by 2. Here x=.11 cm that is used, should be reset.

• Hi Kpym, thank you for the answer, but I cannot understand the maths behind on that. Could you please clarify it? – mark Jun 22 '15 at 13:43
• @mark the \polartransformation transform (x,y) to (y*cos(x), y*sin(x)) so it is the standard polar transform. You can reed more in the manual 103.4.2 Installing Nonlinear Transformation. – Kpym Jun 22 '15 at 13:47
• Ok, clear. btw, the data used are from 0 to 50deg but the plot goese over 150deg, there is something wrong. moreless 3,14 bigger.. – mark Jun 22 '15 at 14:03
• @mark corrected now. It is not 3,14... but 3.12980314528 because .11cm=3.12980314528pt ;) – Kpym Jun 22 '15 at 16:30