将曲面图与矢量图结合时出现不需要的空白

将曲面图与矢量图结合时出现不需要的空白

我正在尝试使用 pgfplot/tikz 生成标量场的表面图,并在其上附加矢量图。我几乎已经完成了,只是根据箭头的比例,在轴和表面图之间添加了空白。如何避免这种情况?

比例=0.01 比例=0.05

我正在使用的代码:

\documentclass{article}

% Setting graphing environment
\usepackage{pgfplots}
\pgfplotsset{compat=newest} 
\pgfplotsset{plot coordinates/math parser=false}

\begin{document}

% declare pgf colormaps:
\usepgfplotslibrary{colormaps} 

% declare constants:
\pgfmathsetmacro\kx{2*pi}
\pgfmathsetmacro\ky{2*pi}
\pgfmathsetmacro\k{sqrt(\kx^2+\ky^2)}

% declare equations:
\pgfmathdeclarefunction{rho}{2}{\pgfmathparse{% density
    (\ky/\k)^2*cos(deg(2*\kx*#1))+(\kx/\k)^2*cos(deg(2*\ky*#2))}}
\pgfmathdeclarefunction{ux}{2}{\pgfmathparse{% x-velocity
    -sqrt(2)*(\ky/\k)*cos(deg(\kx*#1))*sin(deg(\ky*#2))}}
\pgfmathdeclarefunction{uy}{2}{\pgfmathparse{% y-velocity
    +sqrt(2)*(\kx/\k)*sin(deg(\kx*#1))*cos(deg(\ky*#2))}}
\pgfmathdeclarefunction{umag}{2}{\pgfmathparse{% velocity magnitude
    sqrt(ux(#1,#2)^2+uy(#1,#2)^2)}}

\begin{tikzpicture}
    \begin{axis} [
            domain=0:1, view={0}{90},
            axis background/.style={fill=white},
            colormap/jet, colorbar 
        ]
        \addplot3 [
                surf, shader=interp, samples=10             ]
            { rho(x,y) };

        \addplot3 [
                white, -stealth, samples=20,
                quiver={
                    u={ux(x,y)/umag(x,y)}, 
                    v={uy(x,y)/umag(x,y)}, 
                    w={0}, 
                    scale arrows=0.01 
                },
            ]
            {umag(x,y)};

    \end{axis}
\end{tikzpicture}
\end{document}

答案1

箭头超出了绘图区域,使其变大。要修复此问题,请使用 、 、 和 明确xmin限制xmax绘图ymin窗口ymax

\documentclass{standalone}

% Setting graphing environment
\usepackage{pgfplots}
\pgfplotsset{compat=newest} 
\pgfplotsset{plot coordinates/math parser=false}

\begin{document}

% declare pgf colormaps:
\usepgfplotslibrary{colormaps} 

% declare constants:
\pgfmathsetmacro\kx{2*pi}
\pgfmathsetmacro\ky{2*pi}
\pgfmathsetmacro\k{sqrt(\kx^2+\ky^2)}

% declare *functions*: <-- Matthew with his mathematician hat on
\pgfmathdeclarefunction{rho}{2}{\pgfmathparse{% density
    (\ky/\k)^2*cos(deg(2*\kx*#1))+(\kx/\k)^2*cos(deg(2*\ky*#2))}}
\pgfmathdeclarefunction{ux}{2}{\pgfmathparse{% x-velocity
    -sqrt(2)*(\ky/\k)*cos(deg(\kx*#1))*sin(deg(\ky*#2))}}
\pgfmathdeclarefunction{uy}{2}{\pgfmathparse{% y-velocity
    +sqrt(2)*(\kx/\k)*sin(deg(\kx*#1))*cos(deg(\ky*#2))}}
\pgfmathdeclarefunction{umag}{2}{\pgfmathparse{% velocity magnitude
    sqrt(ux(#1,#2)^2+uy(#1,#2)^2)}}

\begin{tikzpicture}
    \begin{axis} [
            domain=0:1, view={0}{90},
            axis background/.style={fill=white},
            colormap/jet, colorbar,
            xmin=0,xmax=1,
            ymin=0,ymax=1 
        ]
        \addplot3 [
                surf, shader=interp, samples=10             ]
            { rho(x,y) };

        \addplot3 [
                white, -stealth, samples=20,
                quiver={
                    u={ux(x,y)/umag(x,y)}, 
                    v={uy(x,y)/umag(x,y)}, 
                    w={0}, 
                    scale arrows=0.1 
                },
            ]
            {umag(x,y)};

    \end{axis}
\end{tikzpicture}
\end{document}

示例代码输出

相关内容