10

I've got a slight problem with the way TikZ draws my 3D figure. The edges of the top and bottom of the cylinder in the picture don't seem to match correctly even though I don't think I made a mistake. Does anybody know why it does this or is there a better way to draw this? (still quite new to TikZ and 3D drawing)

    \documentclass[11pt,a4paper,onesided]{report}

\usepackage[ngerman]{babel}



\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{pgfplots}


\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}
\begin{document}

    \begin{tikzpicture}[scale=1]
    \draw[thin,->] (-2,0) -- (5,0);
    \draw[thin,->] (0,-2) -- (0,3);

    %Zylinder
    \draw[] (1.5,-1.5) -- (1.5,1.5);
    \draw[] (3.5,-1.5) -- (3.5,1.5);
    \begin{scope}[canvas is zx plane at y=0]
    \draw (0,2.5) circle (2cm);
    \draw[fill=gray,opacity=0.5] (0,2.5) circle (1cm);
    %
    \draw[->] (-2,0) -- (3,0);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=-1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}     

    \begin{scope}[every node/.append style={
        xslant=1,sloped}
    ]    
    \node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
    \node at (4,.4) {\scalebox{1}[.7]{$T$}};
    \node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
    \end{scope}


    \end{tikzpicture}

\end{document}

The result:

enter image description here

  • I uploaded the image so anyone can quickly see the problem. I hope it's OK. – Gonzalo Medina Sep 10 '15 at 22:43
  • Have a look at Andrew's excellent answer here. – Tom Bombadil Sep 11 '15 at 7:48
9

Here's an option producing a more pleasing result:

enter image description here

The code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}

\begin{document}

\begin{tikzpicture}[scale=1]
\def\Anglei{-66}

\draw[thin,->] (-2,0) -- (5,0);
\draw[thin,->] (0,-2) -- (0,3);

%Zylinder
\begin{scope}[canvas is zx plane at y=0]
\draw (0,2.5) circle (2cm);
\draw[fill=gray,opacity=0.5] (0,2.5) circle (1cm);
\draw[->] (-2,0) -- (3,0);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ1a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ1b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ2a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ2b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope} 

\begin{scope}[canvas is xy plane at z=0]
\draw (circ1a) -- (circ2a);
\draw (circ1b) -- (circ2b);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\draw (0,2.5) circle (1cm);
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\draw (0,2.5) circle (1cm);
\end{scope} 

\begin{scope}[every node/.append style={
xslant=1,sloped}
]
\node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
\node at (4,.4) {\scalebox{1}[.7]{$T$}};
\node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
\end{scope}
\end{tikzpicture}

\end{document}

The idea is to place some auxiliary coordinates in the appropriate diameter on the circles; for this, the appropriate angle is required and, as Heiko Oberdiek mentions in his comment, since the slanted axis is rotated by 45°, this angle is is (90° + 45°)/2 = 67.5° (modulo the sign).

\def\Anglei{-67.5}

\begin{scope}[canvas is zx plane at y=1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ1a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ1b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ2a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ2b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope} 

Perhaps dashing some arcs might also be a good idea:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}

\begin{document}

\begin{tikzpicture}[scale=1]
\def\Anglei{-67.5}
\def\Angleii{233}
\def\Angleiii{170}

\draw[thin,->] (-2,0) -- (5,0);
\draw[thin,->] (0,-2) -- (0,3);

%Zylinder
\begin{scope}[canvas is zx plane at y=0]
\coordinate (circmb) at ( $ (0,2.5) + (\Angleii:2cm) $ );
\coordinate (circma) at ( $ (0,2.5) + (\Angleiii:2cm) $ );
\draw[dashed] (circmb) arc [start angle=\Angleii,end angle=\Angleiii,radius=2cm];
\draw (circma) arc [start angle=\Angleiii,end angle=\Angleii-360,radius=2cm];
\fill[fill=gray,opacity=0.5] (0,2.5) circle (1cm);

\coordinate (circm1) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circm2) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );

\draw (circm1) arc [start angle=\Anglei,end angle=180+\Anglei,radius=1cm];
\draw[dashed] (circm2) arc [start angle=180+\Anglei,end angle=360+\Anglei,radius=1cm];

\draw[->] (-2,0) -- (3,0);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ1a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ1b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\path (0,2.5) circle (1cm);
\coordinate (circ2a) at ( $ (0,2.5) + (\Anglei:1cm) $ );
\coordinate (circ2b) at ( $ (0,2.5) + (180+\Anglei:1cm) $ );
\end{scope} 

\begin{scope}[canvas is xy plane at z=0]
\draw (circ1a) -- (circ2a);
\draw (circ1b) -- (circ2b);
\end{scope}

\begin{scope}[canvas is zx plane at y=1.5]
\draw (0,2.5) circle (1cm);
\end{scope}

\begin{scope}[canvas is zx plane at y=-1.5]
\draw (circ2a) arc [start angle=\Anglei,end angle=180+\Anglei,radius=1cm];
\draw[dashed] (circ2b) arc [start angle=180+\Anglei,end angle=360+\Anglei,radius=1cm];
\end{scope} 

\begin{scope}[every node/.append style={
xslant=1,sloped}
]
\node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
\node at (4,.4) {\scalebox{1}[.7]{$T$}};
\node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

  • 1
    Trial and error is not needed here. The slanted axis is rotated by 45°, thus the needed angle is (90° + 45°)/2 = 67.5° (for the special case 45° and neglecting the sign). – Heiko Oberdiek Sep 11 '15 at 0:51
  • @HeikoOberdiek Of course, I'll add this. Thanks. – Gonzalo Medina Sep 11 '15 at 0:55
  • Thanks for the improvement! This actually does look better. – Michael A Sep 12 '15 at 14:06
3

You didn’t make a mistake: that’s exactly what you’d get if you took two hoops and connected rods at the diameter. Here’s what it looks like when you draw two more lines in between:

Cylinder with four guidelines

You might also consider using pgfplots to draw the cylinder as a surface or wireframe. You can also move the lines so that they’re at (cos t, sin t) in the zx-plane for some more visually-pleasing value of t than 0 and π.

\PassOptionsToPackage{svgnames}{xcolor}
\documentclass{standalone}
\usepackage{iftex}

\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\else
\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{Latin Modern Math}
\fi

\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.12}
\usetikzlibrary{calc,fit,patterns,decorations.markings,matrix,3d}

\begin{document}

    \begin{tikzpicture}[scale=1]
    \draw[thin,->] (-2,0) -- (5,0);
    \draw[thin,->] (0,-2) -- (0,3);

    %Zylinder
    \begin{scope}[canvas is xy plane at z=0]
    \draw[color=DarkGray] (1.5,-1.5) -- (1.5,1.5);
    \draw[color=DarkGray] (3.5,-1.5) -- (3.5,1.5);
    \end{scope}
    \begin{scope}[canvas is xy plane at z=sqrt(3)/2]
    \draw[color=DarkGray] (2,-1.5) -- (2,1.5);
    \draw[color=DarkGray] (3,-1.5) -- (3,1.5);
    \end{scope}
    \begin{scope}[canvas is zx plane at y=0]
    \draw (0,2.5) circle (2cm);
    \draw[fill=gray,opacity=0.5] (0,2.5) circle (1cm);
    %
    \draw[->] (-2,0) -- (3,0);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}

    \begin{scope}[canvas is zx plane at y=-1.5]
    \draw (0,2.5) circle (1cm);
    \end{scope}     

    \begin{scope}[every node/.append style={
        xslant=1,sloped}
    ]    
    \node at (2.4,-.2) {\scalebox{1}[.7]{$T_0$}};
    \node at (4,.4) {\scalebox{1}[.7]{$T$}};
    \node at (-.8,-.2) {\scalebox{1}[.7]{$\mathbb{R}^n$}};
    \end{scope}
    \end{tikzpicture}

\end{document}
  • Thanks for your help! Could you elaborate a bit more on what exactly you mean with drawing the cylinder as a wireframe? I cant find anything more specific on this. Also, what exactly are these more appealing values you mean? your code creates almost the same image. – Michael A Sep 12 '15 at 14:03
  • I unfortunately am without a working computer at the moment, but here is an example: tex.stackexchange.com/questions/265983/… – Davislor Sep 13 '15 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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