In my opinion, you are using a too powerful tool for a relatively simpler task. The canvas transformations simply don't care about any other settings like bounding boxes or any other high level constraints. They just happen. Instead, you can modify the default vertical shadings such that they obey the shading angle
option. I've randomly rotated the shading below with 45, 120, and -45 degrees hoping that it was the reason for the question :)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\tikzoption{top color}{%
\pgfutil@colorlet{tikz@axis@top}{#1}%
\pgfutil@colorlet{tikz@axis@middle}{tikz@axis@top!50!tikz@axis@bottom}%
\def\tikz@shading{axis}\tikz@addmode{\tikz@mode@shadetrue}}
\tikzoption{bottom color}{%
\pgfutil@colorlet{tikz@axis@bottom}{#1}%
\pgfutil@colorlet{tikz@axis@middle}{tikz@axis@top!50!tikz@axis@bottom}%
\def\tikz@shading{axis}\tikz@addmode{\tikz@mode@shadetrue}}
\tikzoption{middle color}{%
\pgfutil@colorlet{tikz@axis@middle}{#1}%
\def\tikz@shading{axis}\tikz@addmode{\tikz@mode@shadetrue}}
\makeatother
%: isometric South West (-150): Y , East : X , North : Z
\tikzset{isometricYXZ/.style={x={(1cm,0cm)}, y={(-1.299cm,-0.75cm)}, z={(0cm,1cm)}}}
\begin{document}
\begin{tikzpicture}[inner sep=0.2cm]
\def \radi{3}
\def \x{2}
\def \y{2}
\def \z{2}
\begin{scope}[isometricYXZ]
% the grid
\begin{scope}[color=gray!50, thin]
\foreach \xi in {0,...,\radi}{ \draw (\xi,\radi,0) -- (\xi,0,0) -- (\xi,0,\radi); }%
\foreach \yi in {1,...,\radi}{ \draw (0,\yi,\radi) -- (0,\yi,0) -- (\radi,\yi,0); }%
\foreach \zi in {0,...,\radi}{ \draw (0,\radi,\zi) -- (0,0,\zi) -- (\radi,0,\zi); }%
\end{scope}
\draw[-latex, ultra thick, color=blue] (0,0,0) -- (4,0,0) node[anchor=west] {X};%
\draw[-latex, ultra thick, color=red] (0,0,0) -- (0,4,0) node[anchor=north] {Y};%
\draw[-latex, ultra thick, color=green] (0,0,0) -- (0,0,4) node[anchor=east] {Z};%
\fill[shading angle=45, top color=green!30, bottom color=blue!30] (0,0,0) -- (\x,0,0) -- (\x,\y,0) -- (0,\y,0) -- cycle; %
\fill[shading angle=-45, top color=green!30, bottom color=blue!30] (0,0,0) -- (0,0,\z) -- (0,\y,\z) -- (0,\y,0) -- cycle; %
\fill[shading angle=120, top color=green!30, bottom color=blue!30] (0,0,0) -- (\x,0,0) -- (\x,0,\z) -- (0,0,\z) -- cycle;
\draw[color=gray, thick]%
(0,\y,\z) -- (\x,\y,\z) -- (\x,\y,0) (\x,\y,\z) -- (\x,0,\z);%
\end{scope}
\shade[ball color=yellow] ($\y*(-1.299cm,-0.75cm)+(\x,\z)$) circle (0.1);%
\end{tikzpicture}
\end{document}

I guess the reason for the shading angle
disabled (set to zero) originally is to get the other transformations invalid at the time of shading declaration. You can make these changes local if you have other TikZ pictures that use these options.
So, your input is a 2d function f(x,y) and its surface, and you want to see its trace through z=0. This can be accomplished by means of a contour plot with level 0.
Here is a solution:

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
%
\begin{document}
\thispagestyle{empty}
%
Initial \TeX{}t
\begin{figure}[h]
\begin{centering}
\begin{tikzpicture}
\tikzset{%
small dot/.style={fill=cyan,circle,scale=0.25}%
}
\begin{axis}[%
grid=major,
colormap/cool,
zlabel=$z$,
zmin=-5,
ztick={-5,-2.5,0,2.5,5},
ytick={-5,-2.5,0,2.5,5},
zmax=5,
xmin=-5,
xmax=5,
xlabel=$x$,
ylabel=$y$%
]
\addplot3 [surf] {0};
\addplot3[contour gnuplot={labels=false,levels={0},draw color=blue},domain=-4:4]
{exp(-x^2 -0.3*y^2 -0.2*x*y)*5- 0.1};
%
\end{axis}
\end{tikzpicture}
\par
\end{centering}
\caption{Funzione definita $f_{1}(x,y)$ o $z_{1}$}
\end{figure}
\par
Ending \TeX{}t
%
\end{document}
I took the freedom to discard the blue cross to show only the contour plot fragment. Note that I used some random surface which came to my mind, but any expression or table data is suitable here.
This example requires gnuplot
as pgfplots
is unable to compute the contour levels on its own. Consequently, you need to run it with -shell-escape
(like pdflatex -shell-escape <file.tex>
).
The argument labels=false
deactivates contour text labels, the levels
key chooses the requested contour levels (accepts a list), and draw color
is necessary because otherwise, the current colormap
would be used which is inadequate in this context.
EDIT the following is OPTIONAL.
For the records, here is my earlier (mis)understanding of the request. It addresses a slighty different use-case, namely one in which the 1d contour is given and the question is how to position it correctly.
From what I understand, you are given some 1d function f(x) which is given either as math expression (your example with sin(x)) or perhaps as data table, right? And you want that to be plotted into a 3D plane which happens to be orthogonal to one of the cube faces?
In this case, you can use either a parametric plot (if the function is to be sampled) or reorder the columns if the plot function is given as data table.
Here is your example with modifications:

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
%
\begin{document}
\thispagestyle{empty}
%
Initial \TeX{}t
\begin{figure}[h]
\begin{centering}
\begin{tikzpicture}
\tikzset{%
small dot/.style={fill=cyan,circle,scale=0.25}%
}
\begin{axis}[%
grid=major,
colormap/cool,
zlabel=$z$,
zmin=-5,
ztick={-5,-2.5,0,2.5,5},
ytick={-5,-2.5,0,2.5,5},
zmax=5,
xmin=-5,
xmax=5,
xlabel=$x$,
ylabel=$y$%
]
\addplot3[green] table[x=X,y expr=5,z=Y]
{
X Y
-5 5
-4 -1
-3 0
-2 1
-1 1.2
0 1
1 2
2 3
4 1
5 0
};
\addplot3 [surf] {0};
\addplot3 [color=blue] coordinates {(0,-5,0) (0,5,0)};
\addplot3 [color=blue] coordinates {(-5,0,0) (5,0,0)};
\node[small dot,pin=-60:{$z_1$}] at (axis cs:2.5,-5,0) {};
\addplot3[red,samples y=1]
(x,{4*sin(deg(x))},0);
%
\end{axis}
\end{tikzpicture}
\par
\end{centering}
\caption{Funzione definita $f_{1}(x,y)$ o $z_{1}$}
\end{figure}
\par
Ending \TeX{}t
%
\end{document}
The red
plot is a parametric plot; you see that I provided x,y,and z coordinate values explicitly, z being zero here due to the z=0 plane. Note that samples y=1
is required because \addplot3 {<expression>}
always samples a 2d function f(x,y). If we say samples y=1
, we tell pgfplots
that it actually has just one coordinate, i.e. that it should sample a line although it is a 3d plot.
The green
plot is a table plot in which I mapped the table's two columns X
and Y
to the plane y=5
. Note that the ordering of plots is important.
Is this what you had in mind?
Best Answer
A
pstricks
solution. Thepst-eucl
package is designed for plane geometry. Its\pstlineAB
command defines a line with two points; the line can stop at a chosen distance of the two points, wich allows for lines defined by two pointsthat are figure-wide (too long lines are cropped by thepspicture*
environment). I had to patch the labels-making command in order to use coloured sans maths as in the figure.