我试图将此图上的额外 y 刻度标签与虚线对齐,但无法找到方法。我该怎么做?
谢谢。
\usepackage{pgfplots}
\begin{tikzpicture}
\begin{axis}[
standard,
axis lines=middle,
xlabel = $x$,
ylabel = {$y$},
xtick={\empty},
ytick={\empty},
extra x ticks={1,3},
extra x tick labels={$x_0$,$x_0+h$},
extra y ticks={1, sqrt(3), 1.5},
extra y tick labels={$f(x_0)$, $f(x_0+h)$, $f(x_0+h)+h'(x_0+h)$},
]
\addplot [
domain=0:4,
samples=100,
color=black,
]
{.5*x+.5};
\addplot [
domain=0:4,
samples=100,
color=black,
]
{sqrt(x)};
\addplot [dashed,
domain=0:4,
samples=2,
]
{1};
\addplot [dashed,
domain=0:4,
samples=2,
]
{sqrt(3)};
\addplot [dashed,
domain=0:4,
samples=2,
]
{2};
\end{axis}
\end{tikzpicture}
答案1
给出的值extra y ticks
必须是数字。你不能sqrt(3)
在那里给出。所以我们定义一个宏
\pgfmathsetmacro{\tmp}{sqrt(3)}
并使用它。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\pgfmathsetmacro{\tmp}{sqrt(3)}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
%standard, %%<--- ????
axis lines=middle,
xlabel = $x$,
ylabel = {$y$},
xtick={\empty},
ytick={\empty},
extra x ticks={1,3},
extra x tick labels={$x_0$,$x_0+h$},
extra y ticks={1, \tmp, 1.5},
extra y tick labels={$f(x_0)$, $f(x_0+h)$, $f(x_0+h)+h'(x_0+h)$},
]
\addplot [
domain=0:4,
samples=100,
color=black,
]
{.5*x+.5};
\addplot [
domain=0:4,
samples=100,
color=black,
]
{sqrt(x)};
\addplot [dashed,
domain=0:4,
samples=2,
]
{1};
\addplot [dashed,
domain=0:4,
samples=2,
]
{sqrt(3)}; %% or \tmp
\addplot [dashed,
domain=0:4,
samples=2,
]
{1.5};
\end{axis}
\end{tikzpicture}
\end{document}