将 3D 图拟合到形状上

将 3D 图拟合到形状上

我希望根据绘制矩形所用的坐标系来指定 3D 绘图。目前,3D 绘图占用了太多空间,并且偏离了图片。我希望它包含在矩形内。

\documentclass[border=10pt,varwidth]{standalone}
\usepackage{tikz,tikz-3dplot}
\usepackage{pgfplots}

\begin{document}


\tdplotsetmaincoords{70}{155}
\begin{tikzpicture} [scale=3, tdplot_main_coords, axis/.style={->,blue,thick},
vector/.style={-stealth,black,very thick},
vector guide/.style={dotted,black,thick},
]


\draw[thick,tdplot_main_coords] (1.5,0.5,0)-- (1.5,-0.5,0) -- (-1.5,-0.5,0)--(-1.5,0.5,0)--cycle;

\begin{axis}[hide axis, view={55}{45}]
    \addplot3[surf, colormap/hot2, samples=41, domain=0:1] {sin(300*x)*sin(300*y)};
\end{axis}

\end{tikzpicture}
\end{document}

答案1

您可以直接在环境内绘制矩形axis(即,与您要求的相反)。使用\pgfplotsset{compat=1.12},以便您可以使用直接坐标(0,0,-1)(否则,您需要(axis cs: 0,0,-1))。

以下是代码:

\documentclass[border=10pt]{standalone}
\usepackage{tikz,tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}   %% <<--- use this

\begin{document}


\tdplotsetmaincoords{70}{155}
\begin{tikzpicture} [scale=3, tdplot_main_coords, axis/.style={->,blue,thick},
vector/.style={-stealth,black,very thick},
vector guide/.style={dotted,black,thick},
]
\begin{axis}[hide axis,view={55}{45}]
    \draw[thick] (0,0,-1) -- (0,1,-1) -- (1,1,-1)-- (1,0,-1) -- cycle;  %% <<-- draw it
    \addplot3[surf, colormap/hot2, samples=41, domain=0:1] {sin(300*x)*sin(300*y)};
\end{axis}

\end{tikzpicture}
\end{document}

在此处输入图片描述

从 BTW 中删除varwidth选项\documentclass

编辑

您也可以绘制矩形。选择适当的坐标,如下所示

\addplot3[thick] coordinates{(-1,-1,-1)  (-1,1,-1)  (1,1,-1) (1,-1,-1)}--cycle;  %% <<-- plot it

完整代码如下:

\documentclass[border=10pt]{standalone}
\usepackage{tikz,tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}   %% <<--- use this

\begin{document}


\tdplotsetmaincoords{70}{155}
\begin{tikzpicture} [scale=3, tdplot_main_coords, axis/.style={->,blue,thick},
vector/.style={-stealth,black,very thick},
vector guide/.style={dotted,black,thick},
]
\begin{axis}[hide axis,view={55}{45}]
    \addplot3[thick] coordinates{(-1,-1,-1)  (-1,1,-1)  (1,1,-1) (1,-1,-1)}--cycle;  %% <<-- plot it
    \addplot3[surf, colormap/hot2, samples=41, domain=0:1] {sin(300*x)*sin(300*y)};
    %\addplot3[surf, colormap/hot2, samples=41, domain=-0.5:0.5,y domain=-1:1] {sin(300*x)*sin(300*y)};
\end{axis}

\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容