标记闭合循环区域的中心

标记闭合循环区域的中心

我有这个数字:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepackage{kpfonts}

\newcommand{\func}{ .00038*(x+8)*(x+6)*(x+3)*(x-1)*(x-6)*(x-8) }

\pgfplotsset{
    every x tick label/.style={font=\tiny, below=-2}
    }

\begin{document}    
\begin{tikzpicture}
    \begin{axis}
        [axis lines=middle, axis on top=true,
            xtick={-8,-6,-3,1,6,8}, ytick=\empty, 
            grid=none, 
            every inner x axis line/.style={-},
            every inner y axis line/.style={-},
            xticklabels = {$x_1$, $x_2$,$x_3$,$x_4$,$x_5$,$x_6$,},
            xmin=-8,xmax=8] 
        \addplot[color=black, samples=100,domain=-8:8] { \func } ;
        \addplot[fill=gray!40, domain=-8:-6] { \func } \closedcycle node [xshift=12,yshift=-10] {A};
        \addplot[fill=gray!60, domain=-6:-3] { \func } \closedcycle node [xshift=18,yshift=10] {B};
        \addplot[fill=gray!80, domain=-3:1] { \func } \closedcycle node [xshift=25,yshift=-15] {C};
        \addplot[fill=gray!40, domain=1:6] { \func } \closedcycle node [xshift=32,yshift=40]{D};
        \addplot[fill=gray!60, domain=6:8] { \func } \closedcycle node [xshift=14,yshift=-20] {E};
        \node [draw=black,align=left,font=\scriptsize] at (axis cs:-5,5)
            {Area of A = 3.2\\Area of B = 4.2\\Area of C = 8.6\\Area of D = 24.1\\Area of E = 5.9};
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

这看起来就像我想要的那样,但我希望有更好的方法将标签放在中心。我必须创建类似的东西,我不想每次都摆弄xshiftyshift

答案1

您可以使用特殊节点current path bounding box自动定位您的节点:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

\tikzset{
    declare function={
        f(\x)=.00038*(\x+8)*(\x+6)*(\x+3)*(\x-1)*(\x-6)*(\x-8);
    }
}

\pgfplotsset{
    every x tick label/.style={font=\tiny, below=-2}
    }

\begin{document}    
\begin{tikzpicture}
    \begin{axis}
        [axis lines=middle, axis on top=true,
            xtick={-8,-6,-3,1,6,8}, ytick=\empty, 
            grid=none, 
            every inner x axis line/.style={-},
            every inner y axis line/.style={-},
            xticklabels = {$x_1$, $x_2$,$x_3$,$x_4$,$x_5$,$x_6$,},
            xmin=-8,xmax=8] 
        \addplot[color=black, samples=100,domain=-8:8] { f(x) } ;
        \addplot[fill=gray!40, domain=-8:-6] { f(x) } \closedcycle node at (current path bounding box.center) {A} ;
        \addplot[fill=gray!60, domain=-6:-3] { f(x) } \closedcycle node at (current path bounding box.center) {B};
        \addplot[fill=gray!80, domain=-3:1] { f(x) } \closedcycle node at (current path bounding box.center) {C};
        \addplot[fill=gray!40, domain=1:6] { f(x) } \closedcycle node at (current path bounding box.center) {D};
        \addplot[fill=gray!60, domain=6:8] { f(x) } \closedcycle node at (current path bounding box.center) {E};
        \node [draw=black,align=left,font=\scriptsize] at (axis cs:-5,5)
            {Area of A = 3.2\\Area of B = 4.2\\Area of C = 8.6\\Area of D = 24.1\\Area of E = 5.9};
    \end{axis}
\end{tikzpicture}
\end{document}

相关内容