2

I have a problem using custom xticklabels in my polar plot. I need the exact xtick sequence specified by xticklabels but no matter what I do, the first label for 120° is not displayed.

\documentclass[12pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.groupplots}
\usepgfplotslibrary{polar}
\usetikzlibrary{pgfplots.polar}

\begin{document}
    \begin{tikzpicture}
        \begin{polaraxis}[
            xmin=0,xmax=360,
            xticklabels={120$^\circ$,90$^\circ$,60$^\circ$,30$^\circ$,0$^\circ$,330$^\circ$,300$^\circ$,270$^\circ$,240$^\circ$,210$^\circ$,180$^\circ$,150$^\circ$},
            width = 0.5\textwidth,
            no markers,
            xticklabel style={font=\large}]
        \end{polaraxis}
    \end{tikzpicture}
\end{document}
  • Please make your code compilable. Right now, compilation fails with errors. – cfr Dec 22 '15 at 3:25
  • Why do you want to put the wrong labels on? Why do you want to put e.g. 90 for 0 or whatever rather than 0 for 0 and 90 for 90? If you remove the xticklabels line and fix the problems with the preamble, you get a full set of labels at the appropriate places. – cfr Dec 22 '15 at 3:30
  • I'm sorry about the code; now it should work. The problem is that the data I want to plot refers to the "wrong" labels. For me it is much easier to just modify the labels than changing the data set. – relab Dec 22 '15 at 3:56
  • 1
    @cfr They're not wrong if you want to plot the degrees of a compass, where north is 0, east is 90, etc. For example if you're plotting vectors of wind speed or ocean current. – Torbjørn T. Dec 22 '15 at 7:11
  • @cfr (Too early in the morning, didn't think.) You're right of course, they will be wrong compared to the data, so one has to transform the data as well. – Torbjørn T. Dec 22 '15 at 8:18
4

y dir=reverse

In the question Rotating polar pgfplot MajorC shows a much simpler way of achieving this, so my old answer (see below) is left only as a curiosity. All that's needed are the axis options

 y dir=reverse,rotate=90,
 xticklabel style={anchor=-\tick-90}, 

enter image description here

\documentclass[12pt,border=4mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\begin{document}
\begin{tikzpicture}
\begin{polaraxis}[
    y dir=reverse,rotate=90,
    xticklabel style={anchor=-\tick-90}, 
    y tick label style={left},
]

\addplot+ [very thick, polar comb] coordinates {(45,0.25)};
\addplot+ [very thick, polar comb] coordinates {(135,0.5)};
\addplot+ [very thick, polar comb] coordinates {(225,0.75)};
\addplot+ [very thick, polar comb] coordinates {(315,1)};

\end{polaraxis}
\end{tikzpicture}
\end{document}

Old answer: horrible workaround


For a more direct solution to your question, see the bottom of the answer.


Here is a different approach, where the axis is rotated and flipped so that the angles are as desired. This is done with the axis options x post scale=-1,rotate=-90. The rotation puts zero at the top, the scaling flips it around the north-south axis.

The other new options for the axis are

  • xtick=\ticklist: Because the list of ticks are used twice, it's saved to a macro with \newcommand\ticklist{0,30,...,330}.

  • xticklabels={}: Turns off all ticklabels showing the degrees, as these are added manually later.

  • y tick label style={left}: By default the "y"-ticklabels are placed above the horizontal line. Due to the rotation they are place by the vertical line, and above doesn't work very well there. Therefor I place them left instead.

  • tick style={draw=none}: The tickmarks were pointing in very wrong directions, so I turn them off completely.

Adding the xticklabels

You could add a series of nodes of the form \node [anchor={atan2(cos(30),sin(30))+180}] at (axis cs:30,\pgfkeysvalueof{/pgfplots/ymax}) {30}; inside the polaraxis environment, replacing all the occurrences of 30 with other angles. That is rather inconvenient, and using a loop as described in section 8.1 of the pgfplots manual didn't work in a polaraxis. Instead I use a workaround, as follows.

  1. Add \usetikzlibrary{through}.
  2. Inside the polaraxis, add two coordinates, one in the center, and one on the outer edge of the axis:

    \coordinate (top of axis) at (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
    \coordinate (axis center) at (axis cs:0,0);
    
  3. After \end{polaraxis}, add a circular node with

    \node (axis circle) at (axis center) [circle through={(top of axis)}] {};
    

    circle through is defined by the through library. As the center of the node is in the center of the axis, and it passes through a point on the edge of the axis, the node edge will be placed exactly on the axis edge.

  4. Finally the ticklabels are added with

    \foreach [evaluate=\i as \j using {atan2(cos(\i),sin(\i))}] \i in \ticklist
       \node [anchor={\j+180}] at (axis circle.\j) {\i};
    

    atan2(cos(\i),sin(\i)) transforms from degrees in a "compass like" polar axis, to those in a "normal" polar axis.

enter image description here

\documentclass[12pt,border=4mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\usetikzlibrary{through}
\begin{document}
\begin{tikzpicture}
\newcommand\ticklist{0,30,...,330}
\begin{polaraxis}[
    xmin=0,xmax=360,
    width = 0.5\textwidth,
    xticklabel style={font=\large},
    x post scale=-1,rotate=-90,
    xtick=\ticklist,xticklabels={},y tick label style={left},
    tick style={draw=none}
]

\addplot+ [very thick, polar comb] coordinates {(45,0.25)};
\addplot+ [very thick, polar comb] coordinates {(135,0.5)};
\addplot+ [very thick, polar comb] coordinates {(225,0.75)};
\addplot+ [very thick, polar comb] coordinates {(315,1)};

\coordinate (top of axis) at (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
\coordinate (axis center) at (axis cs:0,0);
\end{polaraxis}
\node (axis circle) at (axis center) [circle through={(top of axis)}] {};
\foreach [evaluate=\i as \j using {atan2(cos(\i),sin(\i))}] \i in \ticklist
   \node [anchor={\j+180}] at (axis circle.\j) {\i};
\end{tikzpicture}
\end{document}

Wrong labels

The following code seems to work fine:

\documentclass[12pt,border=4mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}    
\begin{document}
    \begin{tikzpicture}
        \begin{polaraxis}[
            xmin=0,xmax=360,
            xtick={0,30,...,330},
            xticklabels={90$^\circ$,60$^\circ$,30$^\circ$,0$^\circ$,330$^\circ$,300$^\circ$,270$^\circ$,240$^\circ$,210$^\circ$,180$^\circ$,150$^\circ$,120$^\circ$},
            width = 0.5\textwidth,
            no markers,
            xticklabel style={font=\large}]
            \addplot+ [polar comb] coordinates {(120,0.5)};
        \end{polaraxis}
    \end{tikzpicture}
\end{document}

I haven't done much though. The preamble is cleaned up a bit. In the axis options the news is that I specified the tick locations explicitly (xtick={0,30,...,330}), and I moved the ticklabel for 120 degrees to the end of the list. There is one caveat though:

  • This only works when something is plotted.

If you remove the \addplot command from the above code, it no longer works.

enter image description here

  • 1
    Nice! That is, I'm impressed that you can offer even an inconvenient solution ;). – cfr Dec 22 '15 at 22:09
  • 1
    @cfr The new one is a little bit less inconvenient. – Torbjørn T. Dec 22 '15 at 23:27
  • 2
    Can't upvote twice. It will only let me down-vote the added convenience and I don't want to do that! Too bad - that is quite ingenious. – cfr Dec 23 '15 at 0:02

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.