7

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}

2 Answers 2

14

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).

4
  • Very good answer. Both answers seen to do the trick. I am in doubt of the best answer :)
    – cacamailg
    Jul 21, 2013 at 19:01
  • 5
    Of course Christian's answer is better, as he is the developer of pgfplots :-) Jul 21, 2013 at 19:04
  • Is there a way to make the sphere solid?
    – CroCo
    Aug 14, 2015 at 14:42
  • Well, opacity=0.5 means "50% translucent". Skip it and it is solid. Aug 15, 2015 at 6:34
8

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

1
  • 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, 2013 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .