我有一个函数 $\rho(x, y, z) = e^{-|x|}$,我想绘制它。它产生一个 3d 体积 - 我已经在 Julia 中绘制了它(使用 Makie.jl),它看起来像这样:
我希望能够在 PGFplots 中绘制类似的东西。我想出了一个相当“老套”的解决方案,虽然可行,但我当然希望有更好的解决方案。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\pgfplotsset{compat = 1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
colormap/viridis,
colorbar,
xlabel = {$x$},
ylabel = {$y$},
zlabel = {$z$},
title = {$\rho = e^{-|x|}$}
]
\addplot3 [
scatter,
only marks,
mark = cube*,
opacity = {0.175*\pgfplotspointmetatransformed/2000},
% mesh/ordering = x varies,
point meta = {exp(-abs(x))}
] table {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}
其中,data.dat
只是立方体中的 x、y 和 z 列。
x y z
-3.0 -3.0 -3.0
-3.0 -3.0 -2.9
[...]
3.0 3.0 3.0
有什么方法可以使 PGFplots 可视化效果更好?
答案1
欢迎来到 TeX-SE!好问题!怎么样?
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\pgfplotsset{compat = 1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-2.6,xmax=2.5,ymin=-5,ymax=5.1,zmin=0.78,zmax=1.2,
colormap/viridis,
colorbar,
xlabel = {$x$},
ylabel = {$y$},
zlabel = {$z$},
title = {$\rho = e^{-|x|}$}
]
%\pgfplotsi
\pgfplotscolormapdefinemappedcolor{0}
\fill[mapped color] (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymin}+0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01) -- (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01)
-- (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmax}-0.01) -- (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymin}+0.01,\pgfkeysvalueof{/pgfplots/zmax}-0.01) -- cycle;
\fill[mapped color] (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01) -- (\pgfkeysvalueof{/pgfplots/xmax}-0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01)
-- (\pgfkeysvalueof{/pgfplots/xmax}-0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmax}-0.01) -- (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmax}-0.01) -- cycle;
\fill[mapped color] (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01) -- (\pgfkeysvalueof{/pgfplots/xmax}-0.01,\pgfkeysvalueof{/pgfplots/ymax}-0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01)
-- (\pgfkeysvalueof{/pgfplots/xmax}-0.01,\pgfkeysvalueof{/pgfplots/ymin}+0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01) -- (\pgfkeysvalueof{/pgfplots/xmin}+0.01,\pgfkeysvalueof{/pgfplots/ymin}+0.01,\pgfkeysvalueof{/pgfplots/zmin}+0.01) -- cycle;
\pgfplotsinvokeforeach{-25,-24,...,25}{
\addplot3 [surf,shader=flat,samples=2,samples y=2,domain=0.8:1.2,domain y=-5:5,
opacity = {0.5*exp(-3*abs(#1/25))},
point meta = {exp(-abs(#1/25))}
] (#1/25,y,x);
}
\end{axis}
\end{tikzpicture}
\end{document}
你可以尝试一下。例如,如果你将不透明度函数更改为
opacity = {0.5*exp(-5*abs(#1/25))},
你得到