14

I'm currently using tikzpicture and pgfplots to plot this polar function:

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

\begin{tikzpicture}
\begin{polaraxis}
    \addplot[red,domain=0:360,samples=360,smooth] (x,{sqrt(4/(cos(4*x)+3))});
\end{polaraxis}
\end{tikzpicture}

\end{document}

However, this plots it on a polar axis, which doesn't really make sense in context. (It's defined implicitly as x4 + y4 = x2 + y2 and I'm just using polar coordinates to easily plot it.)

How can I get the same nice graph with the correct Cartesian grid, as Wolfram|Alpha displays it?

I considered \begin{polaraxis}[hide axis] and an additional \begin{axis} but that seems like it would require manual alignment, etc.

Is there a clean way to do this?

4
  • Can you provide a minimum working example please? Commented Dec 16, 2013 at 6:28
  • @NictraSavios done.
    – wchargin
    Commented Dec 16, 2013 at 17:04
  • @WChargin: Christian's answer is much better, it would be best if you could accept that instead of mine, so I can delete mine.
    – Jake
    Commented Dec 16, 2013 at 21:35
  • @Jake You're right that in this case it's nice to have a dedicated option. However, I appreciate that you can do arbitrary transforms, so it might be good to keep your answer too.
    – wchargin
    Commented Dec 16, 2013 at 23:26

2 Answers 2

14

You can tell pgfplots that the input is actually given in polar coordinates using data cs=polar. Pgfplots will automatically transform it to the output coordinate system:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis lines=center,
    axis equal image,
    enlargelimits=true,
     ]
    \addplot[data cs=polar,red,domain=0:360,samples=360,smooth] (x,{sqrt(4/(cos(4*x)+3))});
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

This key can also be used to provide cartesian coordinates in polar axis or other variations.

20

You can transform the polar coordinates to cartesian using an x filter and a y filter. If you wrap those in a style like this:

\pgfplotsset{
    interpret as polar/.style={
            x filter/.code=\pgfmathparse{cos(rawx)*rawy},
            y filter/.code=\pgfmathparse{sin(rawx)*rawy}
        }
}

you can just add interpret as polar to your \addplot options:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{
    interpret as polar/.style={
            x filter/.code=\pgfmathparse{cos(rawx)*rawy},
            y filter/.code=\pgfmathparse{sin(rawx)*rawy}
        }
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
            axis lines=center,
            axis equal image,
            enlargelimits=true
        ]
    \addplot[
            red, thick,
            domain=0:360,
            samples=360,
            smooth,
            interpret as polar
        ] (x,{sqrt(4/(cos(4*x)+3))});
\end{axis}
\end{tikzpicture}
\end{document}
0

You must log in to answer this question.

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