尝试绘制具有域的双曲面x^2+y^2 <= 50
。目前,我已将域设置为 [-5,5],但这会产生丑陋的图,见下文。有什么建议可以解决这个问题?我尝试根据 x 和 y 设置域(根据 y 设置 x 域,反之亦然),但无济于事。
理想情况下,如果有的话,我想将相同的解决方案应用于梯度矢量场。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\xdefinecolor{lightgrey}{RGB}{220,220,220}
\xdefinecolor{goldenrod}{RGB}{255,223,66}
\xdefinecolor{newblue}{RGB}{57,106,177}
\xdefinecolor{newred}{RGB}{204,37,41}
\xdefinecolor{newgreen}{RGB}{132,186,91}
\xdefinecolor{newpurple}{RGB}{144,103,167}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[axis equal, view={0}{90}]
\addplot3[surf,shader=interp,opacity=0.5,domain=-5:5]
{(1/100)(50-x^2-y^2)};
\addplot [domain=0:2*pi,samples=50]({cos(deg(x))},{sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({2*cos(deg(x))},{2*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({3*cos(deg(x))},{3*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({4*cos(deg(x))},{4*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({5*cos(deg(x))},{5*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({6*cos(deg(x))},{6*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({7*cos(deg(x))},{7*sin(deg(x))});
\addplot[newpurple,double=newpurple,->] plot coordinates {
(0,0)
(-1,3)
};
\addplot[newblue,double=newblue,->] plot coordinates {
(0,0)
(7,-1)
};
\addplot[newred,double=newred,->] plot coordinates {
(7,-1)
(-1,3)
};
\addplot+[newred,double=newred] plot coordinates {
(1,2)
};
\addplot[newgreen,double=newgreen] plot coordinates {
(-3,-6)
(3,6)
};
\addplot3[blue,/pgfplots/quiver,
quiver/u=-x/50,
quiver/v=-y/50,
quiver/scale arrows=0.1,
-stealth,samples=10] {1};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您可以使用data cs=polar
提供极坐标中的方程式。此外,您还可以使用\foreach
循环来简化代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[axis equal image, view={0}{90}]
\addplot3 [
surf,
shader=interp,
opacity=0.5,
domain=0:360,
y domain=0:7,
samples=100, samples y=7,
data cs=polar
] {(50-y^2)};
\foreach \i in {1,...,7}{
\addplot [domain=0:360,samples=100, data cs=polar] {\i};
}
\draw [purple, ultra thick, ->] (axis cs:0,0) -- (axis cs:-1,3);
\draw [blue, ultra thick, ->] (axis cs:0,0) -- (axis cs:7,-1);
\draw [red, ultra thick, ->] (axis cs:7,-1) -- (axis cs:-1,3);
\addplot3[blue,/pgfplots/quiver,
quiver/u=-cos(x),
quiver/v=-sin(x),
quiver/scale arrows=0.1,
-stealth,samples=10,
domain=0:360,
y domain=0:sqrt(50),
data cs=polar] {1};
\end{axis}
\end{tikzpicture}
\end{document}
如果您想要坚持使用笛卡尔坐标来指定函数,则可以使用限制颜色图的范围point meta min=0
,并使用覆盖表面图的不需要的部分\fill [white] (rel axis cs:0,0) rectangle (rel axis cs:1,1) (axis cs:0,0) circle [radius=7];
(这需要\pgfplotsset{compat=1.4}
或更高)。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\xdefinecolor{lightgrey}{RGB}{220,220,220}
\xdefinecolor{goldenrod}{RGB}{255,223,66}
\xdefinecolor{newblue}{RGB}{57,106,177}
\xdefinecolor{newred}{RGB}{204,37,41}
\xdefinecolor{newgreen}{RGB}{132,186,91}
\xdefinecolor{newpurple}{RGB}{144,103,167}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[axis equal, view={0}{90}]
\addplot3[surf,shader=interp,opacity=0.5,domain=-7:7, point meta min=1]
{(50-x^2-y^2)};
\addplot [domain=0:2*pi,samples=50]({cos(deg(x))},{sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({2*cos(deg(x))},{2*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({3*cos(deg(x))},{3*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({4*cos(deg(x))},{4*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({5*cos(deg(x))},{5*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({6*cos(deg(x))},{6*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({7*cos(deg(x))},{7*sin(deg(x))});
\addplot[newpurple,double=newpurple,->] plot coordinates {
(0,0)
(-1,3)
};
\addplot[newblue,double=newblue,->] plot coordinates {
(0,0)
(7,-1)
};
\addplot[newred,double=newred,->] plot coordinates {
(7,-1)
(-1,3)
};
\addplot+[newred,double=newred] plot coordinates {
(1,2)
};
\addplot[newgreen,double=newgreen] plot coordinates {
(-3,-6)
(3,6)
};
\addplot3[blue,/pgfplots/quiver,
quiver/u=-x/50,
quiver/v=-y/50,
quiver/scale arrows=0.1,
-stealth,samples=10] {1};
\fill [white] (rel axis cs:0,0) rectangle (rel axis cs:1,1) (axis cs:0,0) circle [radius=7];
\end{axis}
\end{tikzpicture}
\end{document}