我想使用 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 創建的,並使用 (r,θ, φ) 球面座標中的視點 (2.85216, 1.62152, 0.828166)(其中角度以弧度為單位,而不是度數)。
我的pgfplots
嘗試使用下面的程式碼並產生之後顯示的圖形。
問題:如何更改pgfplots
程式碼以使其與 Mathematica 圖形非常相似,以便:
使用本質上相同的視點(因此具有相同的軸方向);
省略表面上的輪廓規則;
在 y = x, z = 1/2 線上的 z 軸處有斷點;
在原點使用更有說服力的觀點;和
避免 z 軸附近表面出現「鋸齒」。
Re 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<0和y>0分別)
- 添加邊框是為了更好地理解表面
- 軸是單獨繪製的(作為 TikZ 段)。
各種圖形元素的順序很重要。
評論
以下是使用matplotlib
基於 10000x10000 網格的計算所獲得的圖像。使用後一種觀點,表面永遠不能在原點周圍平滑。
使用域極座標進行繪圖的新程式碼
\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}