Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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},
]
\addplot file {mydata.dat};
% mystery addplot goes here
\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;

example of the highlighting I'm looking for

Any ideas?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

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,
]
\addplot+[no marks,emphasize=2:4 with blue!10,domain=0:10] {sin(20*x)+5};
\addplot+[no marks,emphasize=28:30 with red!10,domain=25:40] {sin(20*x)+10};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
@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
1  
@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}
\pgfplotsset{compat=newest}

\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
]
\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))};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
This works well, but when I add legend entries, the legend stretches out atop of all the other layers of the plot (making everything underneath invisible). Do you have any idea if there's an easy fix for this? –  Mickel May 23 at 12:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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