# pgfplots - highlighting/filling an area in a data file

How does one highlight a specific domain range of a datafile in pgfplots? For example I'd like do do something like:

\begin{tikzpicture}[baseline,trim axis left]
\begin{axis}[
ylabel=frequency,
y unit=\si{\hertz},
]
\end{axis}
\end{tikzpicture}


Where the "% mystery addplot goes here" line would generate something like the three colored boxes below, which could be specified by the domain only. Something like

\addplot [color=colora,fill,domain=2500:3500] file {mydata.dat} \closedcycle;


Any ideas?

-

You can use the axis coordinate systems to get a starting point at the axis border. But it needs to come before the plots to not to be printed over the plots. I've defined an emphasize style for giving a domain-like instruction with a color.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7,
emphasize/.code args={#1:#2with#3}{
\pgfplotsextra{
\draw[fill=#3] ({axis cs:#1,0} |- {axis description cs:0,0})
rectangle ({axis cs:#2,0} |- {axis description cs:0,1});
}
}
}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
ylabel=frequency,
]
\end{axis}
\end{tikzpicture}
\end{document}


-
@percusse: +1, very nice. If you add set layers to the axis options and using Andrew Stacey's code from his answer to “Z-level” in TikZ, you can say \draw[fill=blue!10, on layer=axis background] ... to put the rectangle behind the plots even if it's defined after the \addplot commands. –  Jake Mar 19 '13 at 19:23
@percusse: That approach won't work if the domains of the plots overlap, though. In that case, the second highlight will cover the first plot. –  Jake Mar 19 '13 at 19:31
@Jake Yes but I'm hoping some sane entries. Otherwise it's much better to go manual. For the single plot the key can be repeated btw. –  percusse Mar 19 '13 at 19:33
Wow this is really genius! –  picadora Mar 19 '13 at 19:50
@picadora Have a look at Jake's answer I think that's much better. –  percusse Mar 19 '13 at 19:53

You can choose where to put the highlight (behind everything, behind the plots but above the grid, on top of the plots, ...) if you put set layers in the axis options and use the very clever code from Andrew Stacey's answer to “Z-level” in TikZ.

I've defined a new style highlight=<start>:<end> that highlights the desired area. The drawing style (color, opacity) can be specified using the optional highlight style=<options> key, the layer on which the highlight will be drawn is set using highlight layer=<layer name>:

\addplot+[no marks, highlight=1:4] {rnd/2+sin(deg(x))};
\addplot+[no marks, highlight layer=axis ticks, highlight style={red!20}, highlight=6:9] {rnd/2+cos(deg(x))};


\documentclass{article}
\usepackage{pgfplots}

\pgfkeys{%
/tikz/on layer/.code={
\pgfonlayer{#1}\begingroup
\aftergroup\endpgfonlayer
\aftergroup\endgroup
}
}

\pgfplotsset{
highlight/.code args={#1:#2}{
\fill [every highlight] ({axis cs:#1,0}|-{rel axis cs:0,0}) rectangle ({axis cs:#2,0}|-{rel axis cs:0,1});
},
/tikz/every highlight/.style={
on layer=\pgfkeysvalueof{/pgfplots/highlight layer},
blue!20
},
/tikz/highlight style/.style={
/tikz/every highlight/.append style=#1
},
highlight layer/.initial=axis background
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
set layers,
domain=0:10,
grid=both
]