Estou tentando preencher a área deste gráfico 3D, usando a ideia deesta postagem. Aqui está um MWE para a função que desejo traçar:
\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}
\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]
\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};
\end{axis}
\end{tikzpicture}
\end{document}
Aqui está o resultado:
Agora, aqui está o MWE com o código quedevesombra nas laterais:
\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}
\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]
\begin{pgfonlayer}{pre main}
\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};
\end{pgfonlayer}
\addplot3 [name path = xline, draw = none] (x,0,0);
% x is the variable
% second 0 is the y coordinate of the leftmost part of the graph
% third 0 is height
\addplot3 [name path = yline, draw = none] (2,y,0);
% y is the variable
% first 2 is the x coordinate of the rightmost part of the graph
% third 0 is height
\addplot3 [name path = xcurve, y domain = 0:0, draw = none]
(x, 0, {exp(-x^2)});
% now instead of height being 0, it is height of function
\addplot3 [name path = ycurve, y domain = 0:0, draw = none]
(2, x, {exp(-(x^2 + 4))});
% same idea
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
fill between[of = xcurve and xline];
% fills in x axis
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
fill between[of = yline and ycurve, reverse = true];
\end{axis}
\end{tikzpicture}
\end{document}
E este é o resultado:
Alguém pode fazer alguma sugestão sobre o que está acontecendo? Parece que quando adiciono várias funções ao mesmo gráfico via \addplot3[]
, tudo fica confuso.
Desculpe se isso parece um "apenas conserte isso para mim", mas não tenho ideia do que estou fazendo de errado.
EDITAR
Agora vejo que por algum motivo, ao adicionar os novos gráficos para sombrear ao lado, o domínio foi alterado de (0,2) para (-4,4), portanto, definir o domínio à medida que você adiciona os gráficos ajuda:
\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}
\begin{document}
\begin{tikzpicture}
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}
\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]
\begin{pgfonlayer}{pre main}
\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};
\end{pgfonlayer}
\addplot3 [name path = xline, draw = none, domain=0:2] (x,0,0);
% x is the variable
% second 0 is the y coordinate of the leftmost part of the graph
% third 0 is height
\addplot3 [name path = yline, draw = none, domain=0:2] (2,y,0);
% y is the variable
% first 2 is the x coordinate of the rightmost part of the graph
% third 0 is height
\addplot3 [name path = xcurve, domain=0:2, y domain = 0:0, draw = none]
(x, 0, {exp(-x^2)});
% now instead of height being 0, it is height of function
\addplot3 [name path = ycurve, domain=0:2, y domain = 0:0, draw = none]
(2, x, {exp(-(x^2 + 4))});
% same idea
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
fill between[of = xcurve and xline];
% fills in x axis
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
fill between[of = yline and ycurve, reverse = true];
\end{axis}
\end{tikzpicture}
\end{document}
E aqui está o resultado:
Minha pergunta agora é: por que a função original foi alterada?
OUTRA EDIÇÃO
Eu descobri uma maneira de conseguir o que quero (sem incluir as excelentes respostas abaixo), mas ainda tenho mais algumas preocupações. Aqui está um MWE com um bom resultado:
\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps,fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
zmax=1.25,
zmin=0,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]
\addplot3[
surf,
samples=30,
domain=0:2,
%shader=interp,
opacity=0.5,
]
{exp(-(x^2+y^2))};
\addplot3 [name path = xline, draw = none, domain=0:2] (x,0,0);
% x is the variable
% second 0 is the y coordinate of the leftmost part of the graph
% third 0 is height
\addplot3 [name path = yline, draw = none, domain=0:2] (2,y,0);
% y is the variable
% first 2 is the x coordinate of the rightmost part of the graph
% third 0 is height
\addplot3 [name path = xcurve, domain=0:2, y domain = 0:0, draw = none]
(x, 0, {exp(-x^2)});
% now instead of height being 0, it is height of function
\addplot3 [name path = ycurve, domain=0:2, y domain = 0:0, draw = none]
(2, x, {exp(-(x^2 + 4))});
% same idea
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
fill between[of = xcurve and xline];
% fills in x axis
\addplot [color = SteelBlue3, opacity = 0.5, draw = none]
fill between[of = yline and ycurve, reverse = true];
\end{axis}
\end{tikzpicture}
\end{document}
O que dá este resultado:
Agora, a questão é a %shader=interp,
linha. Quando eu descomente isso, isso acontece:
Então, este comando força o gráfico a se deslocar para cima por algum motivo? Por que isso acontece?
Responder1
Algo assim?
\PassOptionsToPackage{usenames,dvipsnames,table,x11names}{xcolor}
\documentclass[a4paper, 12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{colormaps,fillbetween}
\begin{document}
\begin{tikzpicture}[declare function={f(\x,\y)=exp(-(\x*\x+\y*\y));}]
\pgfdeclarelayer{pre main}
\pgfsetlayers{pre main,main}
\begin{axis}[
zmax=1.25,
view = {45}{45},
grid=minor,
colormap={mycol}{color=(SteelBlue3), color=(SteelBlue3)},
xlabel = $x$,
ylabel = $y$,
zlabel = {$f(x,y)$},
]
\addplot3[
surf,
samples=30,
domain=0:2,
shader=interp,
opacity=0.5,
]
{f(x,y)};
\fill[blue] (0,0,-1) -- plot[variable=\x,domain=0:2] (\x,0,{f(\x,0)}) -- (2,0,-1) --
cycle;
\fill[blue!80] (2,0,-1) -- plot[variable=\y,domain=0:2] (2,\y,{f(2,\y)})
-- (2,2,-1) --cycle;
\end{axis}
\end{tikzpicture}
\end{document}
Ou, se você fizer os preenchimentos com
\fill[SteelBlue3] (0,0,-1) -- plot[variable=\x,domain=0:2] (\x,0,{f(\x,0)}) -- (2,0,-1) --
cycle;
\fill[SteelBlue3] (2,0,-1) -- plot[variable=\y,domain=0:2] (2,\y,{f(2,\y)})
-- (2,2,-1) --cycle;
você consegue
Observe que eu adicionei \pgfplotsset{compat=1.16}
. Se você não quiser isso, você precisa anexar as coordenadas comaxis cs:
\fill[SteelBlue3] (axis cs:0,0,-1) -- plot[variable=\x,domain=0:2] (axis cs:\x,0,{f(\x,0)}) -- (axis cs:2,0,-1) --
cycle;
\fill[SteelBlue3] (axis cs:2,0,-1) -- plot[variable=\y,domain=0:2] (axis cs:2,\y,{f(2,\y)})
-- (axis cs:2,2,-1) --cycle;