关于顶部的关键轴(pgfplots)

关于顶部的关键轴(pgfplots)

如何让曲线绘制在网格上方?

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
 [extended line/.style={shorten >=-#1,shorten <=-#1},
 /pgf/declare function={f(\x)=  1000*(\x +5)*exp(-0.2*\x);}]
 \begin{axis}
 [xmax=22, xmin=0,
  ymax=6000, ymin=0,
  x=0.6cm,
  y=0.0012cm,
  axis on top=true,
  axis x line=bottom,
  axis y line=left,
  major tick style=black,
  axis line style = ultra thick,
  enlargelimits=false,
  tick align=outside,
  tickwidth=0.14cm,
  tick style=thick,
  minor x tick num=4,
  xtick={0,2,...,22},
  xticklabels = {0,2,...,20,},
  ytick={0,1000,...,6000},
  yticklabels={0,1000,2000,...,5000,},
  xlabel=$x$,ylabel=$f(x)$,
  axis line style={shorten >=-5pt, shorten <=-5pt},
  minor y tick num=4,
  grid=both,
  samples=2000,
  >=stealth,]

\draw[fill=orange!30,draw=orange, very thick](2,0)--(2,4600)--(8,2600)--(8,0)--cycle;
\addplot [ultra thick,domain=0:20] {f(x)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

您需要set layers在图层上添加并绘制绘图axis foreground

  \documentclass[border=10pt]{standalone}
  \usepackage{pgfplots}
  \usetikzlibrary{intersections}
  \usepgfplotslibrary{fillbetween}
  \pgfplotsset{compat=newest}
  \begin{document}
  \begin{tikzpicture}
   [extended line/.style={shorten >=-#1,shorten <=-#1},
   /pgf/declare function={f(\x)=  1000*(\x +5)*exp(-0.2*\x);}]
   \begin{axis}           
   [xmax=22, xmin=0,
    ymax=6000, ymin=0,
    x=0.6cm,
    y=0.0012cm,
    set layers,
    axis on top=true,
    axis x line=bottom,
    axis y line=left,
    major tick style=black,
    axis line style = ultra thick,
    enlargelimits=false,
    tick align=outside,
    tickwidth=0.14cm,
    tick style=thick,
    minor x tick num=4,
    xtick={0,2,...,22},
    xticklabels = {0,2,...,20,},       
    ytick={0,1000,...,6000},
    yticklabels={0,1000,2000,...,5000,},  
    xlabel=$x$,ylabel=$f(x)$,
    axis line style={shorten >=-5pt, shorten <=-5pt},
    minor y tick num=4,
    grid=both,
    samples=2000,
    >=stealth,]

  \draw[fill=orange!30,draw=orange, very thick](2,0)--(2,4600)--(8,2600)--(8,0)--cycle;
  \begin{pgfonlayer}{axis foreground}
  \addplot [ultra thick,domain=0:20] {f(x)}; 
  \end{pgfonlayer}
  \end{axis}                  
  \end{tikzpicture}
  \end{document}

在此处输入图片描述

当然,如果您希望所有图都位于前台,那么这axis on top可能不是理想的关键,您可能希望使用另一个图层排序方案。

相关内容