# TikZ Plotting: Braces between tickmarks

What is the best way to draw braces between tick marks? It would be convenient to assign nodes at tick marks, is this doable?

The best method for placement that I have right now is a guess-and-check.

MWE:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{patterns,decorations.pathreplacing}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis x line = middle,
axis y line = middle,
xticklabels={}
]
\addplot [domain=-1:5] { -x * (x-4) };
\end{axis}
\draw [thick,decoration={brace,mirror},decorate] (1.2,3) -- (5.7,3) node[midway,below,yshift=-.1cm] {4};
\end{tikzpicture}

\end{document}


-
Update: In my actual implementation (not the MWE), my brace fell out of the graphing area and was clipped off. However, I did not want to use clip=false, since my graph would have extended out as well. To fix all this, I used after end axis/.code={\draw [decoration={brace},decorate] (A) -- (B) node {yadda}; in the argument of \begin{axis}, and \coordinate (A) at (axis cs:pi,0);, \coordinate (B) at (axis cs:5*pi/3,0); just before \end{axis}. I hope this helps anyone else who might run into the same situation! –  user2868156 May 14 at 6:51
(Special thanks to Jake's answer in Annotate Plots in TikZ/PGFplots.) –  user2868156 May 14 at 6:59

Below I present three levels of improvement:

1. You can move the \draw inside the axis environment and use the axis cs coordinate system which gives you access to the coordinates of the plot; also, you can use the raise option to shift the brace:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns,decorations.pathreplacing}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis x line = middle,
axis y line = middle,
xticklabels={}
]
\addplot [domain=-1:5] { -x * (x-4) };
\draw [thick,decoration={brace,mirror,raise=5pt},decorate]
(axis cs:0,0) --
node[below=7pt] {4}
(axis cs:4,0);
\end{axis}
\end{tikzpicture}

\end{document}


2. You don't have to know beforehand the coordinates for the intersection points; you can let the intersections library do the calculations for you:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{patterns,decorations.pathreplacing,intersections}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis x line = middle,
axis y line = middle,
xticklabels={},
samples=100
]
\addplot[name path=curve,domain=-1:5] { -x * (x-4) };
\draw[
name intersections={of=curve and line, by={a,b}},
thick,
decoration={brace,mirror,raise=5pt},decorate
]
(a) --
node[below=7pt] {4}
(b);
\end{axis}
\end{tikzpicture}

\end{document}

3. Finally you can also use the calc library and the lengthconvert package, for example, so the length (4) is also automatically calculated:

\documentclass{article}
\usepackage{lengthconvert}
\usepackage{pgfplots}
\usetikzlibrary{patterns,decorations.pathreplacing,intersections,calc}
\pgfplotsset{compat=1.10}

\begin{document}

\begin{tikzpicture}
\begin{axis}[x=1cm,
axis x line = middle,
axis y line = middle,
xticklabels={},
samples=100
]
\addplot[name path=curve,domain=-1:5] { -x * (x-4) };
\draw[
name intersections={of=curve and line, by={a,b}},
decoration={brace,mirror,raise=5pt},
decorate
]
let \p1 = ($(a) - (b)$),
\n2 = {veclen(\x1,\y1)}
in
(a) --
node[below=7pt] {\Convert[precision=1,number-only=true]{\n2}}
(b);
\end{axis}
\end{tikzpicture}

\end{document}

-
Ah brilliant, thank you! –  user2868156 May 14 at 4:01