# How to tranch a 3D plot by x or y

When I use a 3D plot in class, I like representing first, 2D plots along each axis (as if I was "tranching" the 3D graph), and then explain the dynamic in 3D. The Final 3D plot is then easier to assimilate for students.

How can I "tranch" properly a 3D plot with a set of 2D plots passing conditional argument in styles ?

So far, I get the result I like by (inefficiently) superposing the plot with 2 different styles.

It works well but I am reinventing the wheel since the 3D plot facets already exist, i'd just need to display them along the x only or along the y only to have what I'd like. I can display, the 2D version in 3D or the 3D only or both which is convenient.

Where am I stuck?

1. I created \newifs to choose whether i wanted a 3D plot or the set of 2D plot. It's too laborious to be correct.
2. As a result, how could I pass the argument equivalent to \newif so that the style takes care of the 2D and/or 3D view (and not plot twice the function with 2 distinct styles). It could also handle the granularity of the samples to see more or less 2D curves.
3. Also, as you can see in the tikzset in comment, 2D/.style 3D/.style generate multiple errors.
4. No problem to "tranch" based on y but \ifTranchX to try along the x is not working

Here is MWE

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\pgfplotsset{colormap={whitered}{color(0cm)=(white!20!orange); color(2.5cm)=(orange!75!red)}}

\newif\ifTwoD
\newif\ifThreeD
\newif\ifTranchX
\newif\ifTranchY

\TwoDtrue
\ThreeDtrue

%\TranchXtrue
\TranchYtrue

%\tikzset{
%2D/.style  ={samples y=10,mesh,patch type=line,thick},
%3D/.style  ={surf,opacity=0.2},
%}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
view={-30}{30},
axis lines=left,
axis on top,
axis line style={black!40},
xlabel style ={sloped},
ylabel style ={sloped},
colormap name=whitered,
ticklabel style={font=\small},
samples=51]

\ifTwoD
\ifTranchX
\fi
\ifTranchY
\fi
\fi

\end{axis}
\end{tikzpicture}
\end{document}


NB: On a cultural note, i tried to mimic the color of "papier millimétré" I used to work on in the 80's (in France, but maybe elsewhere ?). papier millimétré

• Apart from the information that you need \pgfplotsset (and not \tikzset) in \pgfplotsset{ 2D/.style ={samples y=10,mesh,patch type=line,thick}, 3D/.style ={surf,opacity=0.2}, } I am not sure I understand the question. The output looks good, and using ifs sounds like a good idea. What precisely do you want an answerer to do?
– user194703
May 2 '20 at 17:39
• @Schrödinger'scat merci :) glad to read there is not much to change. My question is, how can I take advantage of the styles and 3d plot facets (I mean the grid on the 3d plot) to have only one addplot to draw rather that 2 I superpose. I presume it would speed up the process (I have so many of these to compile at the end).
– JeT
May 2 '20 at 17:45
• I am afraid that there is no easy way to achieve this. You would have to hack the plot handler.
– user194703
May 2 '20 at 18:05
• @Schrödinger'scat understood! Merci for your feedback. Shall we close that question?
– JeT
May 2 '20 at 18:07
• There is no need to, maybe someone shows up who knows how to do that. Notice that I did not say "absolutely impossible", I just do not see an easy way to achieve this. On the other hand, I would think that if someone looks deeply into the plot handlers, they will be able to come up with something that makes every fifth line thick. What I want to say is that, if that is the question, it is a very interesting one (and you may perhaps want to reformulate it that way), and an answer will be useful for many.
– user194703
May 2 '20 at 18:10

This just collects the comments under the post and does not hack any plot handler. I just thought that instead of writing yet another comment I just collect everything in one post which users can just copy. I think that if the we have ifs for the x and y direction, we won't need another if for 2d. These ifs can be turned into pgf keys in the usual way. The \tikzset has to become a \pgfplotsset to be able to "host" genuine pgfplots styles.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{colormap={whitered}{color(0cm)=(white!20!orange); color(2.5cm)=(orange!75!red)}}

\newif\ifThreeD
\newif\ifTranchX
\newif\ifTranchY

\ThreeDfalse
\TranchXfalse
\TranchYfalse

\pgfplotsset{tranch 3d/.is if=ThreeD,
tranch y/.is if=TranchY,tranch x/.is if=TranchX,
2D/.style  ={samples y=10,mesh,patch type=line,thick},
3D/.style  ={surf,opacity=0.2,postaction={draw}},
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[tranch 3d,tranch x,
view={-30}{30},
axis lines=left,
axis on top,
axis line style={black!40},
xlabel style ={sloped},
ylabel style ={sloped},
colormap name=whitered,
ticklabel style={font=\small},
samples=51]

\ifTranchX
\fi
\ifTranchY
\fi

\ifThreeD
\fi
\end{axis}
\end{tikzpicture}
\end{document}


If you replace tranch x by tranch y you get

and so on and so forth.

• I did not know the tranch 3d/.is that is soooo useful !
– JeT
May 2 '20 at 20:18
• @Julien-ElieTaieb I had a look at the follow-up question (+1) but apart from some way to hack mash plots there is no progress.
– user194703
May 2 '20 at 20:20
• you gave me an improvement on my question so thank you. Let's see if like you said someone comes up with an alternative idea (but given the complexity of the problems you generally sort... I doubt it :).
– JeT
May 2 '20 at 20:22
• @Julien-ElieTaieb I am pretty sure that users like Christian Feuersänger or Jake could solve this almost instantly, but they are not too active these days.
– user194703
May 2 '20 at 20:27
• @Schrodigerscat, just as if my daughter was asking Beyoncé something :)
– JeT
May 2 '20 at 21:17