带有额外刻度样式的奇怪行为

带有额外刻度样式的奇怪行为

y = 6000 和 y = 5400 处的额外 y 刻度以及 x = 24 和 x = 20.4 处的额外 x 刻度未绘制。

    \documentclass[border=10pt]{standalone}
    \usepackage{pgfplots}
    \usetikzlibrary{intersections}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{major grid style={thick,darkgray}}
    \pgfplotsset{minor grid style={lightgray}}
    \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}[
         restrict x to domain=0:22, xmax=22, xmin=0,
         restrict y to domain=0:6000, ymax=6000, ymin=0,
         x=0.6cm,
         y=0.0012cm,
         axis x line=bottom,
         axis y line=left,
         major tick style=black,
         axis line style = ultra thick,
         tick align=outside,
         tickwidth=0.14cm,
         tick style=thick,
         minor x tick num=4,
         minor y tick num=4,
         grid=both,
         xtick={0,2,...,20},
         ytick={0,1000,...,5000},
         y tick label style={/pgf/number format/1000 sep={}},
         extra x ticks={20.4,20.8,21.2,21.6,22},
         extra x tick label={\null},
         extra y ticks={5200,5400,5600,5800,6000},
         extra y tick label={\null},
         extra x tick style={grid=minor,tick style={draw=none}},
         extra y tick style={grid=minor,tick style={draw=none}},
         xlabel=$x$,ylabel=$f(x)$,
         every axis x label/.style={
         at={(ticklabel* cs:1.01)},anchor=west},
         every axis y label/.style={
         at={(ticklabel* cs:1.01)}, anchor=south},
         samples=3000,
         axis on top=false,
         >=stealth,
         ]
        \addplot [ultra thick,domain=0:20] {f(x)};
        \end{axis}                  
        \end{tikzpicture}
        \end{document}

在此处输入图片描述

答案1

如果我正确理解了您想要实现的目标,那么您就不需要了extra ticks。您只需要相应地定义刻度标签:

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\usepgfplotslibrary{fillbetween}

\pgfplotsset{major grid style={thick,darkgray}}
\pgfplotsset{minor grid style={lightgray}}
\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 x line=bottom,
     axis y line=left,
     major tick style=black,
     axis line style = ultra thick,
     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)$,
     minor y tick num=4,
     grid,
     samples=40,
     >=stealth,
     ]
    \addplot [ultra thick,domain=0:20] {f(x)};
    \end{axis}
    \end{tikzpicture}
    \end{document}

在此处输入图片描述

答案2

xtick={0,2,...,22}, ytick={0,1000,...,6000}应该允许你所需要的

与您的问题无关,也添加\pgfplotsset{compt=1.15}在序言中

\documentclass[border=10pt]{standalone}
 \usepackage{pgfplots}
 \usetikzlibrary{intersections}
  \usepgfplotslibrary{fillbetween}
  \pgfplotsset{compt=1.15} % <-------
  \pgfplotsset{major grid style={thick,darkgray}}
  \pgfplotsset{minor grid style={lightgray}}
\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}[
     restrict x to domain=0:22, xmax=22, xmin=0,
     restrict y to domain=0:6000, ymax=6000, ymin=0,
     x=0.6cm,
     y=0.0012cm,
     axis x line=bottom,
     axis y line=left,
     major tick style=black,
     axis line style = ultra thick,
     tick align=outside,
     tickwidth=0.14cm,
     tick style=thick,
     minor x tick num=4,
     minor y tick num=4,
     grid=both,
     xtick={0,2,...,22}, % <--------
     ytick={0,1000,...,6000}, % <-----------
     y tick label style={/pgf/number format/1000 sep={}},
     extra x ticks={20.4,20.8,21.2,21.6,22},
     extra x tick label={\null},
     extra y ticks={5200,5400,5600,5800,6000},
     extra y tick label={\null},
     extra x tick style={grid=minor,tick style={draw=none}},
     extra y tick style={grid=minor,tick style={draw=none}},
     xlabel=$x$,ylabel=$f(x)$,
     every axis x label/.style={
     at={(ticklabel* cs:1.01)},anchor=west},
     every axis y label/.style={
     at={(ticklabel* cs:1.01)}, anchor=south},
     samples=3000,
     axis on top=false,
     >=stealth,
     ]
    \addplot [ultra thick,domain=0:20] {f(x)};
    \end{axis}                  
    \end{tikzpicture}
    \end{document}

在此处输入图片描述

相关内容