使用 pgfplots 的标签对齐和坐标问题

使用 pgfplots 的标签对齐和坐标问题

我对以下代码有两个问题

\documentclass{scrartcl}

\usepackage{pgfplots}
\pgfplotsset{compat = 1.3}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
  width = \linewidth, height = 11cm, 
  axis lines = middle, 
  x label style = {below, yshift = -4.5pt},
  y label style = {left},
  xmin = 0, xmax =  22, xtick = {0,   5, ...,  20}, minor x tick num = 5, xlabel = {$n$}, 
  ymin = 0, ymax = 450, ytick = {0, 100, ..., 400}, minor y tick num = 1, ylabel = {$w_n$}
]
\addplot+ 
[
  domain = 1 : 20, samples at = {1, ..., 20}, 
  only marks, mark options = {black, fill = black}
] 
{400 * (1 - 0.75^x)};
\addplot [domain = 0 : 21, blue] {400};
\addplot [domain = 0 : 21, red]  {380};
\draw [->, dashed, red] (13, 380) -- (13, 0);
\end{axis}
\end{tikzpicture}   

\end{document}

输出结果如下:

MWE 的输出

  1. 红色虚线应该出现在 n = 13 处。x 坐标不根据轴坐标系进行解释,而 y 坐标则根据轴坐标系进行解释。

  2. x 标签的yshift是通过反复试验找到的。如何将标签与 x 刻度数字底部对齐?

答案1

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth, height=11cm, 
axis lines=middle, 
x label style={at={(xticklabel cs:1.02)}},
y label style={at={(yticklabel cs:1.02)}},
xmin=0, xmax=22,
ymin=0, ymax=450,
xtick distance=5, minor x tick num=5,
ytick distance=100, minor y tick num=1,
xlabel={$n$}, ylabel={$w_n$},
]
\addplot[
domain=1:20,
samples at={1, ..., 20}, 
only marks,
] {400 * (1 - 0.75^x)};
\addplot[blue, domain=0:21, samples=2] {400};
\addplot[red, domain=0:21, samples=2]  {380};
\draw[red, dashed, ->] (13,380) -- (13,0);
\end{axis}
\end{tikzpicture}   
\end{document}

带有散点图和线条的图表

相关内容