额外的 x 刻度未以红色绘制(pgfplots)

额外的 x 刻度未以红色绘制(pgfplots)

我想要做的是,额外的 x 刻度的刻度没有用红色绘制。

\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{math}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}[
/pgf/declare function={f(\x)=0.8*\x + 4;}
]
\begin{axis}[
 restrict x to domain=0:30, xmax=30, xmin=0,
 restrict y to domain=0:26, ymax=26, ymin=0,
 x=0.25cm,
 y=0.25cm,
 axis x line = bottom,
 axis y line = left,
 xtick align=outside,
 ytick align=outside,
 tickwidth=0.1cm,
 axis line style = thick,
 major tick style=black,
 xtick={0,2,...,28},
 ytick={0,2,...,24},
 extra x ticks={7,9.6,11.68,13.344},
 extra x tick label={\null},
 extra y tick label={\null},
 extra x tick style={tick align=inside,tick style=red},
 y tick label style={
       /pgf/number format/1000 sep={}
   },
xlabel={$x$ (en milliers)},ylabel={$y$ (en milliers)},
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=2000,
>=stealth,   
]
 \addplot [smooth,domain=0:30,thick] {f(x)} ;
 \addplot [smooth,domain=0:30,dashed,thick,red] {x} ;

  \draw[red,thick](7,{f(7)})--(9.6,{f(7)})--(9.6,{f(9.6)})--(11.68,{f(9.6)})--(11.68,{f(11.68)})--(13.344,{f(11.68)})--(13.344,{f(13.344)})--(14.6752,{f(13.344)});

\node[above,red] at (7,0){$a_0$};
\node[above,red] at (9.6,0){$a_1$};
\node[above,red] at (11.68,0){$a_2$};
\node[above,red] at (13.344,0){$a_3$};  

\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

似乎它被 覆盖了major tick style。但在 中使用major tick style而不是,它就可以正常工作。注意,我还使用了额外的刻度来添加标签,并删除了节点。tick styleextra x tick styleticklabela_N

注释\addplot只是给你一个提示const plot。它产生的效果与你的并不完全相同\draw。哦,2000 个样本比你实际需要的多得多,这只会导致更长的编译时间。

额外蜱虫的特写

\documentclass[tikz, border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{math}
\pgfplotsset{compat=newest}
\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}[
/pgf/declare function={f(\x)=0.8*\x + 4;}
]
\begin{axis}[
 restrict x to domain=0:30, xmax=30, xmin=0,
 restrict y to domain=0:26, ymax=26, ymin=0,
 x=0.25cm,
 y=0.25cm,
 axis x line = bottom,
 axis y line = left,
 xtick align=outside,
 ytick align=outside,
 tickwidth=0.1cm,
 axis line style = thick,
 major tick style=black,
 xtick={0,2,...,28},
 ytick={0,2,...,24},
 extra x ticks={7,9.6,11.68,13.344},
 extra x tick style={
   xticklabel={$a_{\ticknum}$},
   xticklabel style={above,red},
   tick align=inside,
   major tick style={red}
},
y tick label style={
       /pgf/number format/1000 sep={}
   },
xlabel={$x$ (en milliers)},ylabel={$y$ (en milliers)},
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=20,
>=stealth,   
]
 \addplot [domain=0:30,thick] {f(x)} ;
 \addplot [domain=0:30,dashed,thick,red] {x} ;

% \addplot [blue,const plot,samples at={7,9.6,11.68,13.344}] {f(x)};

  \draw[red,thick](7,{f(7)})--(9.6,{f(7)})--(9.6,{f(9.6)})--(11.68,{f(9.6)})--(11.68,{f(11.68)})--(13.344,{f(11.68)})--(13.344,{f(13.344)})--(14.6752,{f(13.344)});


\end{axis}
\end{tikzpicture}
\end{document}

相关内容