我正在尝试绘制使用壳法积分旋转体时创建的圆柱体图。我正在尝试使用黑色圆弧剪裁圆柱体的“底部”。
例如我想要这个:
看起来像这样:
显然,我可以在外部程序中仔细检查并编辑它,但我正在编写此绘图脚本,因此这行不通。下面是我正在使用的代码。我尝试clip
在范围内使用,但它只会剪辑用制作的项目\draw
,而不会剪辑\addplot
我正在着色的项目。
此外,如果可能的话,我想将这个阴影区域放在 y 轴后面,但我担心这可能需要另一个问题(fillbetween
并且 pgf 层不能很好地发挥作用)。
\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}[
trig format plots=rad,
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[-] coordinates{(\a+\k*\dx,0) (\a+\k*\dx,{\f(\a+\k*\dx)})};
\addplot[-] coordinates{(-(\a+\k*\dx),0) (-(\a+\k*\dx),{\f(\a+\k*\dx)})};
% Shading
\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}
编辑:为了澄清一些变量定义,我使用此代码将图像生成为动画 .gif。可能有更有效的方法来做到这一点,但目前我只是在试验: 动画示例
答案1
我会逆转 Ti 的角色钾Z 和 pgfplots。你用 I 绘制的部分pgfplots
将用 Ti 绘制钾Z,以及用 Ti 绘制的部分钾ZI 会用 pgfplots 来画图。我也不认为这里需要用fp
pgf,因为 pgf 可以完成所有计算。最重要的是,我强烈推荐不是用 定义全局单字母宏\def
,这很容易导致严重的并发症。也许不是在你这个非常特殊的情况下,但这是一个公共网站,不应该提倡这种做法。经过这次咆哮,这就是我得到的。
\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,
trig format plots=rad,
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,0) arc[start angle=180,end angle=0,x radius=b-dx,y radius={aspect*(b-dx)}]
-- (b-dx,a)
arc[start angle=0,end angle=180,x radius=b-dx,y radius={aspect*(b-dx)}]
-- cycle;
\fill[blue] (-b+dx,a) arc[start angle=180,end angle=0,x radius=b-dx,y radius={aspect*(b-dx)}]
-- (b,a) arc[start angle=0,end angle=180,x radius=b,y radius={aspect*(b)}]
-- cycle;
\endpgfonlayer
\shade[left color=blue, right color=blue, middle color=gray!30]
(-b+dx,0) arc[start angle=-180,end angle=0,x radius=b-dx,y radius={aspect*(b-dx)}]
-- (b-dx,a)
arc[start angle=0,end angle=-180,x radius=b-dx,y radius={aspect*(b-dx)}]
-- cycle;
\shade[left color=blue, right color=blue, middle color=gray!30]
(-b,0) arc[start angle=-180,end angle=0,x radius=b,y radius={aspect*(b)}]
-- (b,a)
arc[start angle=0,end angle=-180,x radius=b,y radius={aspect*(b)}]
-- cycle;
\fill[blue] (-b+dx,a) arc[start angle=-180,end angle=0,x radius=b-dx,y radius={aspect*(b-dx)}]
-- (b,a) arc[start angle=0,end angle=-180,x radius=b,y radius={aspect*(b)}]
-- cycle;
\addplot[<->,color=ClemsonPurple,line width=0.95pt,samples=121,domain=0.5:10] {f(x)};
\addplot[<->,color=ClemsonPurple,dashed,line width=0.95pt,samples=121,domain=-0.5:-10] {f(abs(x))};
\end{axis}
\end{tikzpicture}
\end{document}
参数通过设置declare function
。我不太明白你为什么要计算所有这些数量,但你也可以用这种方法计算它们。