4

I'm displayed three lines on a pgfplot. I want to show a marker for the slope of each line and the difference in height between lines.

enter image description here

I'm showing the slopes by adding triangles under the lines (at a manual position) and placing an annotation inside the triangle (at a manual position). I'm showing the difference between the height of the lines by adding a brace manually as well.

How can I place the braces on the plot and anchor them at a certain point on each line? The triangles can be added at the end of each line declaration, but doing it like this is ok. The braces are put in place manually, which is not sustainable as I create more plots.

I want to avoid using xshift, yshiftand manual adjustment to put the braces,traingles and annotations in the right place. How can I do this?

    \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}
4

Use axis coordinate system axis cs to specify the coordinates.

Updates: The OP wishes to anchor the brace to the last points of each line, without manually giving the coordinates. This is how -- define the y coordinates in the preamble via \pgfmathmacro which invokes \pgfmathparse and uses the result from \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

then use \yvaluei and \yvalueii for the y coordinates in the draw command, as displayed below.

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

enter image description here

Code

\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}
  • Great. But is there any way to anchor the brace to the last points of each line, for example. I need to manually create about 20 of these plots and would like to not have to manually enter the coordinates for the start and end point of the braces each time. – user2146441 Jul 3 '14 at 17:23
  • Sorry for this late response, I was working on your concerns. Yes . First define \pgfmathsetmacro{\yvaluei}{6.26+0.28*25} \pgfmathsetmacro{\yvalueii}{2.66+0.28*25}, in the preamble , then \draw [decorate,decoration={brace,amplitude=5pt,raise=4pt}] (axis cs: 25,\yvaluei)--(axis cs: 25,\yvalueii) node [midway,right,xshift=5pt] {\footnotesize $\beta_1$}; I will incorporate this notion in the solution soon. – Jesse Jul 3 '14 at 18:04
  • Jesse, why not just add another named coordinate (as you have done with x,y,a,b) at the end of each plotted function and use these to locate the brace? – Paul Gessler Jul 3 '14 at 18:30
  • Yes, indeed. that would be simpler. I did not define the a,b label. Did think of that though. But the OP asked me a specific direction to anchor at the end of functions, so I was led to that thinking, trying to find a general solution. – Jesse Jul 3 '14 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.