# Clipping a fill between region in pgfplots

I'm trying to create a drawing of the cylinder that is created when integrating the solid of revolution using the shell method. I'm trying to clip the "bottom" of the cylinder using the black arc.

E.g., I want this: To look like this: Obviously I could go through and edit this carefully in an external program, but I'm scripting this drawing, so that won't work. Below is the code I'm using. I've tried using clip inside of a scope, but it only clips items made with \draw and doesn't clip the \addplot that I'm shading.

Also, if at all possible, I would like to put this shaded region behind the y-axis, but I'm afraid that may need to another question (fillbetween and pgf layers don't play nicely).

\documentclass{standalone}
\usepackage{tikz, pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{fp}
\definecolor{ClemsonPurple}{RGB}{82,45,128}

\def\f(#1){((#1-1)*(#1-5)*(#1-9)/15+3)}
\def\a{2}
\def\b{8}
\def\k{10}
\def\N{10}
\FPsub\kmo\k 1
\FPsub\LEN\b\a            %% \LEN=\b-\a
\FPsub\NMO\N 1            %% \NMO=\N-1
\FPdiv\dx\LEN\N           %% \del=\LEN/\n

\tikzset{>=stealth}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[scale=1]
\begin{axis}[
axis lines=center,
axis line style={-},
xmin=-10, xmax=10,
ymin=-2, ymax=7,
xtick={-10,-8,...,10},
ytick={-10,-8,...,10},
enlargelimits={abs=0.75},
ticklabel style={font=\footnotesize,inner sep=0.5pt,fill=white,opacity=0.0, text opacity=1},
every axis plot/.append style={line width=0.4pt, color=black, samples=200}
]
% Inner rim
\addplot[domain=0:pi, name path global = bkInnRim] ({(\a+\kmo*\dx)*cos(x)},{\f(\a+\k*\dx)+(\a+\kmo*\dx)*sin(x)/\b});
\addplot[domain=-pi:0, name path global = frInnRim] ({(\a+\kmo*\dx)*cos(x)},{\f(\a+\k*\dx)+(\a+\kmo*\dx)*sin(x)/\b});
% Outer rim
\addplot[domain=0:pi, name path global = topBkRim] ({(\a+\k*\dx)*cos(x)},{\f(\a+\k*\dx)+(\a+\k*\dx)*sin(x)/\b});
\addplot[domain=-pi:0, name path global = topFrRim] ({(\a+\k*\dx)*cos(x)},{\f(\a+\k*\dx)+(\a+\k*\dx)*sin(x)/\b});
% Bottom rim
\addplot[domain=-pi:0, name path global = botFrRim] ({(\a+\k*\dx)*cos(x)},{(\a+\k*\dx)*sin(x)/\b});
\addplot[domain=0:pi, name path global = botBkRim] ({(\a+\kmo*\dx)*cos(x)},{(\a+\kmo*\dx)*sin(x)/\b});
% Sides
\addplot [thick, fill opacity=0.5, line width=2pt, shading=axis, left color=blue, right color=blue, middle color=gray!10]
fill between [of=topFrRim and botFrRim];
\addplot [thick, color=blue, fill=blue, fill opacity=1.0, line width=2pt]
fill between[of=topFrRim and frInnRim];
\addplot [thick, color=blue, fill=blue, fill opacity=1.0, line width=2pt]
fill between[of=topBkRim and bkInnRim];
\draw[<->, ClemsonPurple, line width=0.95pt] plot[samples=120,domain=0.5:10] (\x,{\f(\x)});
\draw[<->, dashed, ClemsonPurple, line width=0.95pt] plot[samples=120,domain=0.5:10] (-\x,{\f(\x)});
\addplot [thick, color=blue, shading=axis, left color=blue, right color=blue, middle color=gray!10, fill opacity=0.9, line width=2pt] fill between[of=frInnRim and bkInnRim];
\end{axis}
\end{tikzpicture}
\end{document}


EDIT: For clarification on some of my variable definitions, I'm using this code to generate images into an animated .gif. There's probably a much more efficient way to do this, but at this point I'm just experimenting: Example animation

• Do you really need pgfplots for that? I'd understand it if you were to use its 3d-like features but you don't so I am wondering why you do not just use TikZ. Also please do never use single-letter definitions like \def\a{2}. Use declare function or whatever but not these. – user194703 Jan 29 '20 at 23:24
• The main reason I'm using pgfplots is I like the way that the axis look and this is my preferred method for drawing graphs of functions, but I do realize I could achieve something similar using TikZ. Also, what is your reasoning for not defining a single letter definition? I'm not doubting that it's bad practice, but I'm unaware of what it may break. – pwesterbaan Jan 30 '20 at 0:05

I would reverse the role of TikZ and pgfplots. The parts you draw with pgfplots I would draw with TikZ, and the parts you draw with TikZ I would draw with pgfplots. I also do not see the need for using fp here, pgf can do all computation. And, most importantly, I strongly recommend not to define global single letter macros with \def, this can easily lead to major complications. Maybe not in your very specific case, but this is a public site and one should not promote this practice. After this rant, here is what I got.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\definecolor{ClemsonPurple}{RGB}{82,45,128}
\tikzset{>=stealth}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}[scale=1,declare function={f(\x)=((\x-1)*(\x-5)*(\x-9)/15+3);
a=1.8;b=8;dx=0.5;aspect=0.15;}]
\begin{axis}[set layers=standard,
axis lines=center,
axis line style={-},
xmin=-10, xmax=10,
ymin=-2, ymax=7,
xtick={-10,-8,...,10},
ytick={-10,-8,...,10},
enlargelimits={abs=0.75},
ticklabel style={font=\footnotesize,inner sep=0.5pt,fill=white,opacity=0.0, text opacity=1},
every axis plot/.append style={line width=0.4pt, color=black, samples=200}
]
\pgfonlayer{axis background}
\shade[left color=blue, right color=blue, middle color=blue!50]
-- (b-dx,a)
-- cycle;
-- cycle;
\endpgfonlayer
\shade[left color=blue, right color=blue, middle color=gray!30]
-- (b-dx,a)
-- cycle;
\shade[left color=blue, right color=blue, middle color=gray!30]
-- (b,a)
-- cycle;

The parameters are set via declare function. I did not quite understand why you are computing all these quantities, but you can compute them that way, too.
• The code that I was using was part of a bash script. I passed the definition of \k and \N into the document, generated the .pdf and converted that individual file to a .png and then created an animated .gif from the .png's. I realize this is probably wildly inefficient, but I'm doing this as practice and just to see what I can do. Also, some of the choices I made were just due to my ignorance of what TikZ and pgf can do. – pwesterbaan Jan 30 '20 at 15:31
• @pwesterbaan You can do on your machine whatever you find convenient. But please understand that posting this in an internet site where others get their inspirations from has a different quality. BTW, you can get the definitions in with the catchfile package. – user194703 Jan 30 '20 at 15:40