我试图在 PGFPlots 中尽可能接近地重现该图形(原始图形是用 gnuplot 和 Illustrator 组合制作的):
3D 表面是f(x,y):=x^2 y^2
。
我对 PGFPlots 的 3D 绘图有点困惑。特别是,我无法实现以下功能:
x=0,0.2,0.4,...,1
在和 处绘制带有网格线的 3D 曲面y=0,0.2,0.4,...,1
。x=0,0.2,0.4,...,1
y=0,0.2,0.4,...,1
在图的‘地板’内绘制网格。在图中获得与上图类似的正交样式视角。
画出平面
y=0.8
,并在该平面内画出曲线f(x,0.8)=0.64x^2
,如上图所示。
有谁能给出一些关于如何实现这一目标的指点吗?
更新:根据@percusse 的建议,我得到了下图。因此,主要未解决的问题是如何控制表面上的网格线以获得所需的效果。
\begin{tikzpicture} \begin{axis}[view={40}{40},xmajorgrids,ymajorgrids,xtick={0,0.2,0.4,0.6,0.8,1},ytick={0,0.2,0.4,0.6,0.8,1},zmin=0,zmax=1]
\addplot3[mesh,draw=black,domain=0:1,domain y=0.8:1] {x^2*y^2};
\addplot3[surf,mesh/rows=2,fill=blue,opacity=0.4] coordinates {
(0,0.8,0) (1,0.8,0)
(0,0.8,1) (1,0.8,1)
};
\addplot3[mesh,draw=black,domain=0:1,domain y=0:0.8] {x^2*y^2};
\addplot3[domain=0:1,scatter,mesh,draw=blue,thick,no markers] (x,0.8,{0.64*x^2});
\end{axis}\end{tikzpicture}
答案1
我现在实现了如下解决方案:
这是基于信息来自 Christian Feuersänger 的这个回答. PGFPlots 中似乎没有用于控制表面图上网格线间距的机制。
patch
解决此问题的方法是按照上述链接答案中描述的方式将表面绘制为。面片接受refines
控制网格中子划分数量的选项。不幸的是,似乎不可能获得偶数个内部网格线,因为refines
通过将网格上每个现有象限划分为四分之一来工作,总是导致奇数个网格线。
我对此的(乏味的)解决方案是将网格的每个象限绘制为其自己的面片(因此上图中有 25 个面片)。设置refines=0
结果导致每个面片没有内部网格(仅绘制面片边界),因此当面片相邻放置时,外观呈现为连续表面,网格的线条与其组成面片的边缘重合。
对于我的特定应用,这种方法还有一个额外的优势。y 值在 0.8 到 1.0 之间的五个面片可以在蓝色平面之前绘制,其他二十个面片可以在蓝色平面之后绘制 — 确保平面遮挡住表面的正确部分。
然而,这种方法很繁琐,因此欢迎提出更为简约的建议。
下面是代码的精简版,演示了如何绘制其中一个补丁:
\usepgfplotslibrary{patchplots}
\begin{tikzpicture} \begin{axis}[view={40}{40},xmin=0,xmax=1,ymin=0,max=1,zmin=0,zmax=1]
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.8 0.8
1. 0.8
1. 1.
0.8 1.
0.9 0.8
1. 0.9
0.9 1.
0.8 0.9
0.9 0.9
};
\end{axis}\end{tikzpicture}
结果如下:
完整图形的最终代码是:
\usepgfplotslibrary{patchplots}
\begin{tikzpicture} \begin{axis}[view={40}{40},xmajorgrids,ymajorgrids,xtick={0,0.2,0.4,0.6,0.8,1},ytick={0,0.2,0.4,0.6,0.8,1},ztick={0,0.5,1},zmin=0,zmax=1,scale=1.7,xlabel={$x$},ylabel={$y$},zlabel={$f(x,y)$},grid style ={black!10}]
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0. 0.8
0.2 0.8
0.2 1.
0. 1.
0.1 0.8
0.2 0.9
0.1 1.
0. 0.9
0.1 0.9
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.2 0.8
0.4 0.8
0.4 1.
0.2 1.
0.3 0.8
0.4 0.9
0.3 1.
0.2 0.9
0.3 0.9
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.4 0.8
0.6 0.8
0.6 1.
0.4 1.
0.5 0.8
0.6 0.9
0.5 1.
0.4 0.9
0.5 0.9
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.6 0.8
0.8 0.8
0.8 1.
0.6 1.
0.7 0.8
0.8 0.9
0.7 1.
0.6 0.9
0.7 0.9
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.8 0.8
1. 0.8
1. 1.
0.8 1.
0.9 0.8
1. 0.9
0.9 1.
0.8 0.9
0.9 0.9
};
%%%%%%%%%%
\addplot3[surf, color=blue, opacity=1,fill opacity=0.4, domain=-2:2, faceted color=blue] coordinates {
(0,0.8,0) (1,0.8,0)
(0,0.8,1) (1,0.8,1)
};
%%%%%%%%%%
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic]
table[z expr=x^2*y^2]
{
x y
0 0
0.2 0
0.2 0.2
0 0.2
0.1 0
0.2 0.1
0.1 0.2
0 0.1
0.1 0.1
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.2 0
0.4 0
0.4 0.2
0.2 0.2
0.3 0
0.4 0.1
0.3 0.2
0.2 0.1
0.3 0.1
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.4 0
0.6 0
0.6 0.2
0.4 0.2
0.5 0
0.6 0.1
0.5 0.2
0.4 0.1
0.5 0.1
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.6 0
0.8 0
0.8 0.2
0.6 0.2
0.7 0
0.8 0.1
0.7 0.2
0.6 0.1
0.7 0.1
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.8 0
1 0
1 0.2
0.8 0.2
0.9 0
1 0.1
0.9 0.2
0.8 0.1
0.9 0.1
};
%%
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0. 0.2
0.2 0.2
0.2 0.4
0. 0.4
0.1 0.2
0.2 0.3
0.1 0.4
0. 0.3
0.1 0.3
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.2 0.2
0.4 0.2
0.4 0.4
0.2 0.4
0.3 0.2
0.4 0.3
0.3 0.4
0.2 0.3
0.3 0.3
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.4 0.2
0.6 0.2
0.6 0.4
0.4 0.4
0.5 0.2
0.6 0.3
0.5 0.4
0.4 0.3
0.5 0.3
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.6 0.2
0.8 0.2
0.8 0.4
0.6 0.4
0.7 0.2
0.8 0.3
0.7 0.4
0.6 0.3
0.7 0.3
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.8 0.2
1 0.2
1 0.4
0.8 0.4
0.9 0.2
1 0.3
0.9 0.4
0.8 0.3
0.9 0.3
};
%%%
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0. 0.4
0.2 0.4
0.2 0.6
0. 0.6
0.1 0.4
0.2 0.5
0.1 0.6
0. 0.5
0.1 0.5
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.2 0.4
0.4 0.4
0.4 0.6
0.2 0.6
0.3 0.4
0.4 0.5
0.3 0.6
0.2 0.5
0.3 0.5
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.4 0.4
0.6 0.4
0.6 0.6
0.4 0.6
0.5 0.4
0.6 0.5
0.5 0.6
0.4 0.5
0.5 0.5
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.6 0.4
0.8 0.4
0.8 0.6
0.6 0.6
0.7 0.4
0.8 0.5
0.7 0.6
0.6 0.5
0.7 0.5
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.8 0.4
1 0.4
1 0.6
0.8 0.6
0.9 0.4
1 0.5
0.9 0.6
0.8 0.5
0.9 0.5
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0. 0.6
0.2 0.6
0.2 0.8
0. 0.8
0.1 0.6
0.2 0.7
0.1 0.8
0. 0.7
0.1 0.7
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.2 0.6
0.4 0.6
0.4 0.8
0.2 0.8
0.3 0.6
0.4 0.7
0.3 0.8
0.2 0.7
0.3 0.7
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.4 0.6
0.6 0.6
0.6 0.8
0.4 0.8
0.5 0.6
0.6 0.7
0.5 0.8
0.4 0.7
0.5 0.7
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.6 0.6
0.8 0.6
0.8 0.8
0.6 0.8
0.7 0.6
0.8 0.7
0.7 0.8
0.6 0.7
0.7 0.7
};
\addplot3[patch,patch refines=0,mesh,black,patch type=biquadratic] table[z expr=x^2*y^2] {
x y
0.8 0.6
1 0.6
1 0.8
0.8 0.8
0.9 0.6
1 0.7
0.9 0.8
0.8 0.7
0.9 0.7
};
\addplot3[domain=0:1,scatter,mesh,draw=blue,very thick,no markers] (x,0.8,{0.64*x^2});
\end{axis}\end{tikzpicture}