![tikzpicture 的 3D 凹凸功能](https://linux22.com/image/441871/tikzpicture%20%E7%9A%84%203D%20%E5%87%B9%E5%87%B8%E5%8A%9F%E8%83%BD.png)
我正在尝试使用 tikzpicture 绘制 3D 凹凸函数。手工制作的想法是:
现在我得到的最好的结果是:
\begin{tikzpicture}
\coordinate (z) at (0,4);
\coordinate (y) at (7,0);
\coordinate (x) at (-1,-1);
\draw[->] (-1,0)--(7,0);
\draw[->] (0,-0.5)--(0,4);
\draw[->] (0.3,0.3)--(-1,-1);
\draw (y) node[right] {$y$};
\draw (x) node[left] {$x$};
\draw (z) node[left] {$z$};
\draw[thick,scale=1.3, domain=-0.05:3.05, smooth, variable=\x, blue] plot ({\x}, {exp(-(\x-2)*(\x*2-2)+0.1)-0.5});
\draw[fill=blue, opacity=0.3, thick,scale=1.3, domain=0:3, smooth, variable=\x, blue] plot ({\x}, {exp(-(\x-2)*(\x*2-2)+0.1)-0.5});
\draw [fill=blue, opacity=0.3, blue] (-0.05,-0.6235) arc (180:360:2 and 0.3) ;
\draw [opacity=0.4,blue,dashed] (-0.05,-0.64) arc (170:10:2 and 0.2) ;
\draw [opacity=0.4,blue] (1.53,1.3) arc (180:360:0.42 and 0.13) ;
\draw [dashed ,opacity=0.4,blue] (1.53,1.3) arc (180:0:0.42 and 0.1) ;
\draw [opacity=0.4,blue] (1.1,0.3) arc (180:360:0.85 and 0.14) ;
\draw [dashed,opacity=0.4,blue] (1.1,0.3) arc (180:0:0.85 and 0.14) ;
\end{tikzpicture}
你能帮我画一幅更像手工画的画吗?非常感谢!
答案1
您可以使用例如 PGFPlots
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={100}{30},
xmin=-1, xmax=6,
ymin=-1, ymax=6,
zmin=-1, zmax=4,
xlabel={x}, ylabel={y}, zlabel={z},
axis lines=center,
ticks=none,
]
\addplot3[
surf, colormap/viridis, faceted color=gray,
opacity=0.5,
domain=1:5, y domain=1:5,
samples=20, samples y=20,
line join=round,
] {2*exp(-(\x-3)^2-(\y-3)^2)};
\end{axis}
\end{tikzpicture}
\end{document}
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={100}{30},
xmin=-1, xmax=6,
ymin=-1, ymax=6,
zmin=-1, zmax=4,
xlabel={x}, ylabel={y}, zlabel={z},
axis lines=center,
ticks=none,
]
\addplot3[
surf, colormap/viridis, faceted color=gray,
opacity=0.5,
domain=0:360, domain y=0:2,
samples=10, samples y=20,
line join=round,
] ( {y*cos(x)+3} , {y*sin(x)+3} , {2*exp(-(y*cos(x))^2-(y*sin(x))^2)} );
\end{axis}
\end{tikzpicture}
\end{document}
编辑:仅限 TikZ
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary {3d}
\begin{document}
\begin{tikzpicture}[rotate around x=-90, rotate around z=-100]
\draw[->] (-1,0,0) -- (5,0,0) node[above]{x};
\draw[->] (0,-1,0) -- (0,5,0) node[above]{y};
\draw[->] (0,0,-1) -- (0,0,3) node[above]{z};
\begin{scope}[transparency group, opacity=0.5]
\fill[blue] plot[domain=0:360, samples=100, variable=\ang, smooth] ( {2*cos(\ang)+3} , {2*sin(\ang)+3} , {2*exp(-(2*cos(\ang))^2-(2*sin(\ang))^2)} );
\fill[blue] plot[domain=-2:2, samples=10, variable=\t, smooth] ( {\t*cos(120)+3} , {\t*sin(120)+3} , {2*exp(-(\t*cos(120))^2-(\t*sin(120))^2)} );
\end{scope}
\foreach \ang in {0,40,...,320}
\draw[gray, thick] plot[domain=0:2, samples=10, variable=\t, smooth] ( {\t*cos(\ang)+3} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \t in {0,0.3,...,2}
\draw[gray, thick] plot[domain=0:360, samples=100, variable=\ang, smooth] ( {\t*cos(\ang)+3} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\end{tikzpicture}
\end{document}
答案2
感谢 hpekristiansen,我能够创建一系列透明的凹凸:
\[ \begin{tikzpicture}[rotate around x=-90, rotate around z=-100]
\begin{scope}[transparency group, opacity=0.5]
\fill[blue] plot[domain=0:360, samples=100, variable=\ang, smooth] ( {2*cos(\ang)} , {2*sin(\ang)+3} , {2*exp(-(2*cos(\ang))^2-(2*sin(\ang))^2)} );
\fill[blue] plot[domain=-2:2, samples=10, variable=\t, smooth] ( {\t*cos(120)} , {\t*sin(120)+3} , {2*exp(-(\t*cos(120))^2-(\t*sin(120))^2)} );
\end{scope}
\draw[->] (-1,0,0) -- (5,0,0) node[above]{x};
\draw[->] (0,-1,0) -- (0,5,0) node[above]{y};
\draw[->] (0,0,-1) -- (0,0,3) node[above]{z};
\foreach \ang in {-80,-50,...,130}
\draw[black,opacity=0.4] plot[domain=0:2, samples=10, variable=\t, smooth] ( {\t*cos(\ang)} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \ang in {130,150,...,270}
\draw[black,dashed,opacity=0.4] plot[domain=0:2, samples=10, variable=\t, smooth] ( {\t*cos(\ang)} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \t in {0,0.4,...,2}
\draw[black,opacity=0.4] plot[domain=-90:130, samples=100, variable=\ang, smooth] ( {\t*cos(\ang)} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \t in {0,0.4,...,2}
\draw[black,dashed,opacity=0.4] plot[domain=130:270, samples=100, variable=\ang, smooth] ( {\t*cos(\ang)} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\begin{scope}[transparency group, opacity=0.3]
\fill[blue] plot[domain=0:360, samples=100, variable=\ang, smooth] ( {2*cos(\ang)+3} , {2*sin(\ang)+3} , {2*exp(-(2*cos(\ang))^2-(2*sin(\ang))^2)} );
\fill[blue] plot[domain=-2:2, samples=10, variable=\t, smooth] ( {\t*cos(120)+3} , {\t*sin(120)+3} , {2*exp(-(\t*cos(120))^2-(\t*sin(120))^2)} );
\end{scope}
\draw[->] (-1,0,0) -- (5,0,0) node[above]{x};
\draw[->] (0,-1,0) -- (0,5,0) node[above]{y};
\draw[->] (0,0,-1) -- (0,0,3) node[above]{z};
\foreach \ang in {-80,-50,...,130}
\draw[black,opacity=0.4] plot[domain=0:2, samples=10, variable=\t, smooth] ( {\t*cos(\ang)+3} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \ang in {130,150,...,270}
\draw[black,dashed,opacity=0.4] plot[domain=0:2, samples=10, variable=\t, smooth] ( {\t*cos(\ang)+3} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \t in {0,0.4,...,2}
\draw[black,opacity=0.4] plot[domain=-90:130, samples=100, variable=\ang, smooth] ( {\t*cos(\ang)+3} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \t in {0,0.4,...,2}
\draw[black,dashed,opacity=0.4] plot[domain=130:270, samples=100, variable=\ang, smooth] ( {\t*cos(\ang)+3} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\begin{scope}[transparency group, opacity=0.1]
\fill[blue] plot[domain=0:360, samples=100, variable=\ang, smooth] ( {2*cos(\ang)+6} , {2*sin(\ang)+3} , {2*exp(-(2*cos(\ang))^2-(2*sin(\ang))^2)} );
\fill[blue] plot[domain=-2:2, samples=10, variable=\t, smooth] ( {\t*cos(120)+6} , {\t*sin(120)+3} , {2*exp(-(\t*cos(120))^2-(\t*sin(120))^2)} );
\end{scope}
\draw[->] (-1,0,0) -- (5,0,0) node[above]{x};
\draw[->] (0,-1,0) -- (0,5,0) node[above]{y};
\draw[->] (0,0,-1) -- (0,0,3) node[above]{z};
\foreach \ang in {-80,-50,...,130}
\draw[black,opacity=0.4] plot[domain=0:2, samples=10, variable=\t, smooth] ( {\t*cos(\ang)+6} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \ang in {130,150,...,270}
\draw[black,dashed,opacity=0.4] plot[domain=0:2, samples=10, variable=\t, smooth] ( {\t*cos(\ang)+6} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \t in {0,0.4,...,2}
\draw[black,opacity=0.4] plot[domain=-90:130, samples=100, variable=\ang, smooth] ( {\t*cos(\ang)+6} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\foreach \t in {0,0.4,...,2}
\draw[black,dashed,opacity=0.4] plot[domain=130:270, samples=100, variable=\ang, smooth] ( {\t*cos(\ang)+6} , {\t*sin(\ang)+3} , {2*exp(-(\t*cos(\ang))^2-(\t*sin(\ang))^2)} );
\end{tikzpicture} \]
因此我可以根据 2D 图形创建 3D 图形: