平行线上的装饰定位

平行线上的装饰定位

我在 pgfplot 上显示了三条线。我想显示每条线的斜率和线之间的高度差的标记。

在此处输入图片描述

我通过在线下添加三角形(在手动位置)并在三角形内放置注释(在手动位置)来显示斜率。我还通过手动添加括号来显示线条高度之间的差异。

我如何将括号放在图上并将它们固定在每行的某个点上?可以在每行声明的末尾添加三角形,但这样做是可以的。括号是手动放置的,这在我创建更多图时是不可持续的。

我想避免使用xshift, yshift手动调整来将括号、三角形和注释放在正确的位置。我该怎么做?

    \documentclass{article}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.9}

\begin{document}


\begin{tikzpicture}[scale=0.45,trim axis left, trim axis right]
    \begin{axis}[ylabel={$\hat{S(4)}$},xlabel={$S{^*}_{breakeven}$},ylabel style={rotate=-90},domain=1:25, legend style={legend pos=north west,font=\scriptsize}, legend cell align=left]

\addplot+[samples=200,mark=none]                                    {6.26 + 0.28*x}
coordinate[pos=0.5](x) coordinate[pos=0.9] (y) (x)-| (y)
node [midway,xshift=-0.6cm,yshift=0.3cm] {\footnotesize $Slope X$};;

\addplot+[samples=200,mark=none, dotted, thick]             {2.66 + 0.28*x} 
coordinate[pos=0.3](a) coordinate[pos=0.6] (b) (a)-| (b)
node [midway,xshift=-0.5cm,yshift=0.2cm] {\footnotesize $Slope 1$};

\draw [decorate,decoration={brace,amplitude=5pt,raise=4pt},xshift=4pt] 
(225,107) -- (225,73) 
node [midway,xshift=0.6cm] {\footnotesize $\beta_1$};

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

答案1

使用轴坐标系axis cs指定坐标。

更新:OP 希望将括号锚定到每行的最后一个点,而无需手动提供坐标。这就是方法——在序言中定义 y 坐标,通过\pgfmathmacro调用\pgfmathparse并使用 的结果\pgfmathresult

\pgfmathsetmacro{\yvaluei}{6.26+0.28*25}   % the linear function 1, x=25 is the last point 
\pgfmathsetmacro{\yvalueii}{2.66+0.28*25}  % the linear function 2, x=25 is the last point

然后在绘图命令中使用\yvaluei\yvalueii作为 y 坐标,如下所示。

\draw [decorate,decoration={brace,amplitude=5pt,raise=4pt}] 
(axis cs: 25,\yvaluei)--(axis cs: 25,\yvalueii)  
node [midway,right,xshift=5pt] {\footnotesize $\beta_1$};

在此处输入图片描述

代码

\documentclass{article}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.8}

\begin{document}


\begin{tikzpicture}[scale=1,trim axis left, trim axis right]
    \begin{axis}[ylabel={$\hat{S(4)}$},xlabel={$S{^*}_{breakeven}$},ylabel style={rotate=-90},domain=1:25, legend style={legend pos=north west,font=\scriptsize}, legend cell align=left]

\addplot+[samples=200,mark=none]                                    {6.26 + 0.28*x}
coordinate[pos=0.5](x) coordinate[pos=0.9] (y) (x)-| (y)
node [midway,xshift=-0.6cm,yshift=0.3cm] {\footnotesize $Slope X$};;

\addplot+[samples=200,mark=none, dotted, thick]             {2.66 + 0.28*x} 
coordinate[pos=0.3](a) coordinate[pos=0.6] (b) (a)-| (b)
node [midway,xshift=-0.5cm,yshift=0.2cm] {\footnotesize $Slope 1$};

\draw [decorate,decoration={brace,amplitude=5pt,raise=4pt}] 
(axis cs: 24.2,13.3)--(axis cs: 24.2,9.6)  
node [midway,right,xshift=5pt] {\footnotesize $\beta_1$};

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

相关内容