我有一条爱好曲线,其横轴上方和下方都有区域。我想通过在横轴中反映横轴下方的部分来说明净面积和总面积之间的差异,即取爱好曲线的绝对值。这是我的代码:
\documentclass{article}
\usepackage{pgf,tikz,amsmath,pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{arrows,hobby}
\begin{document}
\pgfdeclarelayer{pre main}
\begin{tikzpicture}[scale=1.0,>=latex, use Hobby shortcut]
\pgfsetlayers{pre main,main}
\draw[name path=f,-] (0,3) .. (3,0) .. (6,-2);
\draw[->,thick] (-1.25,0) -- (7,0) node[above] {\footnotesize $t$};
\draw[->,thick] (-1,-3) -- (-1,4) node[below right]{\footnotesize $v(t)$};
\path[name path=xaxis] (0,0) -- (6,0);
\node[below] at (0,0) {\footnotesize $t_0$};
\node[above] at (6,0) {\footnotesize $t_1$};
\node at (1,1) {\tiny $\text{Area }=A_1$};
\node at (5,-0.75) {\tiny $\text{Area }=A_2$};
\tikzfillbetween[of=f and xaxis,split]{lightgray}
\end{tikzpicture}
\end{document}
答案1
我在评论中提出的想法是使用剪辑仅绘制 x 轴上方的区域,然后绘制并填充路径两次,一次反映在 x 轴上。
那不管用。
它适用于小路但填充忽略了裁剪,填充了轴下方和上方。(这可能是特征图书馆fillbetween
。
因此,这里有另一个解决方案,它利用了您指定的一个点位于 x 轴上的事实。这意味着取指定路径的坐标的绝对值将产生正确的路径(如果您的路径和 x 轴的交点不是指定的坐标,那么这将不起作用)。
\documentclass{article}
%\url{http://tex.stackexchange.com/q/194436/86}
\usepackage{pgf,tikz,amsmath,pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{arrows,hobby}
\makeatletter
\tikzset{abs value/.code={%
\tikz@addmode{%
\pgfsyssoftpath@getcurrentpath\tikz@absvalue@tmppath%
\let\tikz@absvalue@tmppathA=\pgfutil@empty%
\expandafter\tikz@absycoord\tikz@absvalue@tmppath\pgf@stop
\pgfsyssoftpath@setcurrentpath\tikz@absvalue@tmppathA%
}%
}%
}
\def\tikz@absycoord#1#2#3#4{%
\pgfmathparse{abs(#3)}%
\expandafter\def\expandafter\tikz@absvalue@tmppathA\expandafter{\tikz@absvalue@tmppathA#1{#2}}%
\expandafter\expandafter\expandafter\def\expandafter\expandafter\expandafter\tikz@absvalue@tmppathA\expandafter\expandafter\expandafter{\expandafter\tikz@absvalue@tmppathA\expandafter{\pgfmathresult pt}}%
\ifx#4\pgf@stop
\let\@next=\pgfutil@gobble
\else
\let\@next=\tikz@absycoord
\fi
\@next#4%
}
\makeatother
\begin{document}
\pgfdeclarelayer{pre main}
\begin{tikzpicture}[scale=1.0,>=latex, use Hobby shortcut]
\pgfsetlayers{pre main,main}
\draw[name path=f,-] (0,3) .. (3,0) .. (6,-2);
\draw[->,thick] (-1.25,0) -- (7,0) node[above] {\footnotesize $t$};
\draw[->,thick] (-1,-3) -- (-1,4) node[below right]{\footnotesize $v(t)$};
\path[name path=xaxis] (0,0) -- (6,0);
\node[below] at (0,0) {\footnotesize $t_0$};
\node[above] at (6,0) {\footnotesize $t_1$};
\node at (1,1) {\tiny $\text{Area }=A_1$};
\node at (5,-0.75) {\tiny $\text{Area }=A_2$};
\tikzfillbetween[of=f and xaxis,split]{lightgray}
\draw[abs value,magenta,ultra thick,name path=f-above,-] (0,3) .. (3,0) .. (6,-2);
\tikzfillbetween[of=f-above and xaxis,split]{cyan}
\end{tikzpicture}
\end{document}