![如何标记 pgfplots fillbetween 库中的区域](https://linux22.com/image/311779/%E5%A6%82%E4%BD%95%E6%A0%87%E8%AE%B0%20pgfplots%20fillbetween%20%E5%BA%93%E4%B8%AD%E7%9A%84%E5%8C%BA%E5%9F%9F.png)
是否有任何简单的方法可以将标签$A$
大致放置在由 pgfplots fillbetween 库创建的区域的“中心”处?
比如以下这个例子:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:5]
\addplot[name path=A] gnuplot {sin(x)};%
\addplot[name path=B] gnuplot {cos(x)};%
\addplot[opacity=0.5] fill between[of=A and B,
split,
every segment no 0/.style={yellow},
every segment no 1/.style={red},
every segment no 2/.style={green},
];
%% Choosing the coordinates manually is annoying:
\node at (axis cs:2,0.3) {$A_2$};
\node at (axis cs:0.2,0.5) {$A_1$};
\node at (axis cs:4.5,-0.6) {$A_1$};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我认为使用重心坐标可能是一种选择,尽管需要做一些工作。在这种情况下,我在每个情节线的开始和结束处添加坐标,并命名交叉点。节点实际上并不位于区域的中间,而是位于所用坐标的中心。
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:5]
\addplot[name path=A] gnuplot {sin(x)} coordinate[pos=0] (As) coordinate [pos=1] (Ae);
\addplot[name path=B] gnuplot {cos(x)} coordinate[pos=0] (Bs) coordinate [pos=1] (Be);
\addplot[opacity=0.5] fill between[of=A and B,
split,
every segment no 0/.style={yellow},
every segment no 1/.style={red},
every segment no 2/.style={green},
];
\path [name intersections={of=A and B,name=i}];
\node at (barycentric cs:As=1,Bs=1,i-1=1) {$A_1$};
\node at (barycentric cs:i-1=1,i-2=1) {$A_2$};
\node at (barycentric cs:i-2=1,Ae=1,Be=1) {$A_1$};
\end{axis}
\end{tikzpicture}
\end{document}