pgfplots:如何在长方体表面绘制体积数据?

pgfplots:如何在长方体表面绘制体积数据?

在 MATLAB 中我生成了下面的图。在我的任务中,我只有来自长方体表面上显示的 3 个切片的数据。我没有完整的体积信息,就像下面的 MATLAB 代码中显示的那样。是否可以使用 pgfplots 在绘图框的切片上实现 3 个表面图?

[x,y,z] = meshgrid(-1:.1:1,-1:.1:1,-1:.1:0);
v = x.*exp(-x.^2-y.^2-z.^2);
xslice = [-1]; yslice = [-1]; zslice = [0];

s1=slice(x,y,z,v,xslice,yslice,zslice)

box on
set(gca,'DataAspectRatio',[1 1 1])
set(s1,'FaceColor','interp',...
    'EdgeColor','none')

view([-60, 25])
xlim([-1, 1])
ylim([-1, 1])
zlim([-1, 0])

在此处输入图片描述

根据我的任务我创建了以下代码:

\documentclass[tikz,border=12pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{filecontents*}{xslice.dat}
# x     y       z       v
  -1.0000000e+00  -1.0000000e+00  -1.0000000e+00  -4.9787068e-02
  -1.0000000e+00  -1.0000000e+00  -7.5000000e-01  -7.7111720e-02
  -1.0000000e+00  -1.0000000e+00  -5.0000000e-01  -1.0539922e-01
  -1.0000000e+00  -1.0000000e+00  -2.5000000e-01  -1.2713573e-01
  -1.0000000e+00  -1.0000000e+00   0.0000000e+00  -1.3533528e-01
  -1.0000000e+00  -7.5000000e-01  -1.0000000e+00  -7.7111720e-02
  -1.0000000e+00  -7.5000000e-01  -7.5000000e-01  -1.1943297e-01
  -1.0000000e+00  -7.5000000e-01  -5.0000000e-01  -1.6324551e-01
  -1.0000000e+00  -7.5000000e-01  -2.5000000e-01  -1.9691168e-01
  -1.0000000e+00  -7.5000000e-01   0.0000000e+00  -2.0961139e-01
  -1.0000000e+00  -5.0000000e-01  -1.0000000e+00  -1.0539922e-01
  -1.0000000e+00  -5.0000000e-01  -7.5000000e-01  -1.6324551e-01
  -1.0000000e+00  -5.0000000e-01  -5.0000000e-01  -2.2313016e-01
  -1.0000000e+00  -5.0000000e-01  -2.5000000e-01  -2.6914635e-01
  -1.0000000e+00  -5.0000000e-01   0.0000000e+00  -2.8650480e-01
  -1.0000000e+00  -2.5000000e-01  -1.0000000e+00  -1.2713573e-01
  -1.0000000e+00  -2.5000000e-01  -7.5000000e-01  -1.9691168e-01
  -1.0000000e+00  -2.5000000e-01  -5.0000000e-01  -2.6914635e-01
  -1.0000000e+00  -2.5000000e-01  -2.5000000e-01  -3.2465247e-01
  -1.0000000e+00  -2.5000000e-01   0.0000000e+00  -3.4559075e-01
  -1.0000000e+00   0.0000000e+00  -1.0000000e+00  -1.3533528e-01
  -1.0000000e+00   0.0000000e+00  -7.5000000e-01  -2.0961139e-01
  -1.0000000e+00   0.0000000e+00  -5.0000000e-01  -2.8650480e-01
  -1.0000000e+00   0.0000000e+00  -2.5000000e-01  -3.4559075e-01
  -1.0000000e+00   0.0000000e+00   0.0000000e+00  -3.6787944e-01
  -1.0000000e+00   2.5000000e-01  -1.0000000e+00  -1.2713573e-01
  -1.0000000e+00   2.5000000e-01  -7.5000000e-01  -1.9691168e-01
  -1.0000000e+00   2.5000000e-01  -5.0000000e-01  -2.6914635e-01
  -1.0000000e+00   2.5000000e-01  -2.5000000e-01  -3.2465247e-01
  -1.0000000e+00   2.5000000e-01   0.0000000e+00  -3.4559075e-01
  -1.0000000e+00   5.0000000e-01  -1.0000000e+00  -1.0539922e-01
  -1.0000000e+00   5.0000000e-01  -7.5000000e-01  -1.6324551e-01
  -1.0000000e+00   5.0000000e-01  -5.0000000e-01  -2.2313016e-01
  -1.0000000e+00   5.0000000e-01  -2.5000000e-01  -2.6914635e-01
  -1.0000000e+00   5.0000000e-01   0.0000000e+00  -2.8650480e-01
  -1.0000000e+00   7.5000000e-01  -1.0000000e+00  -7.7111720e-02
  -1.0000000e+00   7.5000000e-01  -7.5000000e-01  -1.1943297e-01
  -1.0000000e+00   7.5000000e-01  -5.0000000e-01  -1.6324551e-01
  -1.0000000e+00   7.5000000e-01  -2.5000000e-01  -1.9691168e-01
  -1.0000000e+00   7.5000000e-01   0.0000000e+00  -2.0961139e-01
  -1.0000000e+00   1.0000000e+00  -1.0000000e+00  -4.9787068e-02
  -1.0000000e+00   1.0000000e+00  -7.5000000e-01  -7.7111720e-02
  -1.0000000e+00   1.0000000e+00  -5.0000000e-01  -1.0539922e-01
  -1.0000000e+00   1.0000000e+00  -2.5000000e-01  -1.2713573e-01
  -1.0000000e+00   1.0000000e+00   0.0000000e+00  -1.3533528e-01
\end{filecontents*}

\begin{filecontents*}{yslice.dat}
# x     y       z       v
  -1.0000000e+00  -1.0000000e+00  -1.0000000e+00  -4.9787068e-02
  -1.0000000e+00  -1.0000000e+00  -7.5000000e-01  -7.7111720e-02
  -1.0000000e+00  -1.0000000e+00  -5.0000000e-01  -1.0539922e-01
  -1.0000000e+00  -1.0000000e+00  -2.5000000e-01  -1.2713573e-01
  -1.0000000e+00  -1.0000000e+00   0.0000000e+00  -1.3533528e-01
  -7.5000000e-01  -1.0000000e+00  -1.0000000e+00  -5.7833790e-02
  -7.5000000e-01  -1.0000000e+00  -7.5000000e-01  -8.9574726e-02
  -7.5000000e-01  -1.0000000e+00  -5.0000000e-01  -1.2243413e-01
  -7.5000000e-01  -1.0000000e+00  -2.5000000e-01  -1.4768376e-01
  -7.5000000e-01  -1.0000000e+00   0.0000000e+00  -1.5720854e-01
  -5.0000000e-01  -1.0000000e+00  -1.0000000e+00  -5.2699612e-02
  -5.0000000e-01  -1.0000000e+00  -7.5000000e-01  -8.1622756e-02
  -5.0000000e-01  -1.0000000e+00  -5.0000000e-01  -1.1156508e-01
  -5.0000000e-01  -1.0000000e+00  -2.5000000e-01  -1.3457317e-01
  -5.0000000e-01  -1.0000000e+00   0.0000000e+00  -1.4325240e-01
  -2.5000000e-01  -1.0000000e+00  -1.0000000e+00  -3.1783933e-02
  -2.5000000e-01  -1.0000000e+00  -7.5000000e-01  -4.9227919e-02
  -2.5000000e-01  -1.0000000e+00  -5.0000000e-01  -6.7286587e-02
  -2.5000000e-01  -1.0000000e+00  -2.5000000e-01  -8.1163117e-02
  -2.5000000e-01  -1.0000000e+00   0.0000000e+00  -8.6397688e-02
   0.0000000e+00  -1.0000000e+00  -1.0000000e+00   0.0000000e+00
   0.0000000e+00  -1.0000000e+00  -7.5000000e-01   0.0000000e+00
   0.0000000e+00  -1.0000000e+00  -5.0000000e-01   0.0000000e+00
   0.0000000e+00  -1.0000000e+00  -2.5000000e-01   0.0000000e+00
   0.0000000e+00  -1.0000000e+00   0.0000000e+00   0.0000000e+00
   2.5000000e-01  -1.0000000e+00  -1.0000000e+00   3.1783933e-02
   2.5000000e-01  -1.0000000e+00  -7.5000000e-01   4.9227919e-02
   2.5000000e-01  -1.0000000e+00  -5.0000000e-01   6.7286587e-02
   2.5000000e-01  -1.0000000e+00  -2.5000000e-01   8.1163117e-02
   2.5000000e-01  -1.0000000e+00   0.0000000e+00   8.6397688e-02
   5.0000000e-01  -1.0000000e+00  -1.0000000e+00   5.2699612e-02
   5.0000000e-01  -1.0000000e+00  -7.5000000e-01   8.1622756e-02
   5.0000000e-01  -1.0000000e+00  -5.0000000e-01   1.1156508e-01
   5.0000000e-01  -1.0000000e+00  -2.5000000e-01   1.3457317e-01
   5.0000000e-01  -1.0000000e+00   0.0000000e+00   1.4325240e-01
   7.5000000e-01  -1.0000000e+00  -1.0000000e+00   5.7833790e-02
   7.5000000e-01  -1.0000000e+00  -7.5000000e-01   8.9574726e-02
   7.5000000e-01  -1.0000000e+00  -5.0000000e-01   1.2243413e-01
   7.5000000e-01  -1.0000000e+00  -2.5000000e-01   1.4768376e-01
   7.5000000e-01  -1.0000000e+00   0.0000000e+00   1.5720854e-01
   1.0000000e+00  -1.0000000e+00  -1.0000000e+00   4.9787068e-02
   1.0000000e+00  -1.0000000e+00  -7.5000000e-01   7.7111720e-02
   1.0000000e+00  -1.0000000e+00  -5.0000000e-01   1.0539922e-01
   1.0000000e+00  -1.0000000e+00  -2.5000000e-01   1.2713573e-01
   1.0000000e+00  -1.0000000e+00   0.0000000e+00   1.3533528e-01
\end{filecontents*}

\begin{filecontents*}{zslice.dat}
# x     y       z       v
  -1.0000000e+00  -1.0000000e+00   0.0000000e+00  -1.3533528e-01
  -7.5000000e-01  -1.0000000e+00   0.0000000e+00  -1.5720854e-01
  -5.0000000e-01  -1.0000000e+00   0.0000000e+00  -1.4325240e-01
  -2.5000000e-01  -1.0000000e+00   0.0000000e+00  -8.6397688e-02
   0.0000000e+00  -1.0000000e+00   0.0000000e+00   0.0000000e+00
   2.5000000e-01  -1.0000000e+00   0.0000000e+00   8.6397688e-02
   5.0000000e-01  -1.0000000e+00   0.0000000e+00   1.4325240e-01
   7.5000000e-01  -1.0000000e+00   0.0000000e+00   1.5720854e-01
   1.0000000e+00  -1.0000000e+00   0.0000000e+00   1.3533528e-01
  -1.0000000e+00  -7.5000000e-01   0.0000000e+00  -2.0961139e-01
  -7.5000000e-01  -7.5000000e-01   0.0000000e+00  -2.4348935e-01
  -5.0000000e-01  -7.5000000e-01   0.0000000e+00  -2.2187366e-01
  -2.5000000e-01  -7.5000000e-01   0.0000000e+00  -1.3381536e-01
   0.0000000e+00  -7.5000000e-01   0.0000000e+00   0.0000000e+00
   2.5000000e-01  -7.5000000e-01   0.0000000e+00   1.3381536e-01
   5.0000000e-01  -7.5000000e-01   0.0000000e+00   2.2187366e-01
   7.5000000e-01  -7.5000000e-01   0.0000000e+00   2.4348935e-01
   1.0000000e+00  -7.5000000e-01   0.0000000e+00   2.0961139e-01
  -1.0000000e+00  -5.0000000e-01   0.0000000e+00  -2.8650480e-01
  -7.5000000e-01  -5.0000000e-01   0.0000000e+00  -3.3281048e-01
  -5.0000000e-01  -5.0000000e-01   0.0000000e+00  -3.0326533e-01
  -2.5000000e-01  -5.0000000e-01   0.0000000e+00  -1.8290391e-01
   0.0000000e+00  -5.0000000e-01   0.0000000e+00   0.0000000e+00
   2.5000000e-01  -5.0000000e-01   0.0000000e+00   1.8290391e-01
   5.0000000e-01  -5.0000000e-01   0.0000000e+00   3.0326533e-01
   7.5000000e-01  -5.0000000e-01   0.0000000e+00   3.3281048e-01
   1.0000000e+00  -5.0000000e-01   0.0000000e+00   2.8650480e-01
  -1.0000000e+00  -2.5000000e-01   0.0000000e+00  -3.4559075e-01
  -7.5000000e-01  -2.5000000e-01   0.0000000e+00  -4.0144607e-01
  -5.0000000e-01  -2.5000000e-01   0.0000000e+00  -3.6580781e-01
  -2.5000000e-01  -2.5000000e-01   0.0000000e+00  -2.2062423e-01
   0.0000000e+00  -2.5000000e-01   0.0000000e+00   0.0000000e+00
   2.5000000e-01  -2.5000000e-01   0.0000000e+00   2.2062423e-01
   5.0000000e-01  -2.5000000e-01   0.0000000e+00   3.6580781e-01
   7.5000000e-01  -2.5000000e-01   0.0000000e+00   4.0144607e-01
   1.0000000e+00  -2.5000000e-01   0.0000000e+00   3.4559075e-01
  -1.0000000e+00   0.0000000e+00   0.0000000e+00  -3.6787944e-01
  -7.5000000e-01   0.0000000e+00   0.0000000e+00  -4.2733712e-01
  -5.0000000e-01   0.0000000e+00   0.0000000e+00  -3.8940039e-01
  -2.5000000e-01   0.0000000e+00   0.0000000e+00  -2.3485327e-01
   0.0000000e+00   0.0000000e+00   0.0000000e+00   0.0000000e+00
   2.5000000e-01   0.0000000e+00   0.0000000e+00   2.3485327e-01
   5.0000000e-01   0.0000000e+00   0.0000000e+00   3.8940039e-01
   7.5000000e-01   0.0000000e+00   0.0000000e+00   4.2733712e-01
   1.0000000e+00   0.0000000e+00   0.0000000e+00   3.6787944e-01
  -1.0000000e+00   2.5000000e-01   0.0000000e+00  -3.4559075e-01
  -7.5000000e-01   2.5000000e-01   0.0000000e+00  -4.0144607e-01
  -5.0000000e-01   2.5000000e-01   0.0000000e+00  -3.6580781e-01
  -2.5000000e-01   2.5000000e-01   0.0000000e+00  -2.2062423e-01
   0.0000000e+00   2.5000000e-01   0.0000000e+00   0.0000000e+00
   2.5000000e-01   2.5000000e-01   0.0000000e+00   2.2062423e-01
   5.0000000e-01   2.5000000e-01   0.0000000e+00   3.6580781e-01
   7.5000000e-01   2.5000000e-01   0.0000000e+00   4.0144607e-01
   1.0000000e+00   2.5000000e-01   0.0000000e+00   3.4559075e-01
  -1.0000000e+00   5.0000000e-01   0.0000000e+00  -2.8650480e-01
  -7.5000000e-01   5.0000000e-01   0.0000000e+00  -3.3281048e-01
  -5.0000000e-01   5.0000000e-01   0.0000000e+00  -3.0326533e-01
  -2.5000000e-01   5.0000000e-01   0.0000000e+00  -1.8290391e-01
   0.0000000e+00   5.0000000e-01   0.0000000e+00   0.0000000e+00
   2.5000000e-01   5.0000000e-01   0.0000000e+00   1.8290391e-01
   5.0000000e-01   5.0000000e-01   0.0000000e+00   3.0326533e-01
   7.5000000e-01   5.0000000e-01   0.0000000e+00   3.3281048e-01
   1.0000000e+00   5.0000000e-01   0.0000000e+00   2.8650480e-01
  -1.0000000e+00   7.5000000e-01   0.0000000e+00  -2.0961139e-01
  -7.5000000e-01   7.5000000e-01   0.0000000e+00  -2.4348935e-01
  -5.0000000e-01   7.5000000e-01   0.0000000e+00  -2.2187366e-01
  -2.5000000e-01   7.5000000e-01   0.0000000e+00  -1.3381536e-01
   0.0000000e+00   7.5000000e-01   0.0000000e+00   0.0000000e+00
   2.5000000e-01   7.5000000e-01   0.0000000e+00   1.3381536e-01
   5.0000000e-01   7.5000000e-01   0.0000000e+00   2.2187366e-01
   7.5000000e-01   7.5000000e-01   0.0000000e+00   2.4348935e-01
   1.0000000e+00   7.5000000e-01   0.0000000e+00   2.0961139e-01
  -1.0000000e+00   1.0000000e+00   0.0000000e+00  -1.3533528e-01
  -7.5000000e-01   1.0000000e+00   0.0000000e+00  -1.5720854e-01
  -5.0000000e-01   1.0000000e+00   0.0000000e+00  -1.4325240e-01
  -2.5000000e-01   1.0000000e+00   0.0000000e+00  -8.6397688e-02
   0.0000000e+00   1.0000000e+00   0.0000000e+00   0.0000000e+00
   2.5000000e-01   1.0000000e+00   0.0000000e+00   8.6397688e-02
   5.0000000e-01   1.0000000e+00   0.0000000e+00   1.4325240e-01
   7.5000000e-01   1.0000000e+00   0.0000000e+00   1.5720854e-01
   1.0000000e+00   1.0000000e+00   0.0000000e+00   1.3533528e-01
\end{filecontents*}

第一步,我尝试将 zplane 置于 z=0。但是,得到的图与我想要的图相差甚远。

在此处输入图片描述

以下是代码的相关部分:

\begin{document}

\begin{tikzpicture}
\begin{axis}[view={-60}{25}]

\addplot3[surf,
        mesh/cols=9,
        mesh/ordering=rowwise,
        shader=interp,
        ] 
table  [x index=0, 
        y index=1, 
        z index=3,
        z expr=0,
        ] {zslice.dat};

\end{axis}
\end{tikzpicture}

\end{document}

答案1

您需要使用explicit点元数据。然后您可以指定应该使用哪一列来存储元信息。此外,#文件开头的 导致了错误。最后,x diry dir选项在创建其他表面时可能会有所帮助。

代码

\documentclass[tikz,border=12pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{filecontents*}{zslice.dat}
  x                y               z              v
  ...
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[view={-60}{25}]

\addplot3
[   surf,
  mesh/cols=9,
  mesh/ordering=rowwise,
  shader=interp,
  point meta=explicit,
  x dir=reverse,
  y dir=reverse,
] table  [meta=v] {zslice.dat};
\end{axis}
\end{tikzpicture}

\end{document}

输出

在此处输入图片描述


编辑1:我决定稍微尝试一下。由于许多预定义的颜色图模拟了 MatLab 的颜色图,我猜你使用了 jet。对于其他平面,你唯一需要更改的是mash/orderingcolwise。此外,我了解到 是axis cs从 1.11 版开始的默认坐标系,因此可以轻松绘制黑色轮廓。

代码

\documentclass[tikz,border=12pt]{standalone}
\usepackage{filecontents}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[   view={-60}{25},
    colormap/jet,
    zmin=-1,
    zmax=0,
]
\addplot3
[   surf,
  mesh/cols=9,
  mesh/ordering=rowwise,
  shader=interp,
  point meta=explicit,
  x dir=reverse,
  y dir=reverse,
] table  [meta=v] {zslice.dat};
\addplot3
[   surf,
  mesh/cols=9,
  mesh/ordering=colwise,
  shader=interp,
  point meta=explicit,
  x dir=reverse,
  y dir=reverse,
] table  [meta=v] {yslice.dat};
\addplot3
[   surf,
  mesh/cols=9,
  mesh/ordering=colwise,
  shader=interp,
  point meta=explicit,
  x dir=reverse,
  y dir=reverse,
] table  [meta=v] {xslice.dat};
\draw (-1,-1,-1) -- (1,-1,-1) -- (1,-1,0) -- (-1,-1,0) -- cycle;
\draw (-1,-1,-1) -- (-1,1,-1) -- (-1,1,0) -- (-1,-1,0) -- cycle;
\draw (-1,-1,0) -- (-1,1,0) -- (1,1,0) -- (1,-1,0) -- cycle;
\end{axis}

\end{tikzpicture}

\end{document}

输出

在此处输入图片描述

相关内容