0

I use the following code to draw a cylinder. How can I fill it with gradient colour to look like the attached image.

\documentclass{beamer}
\setbeamertemplate{navigation symbols}{}
\usepackage{tikz} 
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=1., transform shape]
\node [cylinder,draw=blue,thick,aspect=2.,minimum height=5cm,minimum width=4cm,shape border rotate=0,cylinder uses custom fill, cylinder body fill=blue!30!white,cylinder end fill=blue!20!white] at (0,0){};
\end{tikzpicture}
\end{frame}
\end{document} 

enter image description here

2
  • Thank you. I could not figure out how to apply this code (which is for upright cylinder) to a horizontally oriented cylinder.
    – Hany
    Sep 16, 2018 at 12:49
  • 1
    for example in the answer tex.stackexchange.com/a/10707/36296 leave out the shape border rotate=90 and you'll get a horizontal cylinder. Sep 16, 2018 at 12:54

1 Answer 1

2

Based on https://tex.stackexchange.com/a/10707/36296

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc}
\begin{document}
\begin{tikzpicture}
\node [draw,
  shape=cylinder,
  name=nodename, % Can be defined arbitrarily
  alias=cyl, % Will be used by the ellipse to reference the cylinder
  aspect=3,
  minimum height=3cm,
  minimum width=2cm,
  bottom color=blue,
  top color=blue!70!black,
  middle color=blue!50!white, % Has to be called after left color and middle color
  outer sep=-0.5\pgflinewidth, % to make sure the ellipse does not draw over the lines
%  shape border rotate=90
] at (1,2) {};

\fill [blue!90!black] let
  \p1 = ($(cyl.before top)!0.5!(cyl.after top)$),
  \p2 = (cyl.top),
  \p3 = (cyl.before top),
  \n1={veclen(\x3-\x1,\y3-\y1)},
  \n2={veclen(\x2-\x1,\y2-\y1)}
 in 
  (\p1) ellipse (\n2 and \n1);

\end{tikzpicture}
\end{document}

enter image description here

3

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