问题
contour lua
我正在尝试通过设置 来更改轮廓图 ( ) 的颜色point meta
。但是,我不想使用图中的信息,而是想使用另一个函数来为它们着色。在我的例子中,我使用一条线来定义颜色,这样一边应该是一种颜色,另一边应该是另一种颜色。
但是,当我设置时,point meta={y>discriminant?.5:-.5}
轮廓图被破坏了。我通过强制颜色的阶跃函数来解决这个问题,但在中间出现了一些错误,因为它最终没有定义(参见白色轮廓)。
问题
- 为什么当我设置时轮廓图被破坏了
point meta
?那不应该只改变颜色吗? - 有没有更好的方法来使用函数(如本
surf
例所示)来为轮廓图着色,而不是改变colormap
?(见下面的例子)。
平均能量损失
高斯数据的原始问题
% !TeX program=lualatex
\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
compat=1.18,
colormap={bluewhiteorange}{color(0cm)=(NavyBlue); color(0.5cm)=(white); color(1cm)=(BurntOrange)},
colormap={stepblueorange}{color(0cm)=(NavyBlue); color(.4999cm)=(NavyBlue); color(0.5cm)=(white); color(0.5001cm)=(BurntOrange); color(1cm)=(BurntOrange)},
}
\begin{tikzpicture}[
declare function={
mu11=.2;
mu12=.25;
sigma11=.35;
sigma12=.35;
mu21=-.1;
mu22=-.1;
sigma21=.35;
sigma22=.35;
rho1=0.0;
rho2=0.0;
% functions
bivar(\ma,\sa,\mb,\sb,\rho)=1/(2*pi*\sa*\sb*sqrt(1-\rho*\rho)) * exp(-((x-\ma)^2/\sa^2 + (y-\mb)^2/\sb^2 - (2*\rho*(x-\ma)*(y-\mb))/(\sa*\sb)))/(2*(1-\rho*\rho));
twogauss=max(bivar(mu11,sigma11,mu12,sigma12,rho1), bivar(mu21,sigma21,mu22,sigma22,rho2));
% hyperplane
line(\m,\b)=\m*x + \b;
slope=-(mu21-mu11)/(mu22-mu12);
intercept=(mu12+mu22)/2-slope*(mu11+mu21)/2;
discriminant=line(slope, intercept);
},
]
\begin{axis}[
width=15cm,
view={60}{45},
enlargelimits=false,
grid=major,
domain=-1:1,
y domain=-1:1,
samples=50,
xlabel=$x_1$,
ylabel=$x_2$,
zlabel={$P$},
]
\addplot3 [
surf,
colormap name={bluewhiteorange},
point meta={(
bivar(mu11,sigma11,mu12,sigma12,rho1)>
bivar(mu21,sigma21,mu22,sigma22,rho2)?
bivar(mu11,sigma11,mu12,sigma12,rho1):
-bivar(mu21,sigma21,mu22,sigma22,rho2)
)},
% this works on the surf, but not on the contours
% point meta={y>discriminant?.5:-.5},
] {twogauss};
% work around with a semi step function (but it inserts spurious white contours at the middle)
\addplot3 [
contour lua={
number=15,
labels=false,
},
colormap name={stepblueorange},
point meta={(
bivar(mu11,sigma11,mu12,sigma12,rho1)>
bivar(mu21,sigma21,mu22,sigma22,rho2)?
bivar(mu11,sigma11,mu12,sigma12,rho1):
-bivar(mu21,sigma21,mu22,sigma22,rho2)
)},
z filter/.code={\def\pgfmathresult{.8}},
] {twogauss};
% this collapses the contour into the boundary
\addplot3 [
contour lua={
number=15,
labels=false,
},
colormap name={bluewhiteorange},
point meta={y>discriminant?.5:-.5},
z filter/.code={\def\pgfmathresult{1}},
] {twogauss};
\end{axis}
\end{tikzpicture}
\end{document}
使用更简单的抛物面并仅编译轮廓的示例。
% !TeX program=lualatex
\documentclass{standalone}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.18,
colormap={bluewhiteorange}{color(0cm)=(NavyBlue); color(0.5cm)=(white); color(1cm)=(BurntOrange)},
colormap={stepblueorange}{color(0cm)=(NavyBlue); color(.4999cm)=(NavyBlue); color(0.5cm)=(white); color(0.5001cm)=(BurntOrange); color(1cm)=(BurntOrange)},
}
\begin{document}
\begin{tikzpicture}[
declare function={
% center
pc11=1;
pc12=1;
% center
pc21=-2;
pc22=-2;
% functions
parb(\cx,\cy)=-(x-\cx)^2-(y-\cy)^2;
twoparb=max(parb(pc11,pc12), parb(pc21,pc22));
% hyperplane
line(\m,\b)=\m*x + \b;
slope=-(pc21-pc11)/(pc22-pc12);
intercept=(pc12+pc22)/2-slope*(pc11+pc21)/2;
discriminant=line(slope, intercept);
},
]
\begin{axis}[
width=15cm,
view={60}{45},
domain=-5:5,
]
% This one plots the 3D surfaces
% \addplot3 [
% surf,
% samples=10,
% colormap name={bluewhiteorange},
% point meta={y>discriminant?.5:-.5},
% ] {twoparb};
\addplot3 [
contour lua={
number=6,
labels=false,
},
colormap name={stepblueorange},
% this shows the contours with the spurious boundaries in the middle
% point meta={(
% parb(pc11,pc12)>
% parb(pc21,pc22)?
% parb(pc11,pc12):
% -parb(pc21,pc22)
% )},
% this colors the contours based on the line, but destroyes them
point meta={y>discriminant?.5:-.5},
z filter/.code={\def\pgfmathresult{1}},
] {twoparb};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
当您更改时point meta
,您会更改轮廓所基于的值。就 PGFPlots 而言,轮廓是具有单一样式和颜色的线。
以下是对您要实现的目标的猜测:
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fadings}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfplotsset{colormap={bluewhiteorange}{color=(blue); color=(white); color=(orange)}}
\tikzset{
declare function={
gauss(\x,\y)=exp(-6*(\x^2+\y^2));
f(\x,\y)=max( gauss(\x-0.3,\y-0.3) , gauss(\x+0.3,\y+0.3) )-1;
}}
\begin{tikzfadingfrompicture}[name=myfading]
\begin{axis}[
axis lines=none,
view={60}{45},
zmin=-1, zmax=1,
]
\addplot3[
contour lua={number=8, labels=false, draw color=transparent!0},
domain=-1:1, samples=50,
z filter/.code={\def\pgfmathresult{.8}},
] {f(x,y)};
\end{axis}
\path (0,0) circle[radius=10]; %encompassing circle for alignment
\end{tikzfadingfrompicture}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
name=graph,
view={60}{45},
zmin=-1, zmax=1,
xlabel=x, ylabel=y, zlabel=z,
]
\addplot3[
surf,
domain=-1:1, samples=50,
point meta={x+y<0?-z-1:z+1},
] {f(x,y)};
\coordinate (a) at (1,-1,0.8);
\coordinate (b) at (-1,1,0.8);
\end{axis}
\fill[path fading=myfading, fit fading=false, blue] (graph.south west) -- (graph.north west) -- (b) -- (a) -- cycle;
\fill[path fading=myfading, fit fading=false, orange] (graph.north east) -- (graph.south east) -- (a) -- (b) -- cycle;
\end{tikzpicture}
\end{document}