Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to draw a plot that illustrates the ration pattern of an isotropic antenna. I thought about pgfplots to do the job. I grabbed the code from pgfplots manual and did some changes; but it doesn't satisfies me.

As I said in the title, I want the axis to be drawn in the center, but I also want to be able to see the (0,0,0) point. So to be able to look inside, the sphere surface opacity shall be lower.

Moreover, the code is not optimized, I need to specify the min and max for all axis just to see the arrow of the axis outside the surface. Is there a better way?

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{%
    compat=1.8,
    compat/show suggested version=false,
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
    footnotesize,
    axis equal,
    axis lines=center,
    xlabel=$x$,
    ylabel=$y$,
    zlabel=$z$,
    xmax=2,xmin=-2,
    ymax=2,ymin=-2,
    zmax=2,zmin=-2,
    xtick=\empty,
    ytick=\empty,
    ztick=\empty,
    ]
    \addplot3[%
      surf,
      z buffer=sort,
      samples=15,
      variable=\u,
      variable y=\v,
      domain=0:180,
      y domain=0:360
    ]
    ({cos(u)*sin(v)}, {sin(u)*sin(v)}, {cos(v)});
    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this question

2 Answers 2

up vote 10 down vote accepted

Using axis equal causes to maintain the width/height and to modify axis limits and the image scaling. In order to keep the axis limits and modify just the units, we can say scale uniformly strategy=units only. I added width=10cm to enlarge the sphere compared to the axis descriptions (different fonts for the axis description might also have done the job). Adding height=10cm as well avoids confusion as to which of the parameters width or height is to be used in the final version.

Adding view/h=45 appears to be quite good as well.

Combined with opacity as suggested by Henri, we end up at

enter image description here

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
        axis equal,
        width=10cm,
        height=10cm,
        axis lines = center,
        xlabel = {$x$},
        ylabel = {$y$},
        zlabel = {$z$},
        ticks=none,
        enlargelimits=0.3,
        view/h=45,
        scale uniformly strategy=units only,
    ]
    \addplot3[%
        opacity = 0.5,
        surf,
        z buffer = sort,
        samples = 21,
        variable = \u,
        variable y = \v,
        domain = 0:180,
        y domain = 0:360,
    ]
    ({cos(u)*sin(v)}, {sin(u)*sin(v)}, {cos(v)});
    \end{axis}
\end{tikzpicture}
\end{document}

We can also change the parameterization of the sphere to screen coordinate rather than angles and get the LEFT image (the right is the same as above)

enter image description here enter image description here

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
        axis equal,
        width=10cm,
        height=10cm,
        axis lines = center,
        xlabel = {$x$},
        ylabel = {$y$},
        zlabel = {$z$},
        ticks=none,
        enlargelimits=0.3,
        view/h=45,
        scale uniformly strategy=units only,
    ]
    \addplot3[
        surf,
        opacity = 0.5,
        samples=21,
        domain=-1:1,y domain=0:2*pi,
        z buffer=sort]
    ({sqrt(1-x^2) * cos(deg(y))},
     {sqrt( 1-x^2 ) * sin(deg(y))},
     x);

    \end{axis}
\end{tikzpicture}

\end{document}

I causes an even distribution along the z axis (but not along the angles).

share|improve this answer
    
Very good answer. Both answers seen to do the trick. I am in doubt of the best answer :) –  cacamailg Jul 21 '13 at 19:01
3  
Of course Christian's answer is better, as he is the developer of pgfplots :-) –  Henri Menke Jul 21 '13 at 19:04

Remarks

Instead of specifying the limits by hand you can use the key enlargelimits = <value>. The get the transparency use the key opacity = <value>.

If you don't care about compile performance you can use the alternative solution, which was produced as seen on http://www.texample.net/tikz/examples/spherical-polar-pots-with-3dplot/

Implementation

\documentclass[tikz]{standalone}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[%
        axis equal,
        axis lines = center,
        xlabel = {$x$},
        ylabel = {$y$},
        zlabel = {$z$},
        enlargelimits = 0.5,
        ticks=none,
    ]
    \addplot3[%
        opacity = 0.5,
        surf,
        z buffer = sort,
        samples = 21,
        variable = \u,
        variable y = \v,
        domain = 0:180,
        y domain = 0:360,
    ]
    ({cos(u)*sin(v)}, {sin(u)*sin(v)}, {cos(v)});
    \end{axis}
\end{tikzpicture}

% Alternative solution: http://www.texample.net/tikz/examples/spherical-polar-pots-with-3dplot/
\tdplotsetmaincoords{70}{135}
\begin{tikzpicture}[>=stealth,line join=bevel,tdplot_main_coords,fill opacity=.5]
\tdplotsphericalsurfaceplot[parametricfill]{72}{36}%
    {1}{black}{\tdplottheta}%
    {\draw[color=black,thick,->] (0,0,0) -- (2,0,0) node[anchor=north east]{$x$};}%
    {\draw[color=black,thick,->] (0,0,0) -- (0,2,0) node[anchor=north west]{$y$};}%
    {\draw[color=black,thick,->] (0,0,0) -- (0,0,2) node[anchor=south]{$z$};}%
\end{tikzpicture}
\end{document}

Output

enter image description here enter image description here


With PSTricks and pst-solides3d (just for fun)

Compile with xelatex or with latex -> dvips -> ps2pdf.

\documentclass[pstricks]{standalone}
\usepackage{pst-solides3d}
\begin{document}
\begin{pspicture}(-5,-4)(5,6)
    \psSolid[
        object=sphere,
        r=1,
        linecolor=blue,
        fillcolor=blue!10,
        action=draw*,mode=4,
        ngrid=18 18
    ]
    \axesIIID(0,0,0)(2,2,2)
\end{pspicture}
\end{document}

enter image description here

share|improve this answer
    
I have completely forgotten the enlargelimits option. I didn't know about the tikz-3dplot, which seems to do the trick better than pgf-plots. Thanks! –  cacamailg Jul 21 '13 at 18:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.