我正在尝试绘制一个带有决策边界的 3D 散点图,并尝试将青色平面放在蓝点上方,但它们最终却位于顶部,我尝试修改不透明度并按顺序排列图表,但没有成功。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=0.25in]{geometry}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\pgfplotsset{width=10cm,compat=newest}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=7,ymin=-2,ymax=6,zmin=-3,zmax=9,axis background/.style={fill=gray!10},view={13}{5},xlabel=$x$,
ylabel=$y$,
zlabel=$x^2+y^2$, z label style={rotate=-90},
,scatter/classes={
a={mark=*,color=blue},b={mark=*,color=red}}]
%Red scatter
\addplot3+[
only marks,scatter src =explicit symbolic,
scatter,mark size=2.9pt]
coordinates{
(5.4,5.45,5.952500000000001) [b]
(4.7,5.4,3.8900000000000006) [b]
(4.3,5.8,4.769999999999998) [b]
(3.6,5.9,4.8500000000000005) [b]
(3,5.77,4.774899999999998) [b]
(6,4.7,6.289999999999999) [b]
(6,4.2,5.539999999999999) [b]
(1.6,4.7,5.41) [b]
(5,4.5,2.329999999999999) [b]
(2,5.4,5.780000000000001) [b]
(1,3.5,7.330000000000001) [b]
(1.5,2.5,6.280000000000001) [b]
(2.1,2.8,3.370000000000001) [b]
(2.1,2.8,3.370000000000001) [b]
(6.4,3.8,7.300000000000001) [b]
(1.2,4.5,6.89) [b]
(3.67,1.47,4.9738000000000016) [b]
(6,2.5,6.7299999999999995) [b]
(6.2,3,6.74) [b]
(5,2.55,3.0125) [b]
(4.89,1.7,5.416099999999998) [b]
(2.8,1.5,5.650000000000001) [b]
(2.5,2.3,3.4000000000000012) [b]
(2,2.071,5.543641000000001) [b]
(4,2,2.9800000000000004) [b]
(3.5,2.5,2.4800000000000004) [b]
(3,2,3.380000000000001) [b]
(4,5.3, 2.6499999999999986) [b]
(4.4,2.3,2.4500000000000015) [b]
(2.23,5.001,3.8535010000000014) [b]
(2.5,2.8,2.250000000000001) [b]
(1.5,3,5.330000000000001) [b]
(1.5,4,4.930000000000001) [b]
(1.77,3.47,3.777800000000001) [b]
(5.2,4.9,3.6900000000000004) [b]
(3.45,5,1.7524999999999995) [b]
(2,4.2,3.1400000000000006) [b]
(2.6,5.3,3.7699999999999987) [b]
(2.4,4.6,2.4999999999999996) [b]
(3.24,5.3,2.7715999999999985) [b]
(5.3,3,3.049999999999999) [b]
(5.3,3.5,2.5999999999999988) [b]
(5.7,3.8,4.01) [b]
(6,3.5,5.329999999999999) [b]
(5.7,2.7,5) [b]
(5.4,4.3,3.25) [b]
(4.5,5,2.329999999999999) [b]};
%blue scatter
\addplot3+[
only marks,scatter src =explicit symbolic,
scatter,mark size=2.9pt]
coordinates{
(3.17,3,0.7709000000000006) [a]
(2.9,3.27,0.8249000000000006) [a]
(2.8,3.63,0.8149000000000006) [a]
(2.9,4,0.7300000000000003) [a]
(3.2,4.1,0.4099999999999996) [a]
(3.6,4.47,0.6028999999999993) [a]
(4,4.4,0.5800000000000002) [a]
(4.3,4.2,0.6099999999999997) [a]
(4.2,3.4,0.3400000000000002) [a]
(4.5,3.8,0.6499999999999996) [a]
(4,3,0.5800000000000002) [a]
(4,3.7,0.0899999999999999) [a]
(3.7,4.1,0.1599999999999996) [a]
(4.5,3.2,0.8899999999999997) [a]
(3.5,3.5,0.08000000000000014) [a]
(3.7,3.3,0.16000000000000028) [a]
(3.7,3.7,0) [a]
(3.1,3.7,0.3600000000000001) [a]
(3.1,3.4,0.4500000000000003) [a]
(3.5,3.2,0.2900000000000001) [a]
(4.2,3.9,0.2899999999999999) [a]
(3.2,4.37,0.6988999999999999) [a]};
%Boundary
\addplot3[samples=100,domain=1:6.5, domain y =-2:6, color=cyan, fill opacity=0.2](x,y,{1.3});
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
如果你重新排序这些图,那么最隐藏的图会排在最前面,然后clip mode=individual
你就会得到
为什么clip mode=individual
?我不确定我是否理解了这一点,但它是在上下文中提到的mark layer
,我原本以为这是相关的关键。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=0.25in]{geometry}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{positioning}
\pgfplotsset{width=10cm,compat=newest}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=7,ymin=-2,ymax=6,zmin=-3,zmax=9,axis background/.style={fill=gray!10},view={13}{5},xlabel=$x$,
ylabel=$y$,
zlabel=$x^2+y^2$, z label style={rotate=-90},
,scatter/classes={
a={mark=*,color=blue},b={mark=*,color=red}},
clip mode=individual]
%blue scatter
\addplot3+[
only marks,scatter src =explicit symbolic,
scatter,mark size=2.9pt]
coordinates{
(3.17,3,0.7709000000000006) [a]
(2.9,3.27,0.8249000000000006) [a]
(2.8,3.63,0.8149000000000006) [a]
(2.9,4,0.7300000000000003) [a]
(3.2,4.1,0.4099999999999996) [a]
(3.6,4.47,0.6028999999999993) [a]
(4,4.4,0.5800000000000002) [a]
(4.3,4.2,0.6099999999999997) [a]
(4.2,3.4,0.3400000000000002) [a]
(4.5,3.8,0.6499999999999996) [a]
(4,3,0.5800000000000002) [a]
(4,3.7,0.0899999999999999) [a]
(3.7,4.1,0.1599999999999996) [a]
(4.5,3.2,0.8899999999999997) [a]
(3.5,3.5,0.08000000000000014) [a]
(3.7,3.3,0.16000000000000028) [a]
(3.7,3.7,0) [a]
(3.1,3.7,0.3600000000000001) [a]
(3.1,3.4,0.4500000000000003) [a]
(3.5,3.2,0.2900000000000001) [a]
(4.2,3.9,0.2899999999999999) [a]
(3.2,4.37,0.6988999999999999) [a]};
%Boundary
\addplot3[samples=100,
domain=1:6.5, domain y =-2:6, color=cyan,opacity=0.5](x,y,{1.3});
%Red scatter
\addplot3+[
only marks,scatter src =explicit symbolic,
scatter,mark size=2.9pt]
coordinates{
(5.4,5.45,5.952500000000001) [b]
(4.7,5.4,3.8900000000000006) [b]
(4.3,5.8,4.769999999999998) [b]
(3.6,5.9,4.8500000000000005) [b]
(3,5.77,4.774899999999998) [b]
(6,4.7,6.289999999999999) [b]
(6,4.2,5.539999999999999) [b]
(1.6,4.7,5.41) [b]
(5,4.5,2.329999999999999) [b]
(2,5.4,5.780000000000001) [b]
(1,3.5,7.330000000000001) [b]
(1.5,2.5,6.280000000000001) [b]
(2.1,2.8,3.370000000000001) [b]
(2.1,2.8,3.370000000000001) [b]
(6.4,3.8,7.300000000000001) [b]
(1.2,4.5,6.89) [b]
(3.67,1.47,4.9738000000000016) [b]
(6,2.5,6.7299999999999995) [b]
(6.2,3,6.74) [b]
(5,2.55,3.0125) [b]
(4.89,1.7,5.416099999999998) [b]
(2.8,1.5,5.650000000000001) [b]
(2.5,2.3,3.4000000000000012) [b]
(2,2.071,5.543641000000001) [b]
(4,2,2.9800000000000004) [b]
(3.5,2.5,2.4800000000000004) [b]
(3,2,3.380000000000001) [b]
(4,5.3, 2.6499999999999986) [b]
(4.4,2.3,2.4500000000000015) [b]
(2.23,5.001,3.8535010000000014) [b]
(2.5,2.8,2.250000000000001) [b]
(1.5,3,5.330000000000001) [b]
(1.5,4,4.930000000000001) [b]
(1.77,3.47,3.777800000000001) [b]
(5.2,4.9,3.6900000000000004) [b]
(3.45,5,1.7524999999999995) [b]
(2,4.2,3.1400000000000006) [b]
(2.6,5.3,3.7699999999999987) [b]
(2.4,4.6,2.4999999999999996) [b]
(3.24,5.3,2.7715999999999985) [b]
(5.3,3,3.049999999999999) [b]
(5.3,3.5,2.5999999999999988) [b]
(5.7,3.8,4.01) [b]
(6,3.5,5.329999999999999) [b]
(5.7,2.7,5) [b]
(5.4,4.3,3.25) [b]
(4.5,5,2.329999999999999) [b]};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}