我使用以下代码来填充函数和 x 轴之间的区域颜色,并且它可以正常工作。
\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\foreach \x in {1.000,1.001,...,4.500}
\draw[purple!15!white] (\x,{0})--(\x, {0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2});
\draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
\node[below left](o) at (0,0){$0$};
\draw[ultra thick,purple,smooth,domain=0.5:5,samples=257,]
plot (\x, {0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2});
\end{tikzpicture}
\end{document}
我怎样才能做同样的事情,使用贝塞尔曲线(例如 \draw[ultra thick, purple,smooth] (0.5,1.5)..controls(2,-0.5) 和 (3.5,4.5)..(5,2.5);)而不是 0.5余弦(度(0.5pi*\x)+0.5*pi)+2?
答案1
用大约 3500 条细条纹填充一个区域是我最后的手段。只是没有深入研究 PGFPlots'fillbetween
图书馆(看填充)和整个权力spath3
图书馆我们可以通过使用适当的来做到这一点\clip
。
是current subpath start
一个动态坐标,它引用当前子路径的起点——感谢 Caption Obvious——这是最后一个移至操作降落。这就是cycle
我们要去的地方。
然而,我们不想直接循环到该点,而是想将其下方的所有内容都包含在内,直到是= 0.
该坐标(0,0 -| current subpath start)
为的基点current subpath start
。
代码
\documentclass[a4paper, 10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[
scale=1.2,
declare function={
trig(\x)=.5*cos(deg(.5*pi*\x)+.5*pi)+2;
}]
\fill[purple!15] plot[smooth, domain=1:4.5, samples=257] (\x, trig \x)
|- (0,0-|current subpath start) -- cycle;
\draw[very thick, ->] (-1,0) -- (6,0)node[below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4)node[left] {$y$};
\node[below left] {$0$};
\draw[ultra thick, purple, smooth, domain=0.5:5, samples=257]
plot (\x, trig \x);
\end{tikzpicture}
\begin{tikzpicture}[
scale=1.2,
my curve/.style={
insert path={(0.5,1.5)..controls(2,-0.5) and (3.5,4.5)..(5,2.5)}}
]
\begin{scope}
\clip (1,0) rectangle (4.5, 4); % the y value of 4 needs to be carefully chosen
\fill[purple!15, my curve] |- (0,0-|current subpath start) -- cycle;
\end{scope}
\draw[very thick, ->] (-1,0) -- (6,0) node[below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4) node[left] {$y$};
\node[below left] {$0$};
\draw[ultra thick, purple, my curve];
\end{tikzpicture}
\end{document}
输出
答案2
\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\begin{scope}
\clip (0.5,0) -- (0.5,1.5) ..controls (2,-0.5) and (3.5,4.5).. (5,2.5) |- cycle;
\foreach \x in {1.000,1.001,...,4.500}
\draw[purple!15!white] (\x,{0}) -- ++(0,4);
\end{scope}
\draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
\node[below left] (o) at (0,0){$0$};
\draw[ultra thick, purple, smooth]
(0.5,1.5) ..controls (2,-0.5) and (3.5,4.5).. (5,2.5);
\end{tikzpicture}
\end{document}
答案3
由于plot
可以用于路径,这里有一个简单的解决方案:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\fill[purple!15!white](1,0)
-- plot[smooth,domain=1:4.5,samples=257](\x,{0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2})
-- (4.5,0) -- cycle;
\draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
\node[below left](o) at (0,0){$0$};
\draw[ultra thick,purple,smooth,domain=0.5:5,samples=257,]
plot (\x, {0.5*cos(deg(0.5*pi*\x)+0.5*pi)+2});
\end{tikzpicture}
\end{document}
两个图之间使用相同的方法(declare function
为了易读性使用):
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[scale=1.2]
\tikzset{
declare function={
f1(\x) = 0.5*cos(deg(0.5*pi*\x)+0.5*pi)+1;
f2(\x) = 0.5*cos(deg(2*pi*\x)+0.5*pi)+3;
},
samples=256,
}
\fill[purple!15!white] plot[smooth,domain=1:4.5](\x,{f1(\x)})
-- (4.5,0) -- (1,0) -- cycle;
\fill[cyan!15!white] plot[smooth,domain=1.5:4](\x,{f1(\x)})
-- plot[smooth,domain=4:1.5](\x,{f2(\x)}) -- cycle;
\draw[very thick, ->] (-1,0) -- (6,0)node[pos=1,below]{$x$};
\draw[very thick, ->] (0,-1) -- (0,4)node[pos=1,left]{$y$};
\node[below left](o) at (0,0){$0$};
\draw[ultra thick,purple] plot[smooth,domain=0.5:5] (\x,{f1(\x)});
\draw[ultra thick,cyan] plot[smooth,domain=0.5:5] (\x,{f2(\x)});
\end{tikzpicture}
\end{document}