2

I'm trying to plot this 3D graph of the cylinder,

enter image description here

Initially, I used the code,

\usepackage{pgfplots}
    \pgfplotsset{}

\begin{document}
\tdplotsetmaincoords{70}{30}
 \begin{tikzpicture}[tdplot_main_coords]

\draw[->] (0,3,0) -- (0,-3,0) node[above right] {$x$};

\draw[->] (-3,0,0) -- (3,0,0) node[below right] {$y$};

\draw[->] (0,0,-3) -- (0,0,3) node[below right] {$z$};

\draw plot[variable=\x,domain=0:360,samples=180] ({cos(\x)},-1.25,{sin(\x)});

\draw plot[variable=\x,domain=-45:135,samples=180] ({cos(\x)},1.25,{sin(\x)});
\foreach \x in {135,-45};

{\draw ({cos(\x)},-1.25,{sin(\x)}) -- ({cos(\x)},1.25,{sin(\x)});}

\end{tikzpicture}

But,this turned out to be something like,

enter image description here

I don't mind the coloring of the graph; though the shape needs to be the same as the original one. Any help would be appreciated.

Vincent
  • 20,157

2 Answers2

3

One possible solution is to use shapes.geometric library. Using this library, cylinder is easily obtained.

\documentclass[border=5pt]{standalone}

\usepackage{tikz,tikz-3dplot}
\usetikzlibrary{shapes.geometric}

\begin{document}
\tdplotsetmaincoords{70}{30}
 \begin{tikzpicture}[tdplot_main_coords]

\draw[->] (0,3,0) -- (0,-3,0) node[above] {$x$};
\draw[->] (-3,0,0) -- (3,0,0) node[below right] {$y$};
\draw[->] (0,0,-3) -- (0,0,3) node[below right] {$z$};

\node [cylinder, canvas is xz plane at y=0, draw,
    minimum height=3cm, minimum width=1.5cm] (c) {};

\path[dotted](c.150)  edge [bend left=12] (c.210);
\end{tikzpicture}

\end{document}

enter image description here

3

I guess this is not a real cylinder but a cylinder with an elliptical base. If you only need this one, you can use this code and yscale=0.5.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\tikzset{declare function={
vcrity(\ph,\th)=atan2(sin(\th)*sin(\ph),min(cos(\ph),-1/sqrt(2))*cos(\th));% critical t value y cylinder
vcritz(\ph,\th)=\ph;% critical t value y cylinder
},pics/ycylinder/.style={code={
\tikzset{3d/cylinder/.cd,#1}
\def\pv##1{\pgfkeysvalueof{/tikz/3d/cylinder/##1}}
\pgfmathsetmacro{\vmin}{vcrity(\tdplotmainphi,\tdplotmaintheta)}
\pgfmathsetmacro{\vmax}{\vmin-180}
\path[3d/cylinder/mantle]
  let \p1=($(0,1,0)-(0,0,0)$),\n1={atan2(\y1,\x1)} in
  [shading angle=\n1]
  plot[variable=\t,domain=\vmin:\vmax,smooth]
    ({\pv{r}*cos(\t)},0,{\pv{r}*sin(\t)})
    -- 
    plot[variable=\t,domain=\vmax:\vmin,smooth]
    ({\pv{r}*cos(\t)},\pv{h},{\pv{r}*sin(\t)})
    --cycle;
\pgfmathtruncatemacro{\itest}{sign(cos(\tdplotmainphi))}
\ifnum\itest=-1
    \path[3d/cylinder/top] let \p1=($(0,1,0)-(0,0,0)$),\n1={atan2(\y1,\x1)} in
    [shading angle=\n1]
    plot[variable=\t,domain=0:360,smooth cycle]
    ({\pv{r}*cos(\t)},\pv{h},{\pv{r}*sin(\t)}) ;
\fi
\ifnum\itest=1
    \path[3d/cylinder/top] let \p1=($(0,1,0)-(0,0,0)$),\n1={atan2(\y1,\x1)} in
    [shading angle=\n1]
    plot[variable=\t,domain=0:360,smooth cycle]
    ({\pv{r}*cos(\t)},0,{\pv{r}*sin(\t)}) ;
\fi
}},3d/.cd,cylinder/.cd,r/.initial=1,h/.initial=1,
mantle/.style={draw,left color=blue,right
color=red,middle color=purple,fill opacity=0.5},top/.style={draw,left color=blue!30,right
color=red!30,middle color=purple!10,fill opacity=0.5}}
\begin{document}
\tdplotsetmaincoords{70}{120}
\begin{tikzpicture}[tdplot_main_coords,scale=2,yscale=0.5,transform shape]
 \draw (-2,0,0) -- (2,0,0) (0,-2,0) -- (0,2,0) (0,0,-1) -- (0,0,1);
 \path (0,-2,0) pic{ycylinder={r=1,h=4,top/.append style={draw}}};
\end{tikzpicture}
\end{document}

enter image description here