# TikZ, 3d library and circle fading

I'm using the 3d library to put a circle on the z=0 plane, and other things. I'd like to fade this circle radially, but the result isn't as I would expect because the fading is not proportional to the distance from the origin. How can I do?

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture} [x={(-0.3535,-0.3535cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}, scale=3]
\begin{scope} [canvas is xy plane at z=0]
\end{scope}
\draw[-latex] (-1,0,0) -- (1,0,0) node [left] {$x$};
\draw[-latex] (0,-1,0) -- (0,1,0) node [below] {$y$};
\draw[-latex] (0,0,-0.5) -- (0,0,0.5) node [left] {$z$};
\end{tikzpicture}
\end{document}


-
Not sure I understand the problem. The image you included does fade radially proportional to the distance (in the xy plane) from the origin. You have graphed this in 3D, not 2D. If you eliminate the z-axis with [x={(1,0cm)}, y={(0cm,1cm)}, z={(0cm,0cm)}, scale=2] you can see it more clearly. –  Peter Grill Jun 20 '12 at 17:46
@Peter Grill, this is not true. You see an ellipse, but the figure is a circle. So every point on the border is at the same distance from the origin, but has a different fading level. I need to put that circle on the z=0 plane and I need 3d because in the same picture there are other elements that I didn't draw to simplify the problem and the code. If you prefer, I can draw the coordinate system too. –  Luigi Jun 20 '12 at 17:54
I suspect this is because TikZ is not a real 3D system so even though one can simulate it, at heart everything is 2D. So when you ask TikZ to shade that circle, it thinks it is shading a 2D ellipse and behaves appropriately. You'll probably need to do something like a functional shading to make it correct. –  Loop Space Jun 20 '12 at 18:17

As already mentioned by Andrew Stacey in some comment, TikZ is not a real 3D system - and a radial fading is a two-dimensional construct.

You would need some 3d projection algorithm to compute the colors.

A possibility would be to sample such a projection and to interpolate between the sampled points. This is not directly supported by TikZ (because it involves more complicated shadings), but you can use pgfplots and its surf plot handler to get the effect:

EDIT: I did not realize that you asked explicitly for the axis direction vectors of your example. I added them to the answer.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.7} % *EDIT*: this improves scale uniformly.

\begin{document}
\begin{tikzpicture}
\begin{axis}[axis on top,
axis lines=center,
min=-1,max=1,
colormap={somename}{color=(white) color=(blue)},
% *EDIT*: this here respects your choice of unit vectors.
%         scale uniformly computes one common scaling factor
%         and chooses limits such that the image fulfills the
%         prescribed width/height as best as possible.
x={(-0.3535cm,-0.3535cm)}, y={(1cm,0cm)}, z={(0cm,1cm)}, scale mode=scale uniformly,
]

%z buffer=sort, % only for complete sphere
samples=30,
domain=-1:0, % -1:1 is a complete sphere. -1:0 half
y domain=0:2*pi,
variable=\t,
point meta=t^2]
({sqrt(1-t^2) * cos(deg(y))},
{sqrt( 1-t^2 ) * sin(deg(y))},
0);% use 't' here to draw the sphere

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


The idea is to use a sphere parameterization of the form (x(t,y), y(t,y), 0), i.e. to fix z=0. The key point meta defines the color data. I figured that t would be the correct choice, but t^2 looks better. The surf,shader=interp key draws the sampled parametric plot as surface with interpolated colors.

The colormap uses RGB color interpolation. What is transparent in your example? I defined a colormap using blue and white.

EDIT: just in case you are using pgfplots 1.7 or higher, you can use the new feature patch type sampling combined with a higher-order patch like patch type=bicubic. This allows to reconstruct the geometry with smooth(er) boundaries.

The following example has been generated with \usepgfplotslibrary{patchplots}

and

patch type sampling, patch type=bicubic,
samples=10,% CF reduced because bicubic is smooth anyway


-
Thank you for your answer. I'll try it as soon as possible. The keyword transparent is defined in the fadings library (see par. 20.3.1 on page 236 of the pgfmanual). –  Luigi Jun 22 '12 at 16:42
sorry! I forgot to reply and in the meanwhile I gave up, because I needed an oblique projection (see the picture) and pgfplots can only handle othographic projections at the moment. Thank you anyway. –  Luigi Nov 14 '12 at 11:01
No problem. Note that even if you do not need the answer anymore, I took the freedom to advertise that (a) pgfplots supports arbitrary unit vectors as long as they make up a right-handed-system (can be combined with x dir=reverse) and (b) pgfplots now also simplifies the generation of surfaces with smooth(er) boundaries. See my edit for details. –  Christian Feuersänger Nov 14 '12 at 12:13
Wonderful! I need to study much better your manual. Now, if you want to surprise me once more, focus on perspective. I know you can do it! (Thanks) –  Luigi Nov 14 '12 at 14:40