如何绘制 atan2(x,y)?

如何绘制 atan2(x,y)?

我如何绘制像维基百科图片那样的 atan2(y,x)

https://en.wikipedia.org/wiki/Atan2#/media/File:Atan2_diagram.svg

我试过

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
 xlabel=$x$, ylabel=$y$,
 axis lines=center,
%  use fpu=false
]
\addplot3[surf,domain=-1:1,domain y=-1:1,]
{atan2(y,x)};
\end{axis}
\end{tikzpicture}

\end{document}

但 z 比例看起来不一样。是否可以在平面边缘画一条虚线?

答案1

通过将图分成两部分并z filter/.expression={x<0 && y==0? -pi : z}在其中一个图上使用,您可以获得从两侧接近的正确极限。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
 xlabel=$x$, ylabel=$y$, zlabel=$z$,
 axis x line=bottom, axis y line=left, axis z line=right,
 xtick={-1,-0.5,0,0.5,1}, xticklabels={-1, -1/2, 0, 1/2, 1},
 ytick={-1,-0.5,0,0.5,1}, yticklabels={-1, -1/2, 0, 1/2, 1},
 ztick={-pi,0,pi}, zticklabels={$-\pi$, 0, $\pi$},
 xmin=-1.1, xmax=1.1,
 ymin=-1.1, ymax=1.1,
 zmin=-3.3, zmax=3.3,
 x axis line style=-,  y axis line style=-,  z axis line style=-,
 unbounded coords=jump
]
\addplot3[
 surf, colormap name=viridis, 
 domain=-1:1, domain y=-1:0,
 z filter/.expression={x<0 && y==0? -pi : z},
 samples=51,
 ] {rad(atan2(y,x))};
 \addplot3[
 surf, colormap name=viridis, 
 domain=-1:1, domain y=0:1,
 samples=51,
 ] {rad(atan2(y,x))};
\end{axis}
\end{tikzpicture}
\end{document}

图形

答案2

如果你可以使用knitr

姆韦


\documentclass{article}
\usepackage{lipsum}\parindent0pt
\begin{document}
\lipsum[1][1-4]
<<echo=F,dev="tikz",message=F, fig.height=4, fig.align='center', fig.cap="The famous 3D plot",fig.pos="hb!", crop = T>>=
library(lattice)
knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)
g <- seq(-95, 95, 15)
df <- expand.grid(g,g,g)
names(df) <- c('x','y','z')
df$z <- atan2(df$x,df$y) 
trellis.par.set( "axis.line",  list(col=NA,lty=1,lwd=0))
wireframe(z ~ x * y, df, log="xy",
screen=list(x=-35,y=40,z=35),
xlab=list("$x$",rot=40), 
zlab=list("\\footnotesize $z=\\mathop{\\mathrm{atan2}}(x,y)$",rot=90), 
ylab=list("$y$ ",rot=-35), 
col.regions=rainbow(150),
x2=df$x,
scales = list(arrows=F,cex=.8,tick.number =3,lwd=1,col="black"), 
aspect = c(1, .6), drape = T, colorkey=F, perspective=T,
par.settings = list(box.3d = list(col="darkgray",lwd=2)))
@
\lipsum[2-3]
\end{document}

答案3

一个想法是使用两个域。

\documentclass[border=9,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[xlabel=$x$, ylabel=$y$,]
        \addplot3[surf,domain=-1:1,domain y= 0:1]{atan2(y,x)/180*pi};
        \addplot3[surf,domain=-1:1,domain y=-1:0]{atan2(-y,-x)/180*pi-pi};
        \end{axis}
    \end{tikzpicture}
\end{document}

PS. @vi pa 的回答比我的早

如果您确实想要原点的垂直线,您可以将中心方块折叠到该线上。

\documentclass[border=9,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
    \pgfmathdeclarefunction{snap}{1}{%
        \pgfmathparse{#1-sign(#1)/19}%
                     % denominator = number of samples - 1
    }
    \pgfmathdeclarefunction{atan2blowup}{2}{%
        \pgfmathsetmacro\truex{snap(#1)}%
        \pgfmathsetmacro\truey{snap(#2)}%
        \pgfmathparse{%
            ifthenelse((\truex==0)*(\truey==0),%
                atan2(#1,#2),%
                atan2(snap(#1),snap(#2))%
            )%
        }%
    }
    \begin{tikzpicture}[line join=round,line cap=round]
        \begin{axis}[xlabel=$x$, ylabel=$y$,samples=20]
                                % number of samples must be even
            \addplot3[surf,domain=-1:1,domain y=0:1]
                ({snap(\x)},{snap(\y)},{atan2blowup(y,x)/180*pi});
            \addplot3[surf,domain=-1:1,domain y=0:1]
                ({snap(-\x)},{snap(-\y)},{atan2blowup(y,x)/180*pi-pi});
        \end{axis}
    \end{tikzpicture}
\end{document}

答案4

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
% xlabel=$x$, ylabel=$y$,
%axis lines=center,
%  use fpu=false
]

\addplot3[surf,domain=-20:20,domain y=-20:-0.01]
{rad(atan2(y,x))};
\addplot3[surf,domain=-20:20,domain y=0.01:20]
{rad(atan2(y,x))};

\end{axis}
\end{tikzpicture}

\end{document}

atan2 图

相关内容