![pgfplots:如何注释间隔?](https://linux22.com/image/353264/pgfplots%EF%BC%9A%E5%A6%82%E4%BD%95%E6%B3%A8%E9%87%8A%E9%97%B4%E9%9A%94%EF%BC%9F.png)
答案1
对于注释的文本,您可以使用decorations.text
tikz
库。
为了风格化,您可以在命令中添加所需的任何选项\draw
,这里有一个示例:
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=5
]
\addplot[mark=none,blue] {x^2};
\draw[red, dashed] (2, \pgfkeysvalueof{/pgfplots/ymin}) -- (2, \pgfkeysvalueof{/pgfplots/ymax});
\draw[green, thick, dotted] (4, \pgfkeysvalueof{/pgfplots/ymin}) -- (4, \pgfkeysvalueof{/pgfplots/ymax});
\draw[blue, very thick, {Stealth}-{Stealth}, postaction={decoration={raise=3pt, text along path, text={some text},text align=center}, decorate}] (2,5) -- (4,5);
\end{axis}
\end{tikzpicture}
\end{document}
编辑:
根据 OP 的要求,我创建了垂直线的样式\myline
(以及\myarrow
间隔箭头的样式)。
此外,我有点太过狂热了tikz
。正如 Torbjørn T. 正确指出的那样,text along path
对于直线来说,使用 a 有点过头了。您只需将一个节点放在路径上方,无需使用库decorations.text
。
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta}
\tikzset{%
myline/.style = {green, very thick, dashed},
myarrow/.style = {blue, very thick, {Stealth}-{Stealth}},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=5
]
\addplot[mark=none,blue] {x^2};
\draw[myline] (2, \pgfkeysvalueof{/pgfplots/ymin}) -- (2, \pgfkeysvalueof{/pgfplots/ymax});
\draw[myline] (4, \pgfkeysvalueof{/pgfplots/ymin}) -- (4, \pgfkeysvalueof{/pgfplots/ymax});
\draw[myarrow] (2,5) -- node[above] {some text} (4,5);
\end{axis}
\end{tikzpicture}
\end{document}
第二次编辑:
我创建了一个pic
具有三个属性args
:初始值x
、最终值x
和箭头高度y
(最后一个可以用一个固定值代替,如果它始终相同,则修改pic
为仅使用两个args
)。
\documentclass[border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{arrows.meta}
\tikzset{%
myline/.style = {green, very thick, dashed},
myarrow/.style = {blue, very thick, {Stealth}-{Stealth}},
pics/myint/.style n args={3}{code={%
\draw[myline] (#1, \pgfkeysvalueof{/pgfplots/ymin}) -- (#1, \pgfkeysvalueof{/pgfplots/ymax});
\draw[myline] (#2, \pgfkeysvalueof{/pgfplots/ymin}) -- (#2, \pgfkeysvalueof{/pgfplots/ymax});
\draw[myarrow] (#1,#3) -- node[above] {some text} (#2,#3);
}},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=5
]
\addplot[mark=none,blue] {x^2};
\pic {myint={2}{4}{5}};
\end{axis}
\end{tikzpicture}
\end{document}
当然,使用\pic {myint={2}{4}{5}};
,输出与前一个相同。