如何填充余弦/正弦函数的一部分

如何填充余弦/正弦函数的一部分

我有一张绘制正弦和余弦的图表,并在第一个余弦和 x 轴之间填充。但是,我希望填充从 x=1 开始,而不是从 x=0 开始。但是,如果不更改余弦,我无法让它工作。如何实现?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\draw[very thick] (0,4.5)--(0,0);
\draw[very thick,] (0,0)--(14.5,0);
\draw[thick] (0,0) cos (2,2);
\draw[thick] (2,2) sin (4,4);
\draw[thick] (4,4) cos (6,2);
\draw[thick] (6,2) sin (7,1);
\draw[thick] (7,1) cos (8,2);
\draw[thick] (8,2) sin (10,4);
\draw[thick] (10,4) cos (12,2);
\draw[thick] (12,2) sin (14,0);

\fill[opacity=.2,color=black!30!green] (0,0) cos (2,2) -- (2,0) -- (0,0); 

\end{tikzpicture}
\end{document}

答案1

通过剪辑....

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\draw[very thick] (0,4.5)--(0,0);
\draw[very thick,] (0,0)--(14.5,0);
\draw[thick] (0,0) cos (2,2);
\draw[thick] (2,2) sin (4,4);
\draw[thick] (4,4) cos (6,2);
\draw[thick] (6,2) sin (7,1);
\draw[thick] (7,1) cos (8,2);
\draw[thick] (8,2) sin (10,4);
\draw[thick] (10,4) cos (12,2);
\draw[thick] (12,2) sin (14,0);

\begin{scope}
\clip (1,0)--(2,0)--(2,2)--(1,2)--cycle;
\fill[opacity=.2,color=black!30!green] (0,0) cos (2,2) -- (2,0) -- (1,0); 
\end{scope}
\end{tikzpicture}
\end{document}

答案2

以下是使用 pgpflots 库的另一个建议fillbetween

\documentclass{article}
\usepackage{pgfplots}% loads tikz too
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\pgfdeclarelayer{pre main}

\begin{document}
\begin{tikzpicture}
\pgfsetlayers{pre main,main}
\path[very thick](0,0) edge (0,4.5) edge (14.5,0);
\draw[thick,name path=plot]
  (0,0) cos (2,2) sin (4,4) cos (6,2) sin (7,1)
  cos (8,2) sin (10,4) cos (12,2) sin (14,0);
\path[name path=xaxis](0,0)--(14,0);
\path[name path=clippath]
  (1,0|-current bounding box.south) rectangle (2,0|-current bounding box.north);
\tikzfillbetween[of=plot and xaxis,soft clip={clippath}]{opacity=.2,black!30!green}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容