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'm trying to make a plot of a torus with a semicircle drawn on its surface. The following MWE

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[hide axis,
        axis equal,
        view={-37.5}{30}]
            \addplot3[surf,
            shader=interp,
            samples=20,
            domain=0:2*pi,y domain=-pi:pi,
            z buffer=sort]
            ({(1+0.25*cos(deg(x)))*cos(deg(y))}, 
             {(1+0.25*cos(deg(x)))*sin(deg(y))}, 
             {0.25*sin(deg(x))});

            \addplot3[color=black,
            samples=20,
            domain=0:pi,
            line width=2.0pt]
            ({cos(deg(x))}, 
             {sin(deg(x))}, 
             {0.25});
    \end{axis}
\end{tikzpicture}
\end{document}

generates

torus

which shows the problem: the semicircle gets closed. I'm not particularly well-versed in pgfplots (most of the above is stuff I picked up from examples and plots generated with the Matlab script matlab2tikz). Is there an easy way to get the unclosed semicircle I'm after?


Just did a quick test: the following code

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[hide axis,
        axis equal]
            \addplot[color=black,
            samples=20,
            domain=0:pi,
            line width=2.0pt]
            ({cos(deg(x))}, 
             {sin(deg(x))});
    \end{axis}
\end{tikzpicture}
\end{document}

shows that this problem doesn't occur in a 2D plot:

semicircle2D

share|improve this question

1 Answer 1

up vote 9 down vote accepted

You can fix this by using

 \addplot3[color=black,
        samples=20,
        domain=0:pi,
        line width=2.0pt,samples y=0]

so that the addplot3 command only plots a curve, not a surface.

screenshot

Further details can be found in Section 4.6.9: Parameterized plots of the pgfplots documentation.

share|improve this answer
    
Perfect! Thanks for the answer and the reference! –  Wouter May 23 at 20:02
    
@Wouter, use line cap = round so that the 3D effect is not ruined. \addplot3[color=blue, samples=20, domain=0:pi, line width=2.0pt,samples y=0, line cap=round] –  alfC Sep 19 at 5:25
    
@alfC In my case this was part of a bigger figure in which the endpoints of the semicircle were no longer visible, but thanks for the useful hint anyway! –  Wouter Sep 19 at 11:52

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.