如何改变 pgfplots 3D 曲面图以使其看起来更好

如何改变 pgfplots 3D 曲面图以使其看起来更好

我想使用 TikZ/pgf 制作以下 3D 图形,它包括:

  • 当 (x, y) != (0,0) 且 f(0,0)=0 时,函数 f(x, y) = xy/(x^2 + y^2) 的图;
  • 平面 y = x 与表面的交线,即方程 y = x, z = 1/2 的直线,但省略了点 (0, 0, 1/2);
  • 起源;以及
  • 至少是 x、y 和 z 轴的正部分。

Mathematica 图

该图形是使用 Mathematica 创建的,并使用 (r,θ, φ) 球面坐标(其中角度以弧度而不是度数为单位)中的视点 (2.85216, 1.62152, 0.828166)。

pgfplots尝试使用下面的代码并生成之后显示的图形。

问题:我怎样才能修改pgfplots代码,以便与 Mathematica 图形非常相似,以便它:

  1. 使用基本相同的视点(因此轴的方向也相同);

  2. 省略表面上的轮廓规则;

  3. 在 y = x, z = 1/2 线上的 z 轴处有断点;

  4. 使用更有说服力的原点;并且

  5. 避免 z 轴附近表面的“锯齿状”。

关于 5.,我确实尝试增加该samples值,但这样做会导致TeX capacity exceeded错误!

我的pgfplot输出:

普格夫

我的代码:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

% Define a grayscale colormap
\pgfplotsset{
    colormap={grayscale}{[1pt] rgb255(0pt)=(0,0,0); rgb255(1000pt)=(255,255,255)}
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    view={75.833}{35.3489},
    axis lines=center, 
    xlabel={$x$}, ylabel={$y$}, zlabel={$z$},
    ticks=none, 
    domain=-1:1, y domain=-1:1,
    samples=50, % need to avoid "jaggies"
    z buffer=sort,
    clip=false,
    xmin=-1, xmax=1, ymin=-1, ymax=1, zmin=-1, zmax=1.5, 
    colormap name=grayscale, 
    xlabel style={anchor=north west}, ylabel style={anchor=north west},
    zlabel style={anchor=south},
    ]
    % Surface plot
    \addplot3[surf, shader=faceted interp, opacity=0.7] 
        {x != 0 || y != 0 ? (x*y)/(x^2 + y^2) : 0};
    % Point at the origin
    \addplot3[mark=*, mark size=1,mark options={color=black}] coordinates {(0, 0, 0)};
    % Curve of intersection of plane and surface
    \addplot3[samples=20, samples y=0, thick, color=black] 
        ({x}, {x}, {1/2});
\end{axis}
\end{tikzpicture}
\end{document}

答案1

更新

在此处输入图片描述

更新

我根据 @murray 的众多评论修改了代码。有两种表示表面的方法:要么使用极坐标作为定义域,要么使用法线坐标。前者理想地处理原点处的奇点,因为它尊重它。后者坚持函数的初始定义,但难以处理其在 (0, 0) 附近的行为。

对于后者,相对于初始答案的主要修改如下:

  • 表面被分成两部分(y<0y > 0分别)
  • 为了更好地理解表面,添加了边框
  • 轴是单独绘制的(作为 TikZ 段)。

各种图形元素的顺序很重要。

评论 下面是使用基于 10000x10000 网格的计算获得的图像matplotlib。从后者的角度来看,表面永远不可能在原点周围平滑。

在此处输入图片描述

使用极坐标作为域的绘图新代码

\documentclass[11pt, margin=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{math}
  
\pgfplotsset{compat=1.17}
\begin{document}

 \pgfplotsset{
    colormap={cmpgray}{rgb255=(221,221,221) rgb255=(54,54,54)}
} 
\xdefinecolor{axisRGB}{RGB}{128, 30, 0}  % {128, 128, 145}
\begin{tikzpicture}
  \begin{axis}[
    data cs=polar,
    axis lines=none,  % grid=major,
    view={110}{22},
    z buffer=sort,
    clip=false]
    
    % negative Ox axis 
    \draw[axisRGB, thin] (0, 0, 0) -- (-1.8, 0, 0);
    \draw[axisRGB, thin, ->] (0, 0, .02) -- (0, 0, .8)
    node[right, text=black, scale=.7] {$z$};

    \addplot3[
    surf,
    shader=interp,
    domain=0:360, domain y=.02:1.4,
    samples=50, samples y=20,
    opacity=0.95]
    {.5*sin(2*x)};
    
    % negative Oy axis 
    \draw[axisRGB, thin] (0, 0, 0) -- (0, -1.8, 0);
    % negative Oz axis 
    \draw[axisRGB, thin] (0, 0, -.025) -- (0, 0, -.8);
    
    % point at the origin
    \fill[opacity=.7] (0, 0, 0) circle (1.2pt);

    \draw[axisRGB, thin, ->] (0, .02, 0, 0) -- (0, 1.8, 0)
    node[below, text=black, scale=.7] {$y$};
    \draw[axisRGB, thin, ->] (.02, 0, 0) -- (1.8, 0, 0)
    node[below, text=black, scale=.7] {$x$};

    % Intersection curve of surface and plane z=1/2
    \draw[thin] (-1, -1, 1/2) -- (1, 1, 1/2);
  \end{axis}
\end{tikzpicture}
\end{document}

第二幅图的新代码

\documentclass[11pt, margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
 \pgfplotsset{
    colormap={cmpgray}{rgb255=(221,221,221) rgb255=(54,54,54)}
} 
\xdefinecolor{axisRGB}{RGB}{128, 128, 145}

\begin{tikzpicture}
  \begin{axis}[
    view={115}{19},
    axis lines=none,  % center, 
    xlabel={$x$}, ylabel={$y$}, zlabel={$z$},
    ticks=none, 
    z buffer=sort,
    clip=false,
    xmin=-1.3, xmax=1.3,
    ymin=-1.3, ymax=1.3,
    zmin=-1, zmax=1.3, 
    xlabel style={anchor=north west, scale=.8},
    ylabel style={anchor=north west, scale=.8},
    zlabel style={anchor=south, scale=.8},
    ]
    
    % Surface y<0
    \addplot3[
    surf,
    domain=-1:1,
    y domain=-1:-.005,
    samples=55, 
    colormap name=cmpgray,
    shader=interp,  % flat, faceted interp,
    opacity=0.75]
    {x*y/(x^2 + y^2)};

    % Surface y<0 's border
    \addplot3[%
    draw=black, ultra thin,
    domain=-1:1,
    samples y=0]
    (x, -1, {-x/(x*x +1)});
    \addplot3[%
    draw=black, ultra thin,
    domain=-1:1,
    samples y=0]
    (-1, x, {-x/(x*x +1)});

    % negative Ox and Oy axes
    \draw[axisRGB, thin] (0, 0, 0) -- (0, -1.4, 0);
    \draw[axisRGB, thin] (0, 0, 0) -- (-1.4, 0, 0);

    % Point at the origin
    \fill (0, 0, 0) circle (1.2pt);

    % positive Oz axis 
    \draw[axisRGB, thin, ->] (0, 0, .02) -- (0, 0, 1.3)
    node[right, text=black, scale=.7] {$z$};
    
    % Surface y>0
    \addplot3[
    surf,
    domain=-1:1,
    y domain=.005:1,
    samples=55, 
    colormap name=cmpgray, 
    shader=interp,  % flat, faceted interp,
    opacity=0.75]
    {x*y/(x*x + y*y)};

    % positive Oy axis 
    \draw[axisRGB, thin, ->] (0, .02, 0, 0) -- (0, 1.4, 0)
    node[below, text=black, scale=.7] {$y$};

    % negative Oz axis 
    \draw[axisRGB, thin] (0, 0, -.025) -- (0, 0, -1.3);

    % Intersection curve of surface and plane z=1/2
    \draw[thin] (-1, -1, 1/2) -- (1, 1, 1/2);

    % Surface y>0 's border
    \addplot3[%
    draw=black, very thin,
    domain=-1:1,
    samples y=0]
    (x, 1, {x/(x*x +1)});
    \addplot3[%
    draw=black, very thin,
    domain=-1:1,
    samples y=0]
    (1, x, {x/(x*x +1)});

    % positive Ox axis 
    \draw[axisRGB, thin, ->] (.02, 0, 0) -- (1.5, 0, 0)
    node[below, text=black, scale=.7] {$x$};
  \end{axis}
\end{tikzpicture}
\end{document}

旧答案

在此处输入图片描述

像这样;我只改变了视角、坐标轴的长度和着色器。

代码

\documentclass[11pt, margin=10pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}

\pgfplotsset{compat=1.17}
\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    view={115}{15},
    axis lines=center, 
    xlabel={$x$}, ylabel={$y$}, zlabel={$z$},
    ticks=none, 
    domain=-1:1, y domain=-1:1,
    samples=50, % need to avoid "jaggies"
    z buffer=sort,
    clip=false,
    xmin=-1.3, xmax=1.3,
    ymin=-1.3, ymax=1.3,
    zmin=-1, zmax=1.3, 
    xlabel style={anchor=north west, scale=.8},
    ylabel style={anchor=north west, scale=.8},
    zlabel style={anchor=south, scale=.8},
    ]
    % Surface plot
    \addplot3[
    surf,
    colormap/Blues,  % cool,
    % shader=faceted interp,
    opacity=0.3] 
    {x != 0 || y != 0 ? (x*y)/(x^2 + y^2) : 0};
    
    % Point at the origin
    \addplot3[mark=*, mark size=1,mark options={color=black}]
    coordinates {(0, 0, 0)};
    
    % Curve of intersection of plane and surface
    \addplot3[samples=20, samples y=0, thick, color=black] 
    ({x}, {x}, {1/2});
  \end{axis}
\end{tikzpicture}
\end{document}

相关内容