3

Hello dear users of PGFplots, I need to do this figure, a barrel. How can I do this by using PGFplots?

enter image description here

  • You woulsn't do this in pgfplots but rather in TikZ – Tim Hilt Mar 19 at 19:58
9

The only slightly nontrivial task here is to compute the critical angle of the (curved) vertical boundary of the barrel. This answer attempts doing something potentially new. To the best of my knowledge, so far these values have been computed in a "basis-dependent" way, and the expression differs on whether one uses pgfplots, tikz-3dplot or, as in this answer, the perspective library to install the 3d view. Here this gets computed in an independent way, this is what the mysterious function phicrit does. All the rest is standard.

\documentclass[tikz,border=3mm]{standalone}
\definecolor{irk}{RGB}{138,204,183}
\usetikzlibrary{perspective}
\makeatletter
\pgfmathdeclarefunction{phicrit}{0}{%
\begingroup%
\pgfmathparse{atan2(\pgf@xx,\pgf@yx)}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\makeatother
\begin{document}
\begin{tikzpicture}[3d view={110}{15},declare function={rr(\x)=2.3*exp(-\x*\x/45);
    h=3;},>=stealth,semithick]
 \foreach \X [evaluate=\X as \Z using {-2*h+\X*h}] in {1,2,3}
 {\draw[irk,inner color=irk!20!white,outer color=irk!80!white]
  plot[smooth cycle,variable=\t,domain=0:360] 
  ({rr(\Z)*sin(\t)},{rr(\Z)*cos(\t)},\Z);
 \draw[magenta,ultra thick] (0,0,\Z) -- (0,{rr(\Z)},\Z);
 \ifcase\X
 \or
  \fill[magenta] (0,0,\Z) circle[radius=3pt];
  \path (0,0,\Z) node[left=1ex] {$-h$}  -- node[above] {$r$} (0,{rr(\Z)},\Z);
 \or
   \path (0,0,\Z) -- node[above] {$R$} (0,{rr(\Z)},\Z);
 \or
  \fill[magenta] (0,0,\Z) circle[radius=3pt];
  \path (0,0,\Z) node[left=1ex] {$h$} -- node[above] {$r$} (0,{rr(\Z)},\Z);
 \fi 
 \ifnum\X<3
  \draw[dashed,gray] (0,0,\Z) -- ++ (0,0,h);
 \else
  \draw[->] (0,0,\Z) -- ++ (0,0,1) node[pos=1.2] {$z$};
 \fi}
 \draw[irk] plot[variable=\t,smooth,domain=-h:h] 
  ({rr(\t)*sin(phicrit)},{rr(\t)*cos(phicrit)},\t); 
 \draw[irk] plot[variable=\t,smooth,domain=-h:h] 
  ({rr(\t)*sin(phicrit+180)},{rr(\t)*cos(phicrit+180)},\t); 
 \draw[dashed,gray] ({rr(0)},0,0) -- ({-rr(0)-1},0,0)
    (0,{rr(0)},0) -- (0,{-rr(0)-1},0);
 \draw[->] ({rr(0)},0,0) -- ++ (1,0,0) node[pos=1.2] {$x$};  
 \draw[->] (0,{rr(0)},0) -- ++ (0,1,0) node[pos=1.2] {$y$}; 
\end{tikzpicture}
\end{document}

enter image description here

Some of these barrels get visited by marmots, who take care of the honey liquor.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikzlings}
\usetikzlibrary{perspective}
\makeatletter
\pgfmathdeclarefunction{phicrit}{0}{%
\begingroup%
\pgfmathparse{atan2(\pgf@xx,\pgf@yx)}%
\pgfmathsmuggle\pgfmathresult\endgroup%
}%
\makeatother
\begin{document}
\begin{tikzpicture}[declare function={rr(\x)=2.3*exp(-\x*\x/45);
    h=3;},>=stealth,semithick]
 \fill[3d view={110}{15}] plot[variable=\t,smooth cycle,domain=0:360,samples=37] 
  ({rr(h)*sin(\t)},{rr(h)*cos(\t)},h);  
 \marmot[3D,shift={(0,1.5)},scale=1.4,whiskers,teeth]
 \foreach \X [evaluate=\X as \CF using {int(70+20*cos(\X*30+80))}] in {1,...,12}    
 {\draw[3d view={110}{15},top color=brown!\CF!black!80!white,bottom color=brown!\CF!black!80!white] 
  plot[variable=\t,smooth,domain=-h:h] 
  ({rr(\t)*sin(phicrit+\X*15-15)},{rr(\t)*cos(phicrit+\X*15-15)},\t) -- 
  plot[variable=\t,smooth,domain=0:15] 
  ({rr(h)*sin(phicrit+\X*15-15+\t)},{rr(h)*cos(phicrit+\X*15-15+\t)},h) --
  plot[variable=\t,smooth,domain=h:-h] 
  ({rr(\t)*sin(phicrit+\X*15)},{rr(\t)*cos(phicrit+\X*15)},\t) --
  plot[variable=\t,smooth,domain=15:0] 
  ({rr(-h)*sin(phicrit+\X*15-15+\t)},{rr(-h)*cos(phicrit+\X*15-15+\t)},-h)
  -- cycle; }
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |

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.