1

This code gives the cylinder but I would like half of the part of the cylindrical surface is of one color and another half with another color(half with respect to the top not middle, means when we cut the top circular cross section into half).

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) ellipse (1.25 and 0.5);
\draw (-1.25,0) -- (-1.25,-3.5);
\draw (-1.25,-3.5) arc (180:360:1.25 and 0.5);
\draw [dashed] (-1.25,-3.5) arc (180:360:1.25 and -0.5);
\draw (1.25,-3.5) -- (1.25,0);  
\fill [gray,opacity=0.5] (-1.25,0) -- (-1.25,-3.5) arc (180:360:1.25 and 0.5) -- (1.25,0) arc (0:180:1.25 and -0.5);
\end{tikzpicture}
\end{document}

How can we achieve it?

2

Do you mean something like this?

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) ellipse (1.25 and 0.5);
  \draw (-1.25,0) -- (-1.25,-3.5);
  \draw (-1.25,-3.5) arc (180:360:1.25 and 0.5);
  \draw [dashed] (-1.25,-3.5) arc (180:360:1.25 and -0.5);
  \draw (1.25,-3.5) -- (1.25,0);  
  % 
  \fill [left color = blue, right color = red, opacity=0.5] (-1.25,0) -- (-1.25,-3.5) arc (180:360:1.25 and 0.5) -- (1.25,0) arc (0:180:1.25 and -0.5);
\end{tikzpicture}
\end{document}

enter image description here

If you still want a sharp transition you can replace the fill path with

\fill [opacity=0.5, color = blue] (-1.25,0) -- (-1.25,-3.5) arc
  (180:270:1.25 and 0.5) -- (0,-0.5) node[midway, left, color =
  black]{blue} arc(270:180:1.25 and 0.5) ;

\fill [opacity=0.5, color = red] (1.25,0) -- (1.25,-3.5) arc
  (0:-90:1.25 and 0.5) -- (0,-0.5) node[midway, right, color =
  black]{red} arc(270:360:1.25 and 0.5);

enter image description here

  • Thanks, indeed the second one. Is there a way to write on the cylinder? – Shamina May 3 '18 at 2:03
  • 1
    @Shamina Yes, I just updated my answer – caverac May 3 '18 at 2:35

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.