10

I am using the polar library with pgfplots to plot a graph like this:

\begin{tikzpicture}
\begin{polaraxis}
\addplot[mark = none, domain = 0.4:12, samples = 600, data cs = polarrad]{sin(x)};
\end{polaraxis}
\end{tikzpicture}

As described in the manual, this uses radians for the function, but the axis labels are still plotted in degrees. Instead of having the 0, 30, …, 330 tick labels, I would like 0π, π/6, … 11π/6 labels. I am convinced the solution must be quite simple, but I have not been able to find it yet.

5
  • Would xticklabel={$\pgfmathparse{\tick/180}\pgfmathprintnumber[frac,frac denom=6,frac whole=false]{\pgfmathresult}\pi$} option be sufficient for your needs?
    – percusse
    Commented Mar 3, 2013 at 13:23
  • @percusse setting xticklabel has no effect when I try it? (redefining \pgfplots@show@ticklabel@@polar has some effect but I guess that isn't the official interface? Commented Mar 3, 2013 at 13:49
  • This is a solution that I could live with, thank you very much! (@DavidCarlisle it works for me.) Of course it would be nicer to have simplified fractions, but if this is the simplest solution, then I guess there is no built-in way to label axes using radians.
    – Ruud
    Commented Mar 3, 2013 at 13:50
  • @DavidCarlisle You have to include \usepgfplotslibrary{polar}, let me cook up a MWE.
    – percusse
    Commented Mar 3, 2013 at 13:51
  • @percusse yes I got that far and can run the MWE and get a plot. I guess I should just leave pgf questions to egreg... Commented Mar 3, 2013 at 13:58

2 Answers 2

9

I've simplified the fractions a little more

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\usepgfplotslibrary{polar}
\begin{document}

\begin{tikzpicture}
\begin{polaraxis}[
xticklabel={
\pgfmathparse{\tick/180}
\pgfmathifisint{\pgfmathresult}{$\pgfmathprintnumber[int detect]{\pgfmathresult}\pi$}%
{$\pgfmathprintnumber[frac,frac denom=6,frac whole=false]{\pgfmathresult}\pi$}
}
]
\addplot[mark = none, domain = 0.4:12, samples = 600, data cs = polarrad]{sin(x)};
\end{polaraxis}
\end{tikzpicture}

\end{document}

enter image description here

You can introduce yet another \ifnum inside the integer check to remove 1 from 1pi but seems like an overkill to me. It's pretty readable in my humble opinion.

2
  • oh flip I nearly answered this an hour ago and could have had lots of lovely tikz points but I added xticklabel to the wrong place (\addplot) +1 to you +0 to me:( Commented Mar 3, 2013 at 14:11
  • @DavidCarlisle Go ahaed and post it please I'm not super happy with the way it is anyways. :)
    – percusse
    Commented Mar 3, 2013 at 14:12
1

You can also skip the pgf math and simply specify xticklabels (note the plural) yourself:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepgfplotslibrary{polar}
\begin{document}

\begin{tikzpicture}
\begin{polaraxis}[
xticklabels={,0,$\frac\pi6$,$\frac\pi3$,$\frac\pi2$,$\frac{2\pi}3$,$\frac{5\pi}6$,
$\pi$,$\frac{7\pi}6$,$\frac{4\pi}3$,$\frac{3\pi}2$,$\frac{5\pi}3$,$\frac{11\pi}6$}
]
\addplot[mark = none, domain = 0.4:12, samples = 600, data cs = polarrad]{sin(x)};
\end{polaraxis}
\end{tikzpicture}

\end{document}

TeX output

I'm not sure what happens to the first label before 0. (You may also want sin(deg(x)) since the argument is still interpreted as degrees.)

You must log in to answer this question.

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