[Tex/LaTex] How to create a polar plot on a Cartesian grid

pgfplotspolarplottikz-pgf

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?

Best Answer

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.