顶部填充和剪辑标记之间的轴线不协同工作

顶部填充和剪辑标记之间的轴线不协同工作

单独来看,这些选项都有效。但结合起来看:

  1. 字段位于轴上方,不显示 1
  2. 字段 2 和 3 位于轴上方
  3. 线和场的不同颜色

我为每个问题尝试了几种建议的解决方案,但均未成功。感谢您的帮助


\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds, pgfplots.fillbetween}

\pgfplotsset{compat = newest}
\begin{document}
    \begin{tikzpicture}
    \pgfplotsset{
        layers/axis lines on top/.define layer set={axis background,
            axis grid,
            axis ticks,
            axis tick labels,
            pre main,
            main,
            axis lines,
            axis descriptions,
            axis foreground,
        }{/pgfplots/layers/standard}
    }

        \begin{axis}[
            clip marker paths=true,
            xmin = 1, xmax =8,
            ymin = 0, ymax = 2.500, xtick style = {black, thick},
            ytick style = {black, thick},
        ]
       
        \addplot[name path=O,black,mark=no,line width=0.01pt] coordinates  {(1,0) (1,2.5)};
        \addplot[black, opacity=0.4,name path=A,domain=1:8]{1/0.5*(x-1.1)}; 
        \addplot[gray, opacity=0.5,name path=B,domain=1:8]{1/1.8*(x-1.5)}; 
        \addplot[gray!40, opacity=0.5,name path=C,domain=1:8]{1/2.2*(x-2)}; 
        
        \addplot [black, opacity=0.5] fill between [of=A and A];
        \addplot [gray, opacity=0.5] fill between [of=A and B];
        \addplot [gray!40, opacity=0.5] fill between [of=B and C];
        
        \node [anchor=base, align=center ]at (1.6,2.350) {\bf \small 1};
        \node [anchor=center, align=center ]at (4,2.350) {\bf \small 2};
        \node [anchor=center, align=center ]at (6.5,2.350) {\bf \small 3};       
        
        \addplot[blue, ultra thick, mark = *, mark size = 1.5pt] coordinates {(1,1.5) (8,1.5)};
        
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

A

答案1

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
declare function={
f1(\x)=1/0.5*(\x-1.1);
f2(\x)=1/1.8*(\x-1.5);
f3(\x)=1/2.2*(\x-2);
}]
\begin{axis}[
axis on top,
clip marker paths=true,
xmin=1, xmax=8,
ymin=0, ymax=2.5,
xtick style={black, thick},
ytick style={black, thick},
]
\fill[black!60] (1,0) -- (0,2.5) -- (8,{f3(8)}) -- (0,{f3(0)});
\fill[black!40] (1,0) -- (0,2.5) -- (8,{f2(8)}) -- (0,{f2(0)});
\fill[black!20] (1,0) -- (0,2.5) -- (8,{f1(8)}) -- (0,{f1(0)});
\addplot[red, thick, domain=1:8, samples=2] {f1(x)};
\addplot[red, thick, domain=1:8, samples=2] {f2(x)};
\addplot[red, thick, domain=1:8, samples=2] {f3(x)};
\node[font=\bf\small] at (1.6,2.350) {1};
\node[font=\bf\small] at (4.0,2.350) {2};
\node[font=\bf\small] at (6.5,2.350) {3};       
\addplot[blue, ultra thick, mark=*, mark size=1.5pt] coordinates {(1,1.5) (8,1.5)};
\end{axis}
\end{tikzpicture}
\end{document}

带有线性图和填充区域的图形

相关内容