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.

I followed the tips on highlighting in data plots at http://www.howtotex.com/tips-tricks/highlighting-in-tikz-plots/

The highlighting does not work well with \addlegendentry.

A MWE is shown below.

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{external}
\tikzexternalize
%\tikzset{external/mode=list and make}
%\tikzset{external/check=diff}
\tikzset{external/force remake}
%\tikzsetexternalprefix{figure-build/, up to date check=md5, force remake}

\pgfplotsset{compat=1.8}
%\pgfplotsset{compat=newest} % necessary for new features

% start code to enable highlighting
\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 % default color of highlighted area
    },
    /tikz/highlight style/.style={
        /tikz/every highlight/.append style=#1
    },
    highlight layer/.initial=axis background    % default depth
}

\usepackage{filecontents}

\begin{filecontents}{data}
1790 -0.002923 0.005438 0.005438 0.001646 0.010007 0.004569 4.647375 4.720160 4.721806 0.018075 0.005438
1791 -0.000842 0.002737 0.002737 0.001249 0.004828 0.002091 4.651944 4.730167 4.731415 0.020812 0.002737
1792 0.001305 0.002307 0.002307 0.002377 0.003379 0.001072 4.654035 4.734995 4.737372 0.023119 0.002307
1793 -0.005790 0.006761 0.006761 0.000710 0.013261 0.006500 4.655107 4.738374 4.739084 0.029880 0.006761
1794 -0.001449 0.002468 0.002468 0.000567 0.004484 0.002016 4.661608 4.751634 4.752201 0.032348 0.002468
1795 0.004092 0.004121 0.004121 0.005267 0.005296 0.001175 4.663623 4.756118 4.761385 0.036469 0.004121
1796 0.001135 0.001165 0.001165 0.003397 0.003427 0.002262 4.664798 4.761414 4.764811 0.037634 0.001165
1797 -0.000894 -0.000894 -0.000861 0.000118 0.000151 0.001012 4.667060 4.764841 4.764959 0.036773 -0.000861
1798 -0.002721 -0.002721 -0.002688 0.000569 0.000602 0.003290 4.668073 4.764992 4.765561 0.034085 -0.002688
1799 -0.003568 -0.003568 -0.003541 0.000188 0.000215 0.003756 4.671363 4.765594 4.765782 0.030544 -0.003541
1800 -0.000879 -0.000879 -0.000848 0.000709 0.000740 0.001588 4.675119 4.765809 4.766518 0.029696 -0.000848
1801 0.001929 0.001961 0.001961 0.003127 0.003159 0.001198 4.676707 4.766549 4.769676 0.031657 0.001961
1802 -0.000059 -0.000059 -0.000020 0.001011 0.001050 0.001070 4.677906 4.769708 4.770719 0.031637 -0.000020
1803 -0.000024 0.000017 0.000017 0.001097 0.001138 0.001121 4.678976 4.770758 4.771855 0.031654 0.000017
1804 -0.001896 -0.001896 -0.001859 0.000068 0.000105 0.001964 4.680097 4.771896 4.771964 0.029795 -0.001859
1805 -0.002589 -0.002589 -0.002555 0.000066 0.000100 0.002655 4.682062 4.772001 4.772067 0.027240 -0.002555
1806 -0.000969 -0.000969 -0.000937 0.000032 0.000064 0.001001 4.684717 4.772101 4.772133 0.026303 -0.000937
1807 -0.001087 -0.001087 -0.001051 0.001283 0.001319 0.002370 4.685718 4.772165 4.773449 0.025252 -0.001051
1808 -0.001766 -0.001766 -0.001728 0.000454 0.000492 0.002220 4.688088 4.773484 4.773938 0.023524 -0.001728
1809 -0.000970 -0.000970 -0.000938 0.000032 0.000064 0.001002 4.690308 4.773977 4.774009 0.022586 -0.000938
1810 -0.000526 -0.000526 -0.000490 0.000516 0.000552 0.001042 4.691310 4.774041 4.774557 0.022096 -0.000490
1811 -0.004600 -0.004600 -0.004559 0.000362 0.000403 0.004962 4.692353 4.774593 4.774954 0.017537 -0.004559
1812 -0.005381 -0.005381 -0.005347 0.000323 0.000357 0.005704 4.697314 4.774996 4.775318 0.012190 -0.005347
1813 -0.005619 -0.005619 -0.005587 0.000032 0.000064 0.005651 4.703018 4.775353 4.775385 0.006603 -0.005587
1814 -0.000906 -0.000906 -0.000869 0.000101 0.000138 0.001007 4.708669 4.775417 4.775518 0.005734 -0.000869
1815 0.000603 0.000641 0.000641 0.001719 0.001757 0.001116 4.709676 4.775555 4.777273 0.006375 0.000641
1816 -0.001316 -0.001316 -0.001278 0.000086 0.000124 0.001402 4.710791 4.777312 4.777397 0.005097 -0.001278
1817 -0.001534 -0.001534 -0.001494 0.000304 0.000344 0.001838 4.712193 4.777436 4.777739 0.003603 -0.001494
1818 -0.001896 -0.001896 -0.001855 0.000077 0.000118 0.001973 4.714032 4.777780 4.777857 0.001748 -0.001855
1819 -0.001642 -0.001642 -0.001608 0.000125 0.000159 0.001767 4.716004 4.777897 4.778022 0.000140 -0.001608
1820 -0.001103 -0.001103 -0.000141 0.000358 0.001320 0.001461 4.717771 4.778056 4.778414 0.000000 -0.000140
\end{filecontents}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
When highlight does not work with addlegendentry, the display is fine.
\vspace{0.5cm}

\tikzsetnextfilename{myfig1}
\begin{tikzpicture}
    \begin{axis}[
            set layers,
            xmode=linear,
            ymode=linear,
            axis x line*=bottom,
            axis y line*=left,
            tick label style={font=\small},
            grid=both,
            tick align=outside, 
            tickpos=left,
            xlabel=x,
            ylabel=y,
            width=0.45\textwidth,
            height=0.4\textwidth,
        ]
        \addplot [raw gnuplot, color=red] gnuplot {
                plot "data.tex" using 1:3 w lines;
        };
        \addlegendentry{z1};
        \addplot [raw gnuplot, color=blue] gnuplot {
                plot "data.tex" using 1:4 w lines;
        };
        \addlegendentry{z2};
        \addplot+[%
                no marks, raw gnuplot, color=green,
                % first highlighted area (in blue by default) 
                highlight=1790:1800,          % range
                % second highlighted area 
                highlight=1805:1807,              % range
        ] gnuplot {
                plot "data.tex" using 1:11 w lines;
        };
        %\addlegendentry{z3}
        \addplot+[%
                no marks, raw gnuplot, color=yellow,
                highlight style={red!20}, % color
                % first highlighted area
                highlight=1810:1820,          % range
        ] gnuplot {
                plot "data.tex" using 1:12;
        };
        %\addlegendentry{z4}
    \end{axis}
\end{tikzpicture}

\vspace{1.5cm}
When highlight works with addlegendentry, the display is bad.
\vspace{0.5cm}

\tikzsetnextfilename{myfig2}
\begin{tikzpicture}
    \begin{axis}[
            set layers,
            xmode=linear,
            ymode=linear,
            axis x line*=bottom,
            axis y line*=left,
            tick label style={font=\small},
            grid=both,
            tick align=outside, 
            tickpos=left,
            xlabel=x,
            ylabel=y,
            width=0.45\textwidth,
            height=0.4\textwidth,
        ]
        \addplot [raw gnuplot, color=red] gnuplot {
                plot "data.tex" using 1:3 w lines;
        };
        \addlegendentry{z1};
        \addplot [raw gnuplot, color=blue] gnuplot {
                plot "data.tex" using 1:4 w lines;
        };
        \addlegendentry{z2};
        \addplot+[%
                no marks, raw gnuplot, color=green,
                % first highlighted area (in blue by default) 
                highlight=1790:1800,          % range
                % second highlighted area 
                highlight=1805:1807,              % range
        ] gnuplot {
                plot "data.tex" using 1:11 w lines;
        };
        \addlegendentry{z3}
        \addplot+[%
                no marks, raw gnuplot, color=yellow,
                highlight style={red!20}, % color
                % first highlighted area
                highlight=1810:1820,          % range
        ] gnuplot {
                plot "data.tex" using 1:12;
        };
        \addlegendentry{z4}
    \end{axis}
\end{tikzpicture}
\end{document}

This file main.tex is compiled by

pdflatex -shell-escape main.tex

Whenever, the highlight is used in \addplot, the legend becomes a large box which covers the main data plot. Do you have a suggestion to deal with this case? Thank you.

share|improve this question
    
Could you turn the code snippets into a single, minimal example document that can just be copy/pasted? –  Jake Aug 14 '13 at 15:53
1  
I have revised my question. Would you help me to take a look at it? Thank you. –  Novice User Aug 14 '13 at 18:03
    
Your MWE is not working on my system... –  Enthusiastic Student Oct 22 at 9:45

1 Answer 1

I had the same error, I found a workaround by removing all highlight keywords from the \addplots and then adding one \addplots at the end printing an invisible plot with forget plot, draw=none, no marks, this time with all the highlights options and without legend.

The bug appears in this MWE modified from here :

enter image description here

\documentclass[tikz]{standalone}
\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
highlight=1:4] {sin(deg(x))};
\addlegendentry{legend1}
\addplot+[no marks,
%highlight 2
highlight layer=axis ticks,
highlight style={red!20},
highlight=6:9] {cos(deg(x))};
\addlegendentry{legend2}

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

While this gives a correct result :

enter image description here

\documentclass[tikz]{standalone}
\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] {sin(deg(x))};
\addlegendentry{legend1}
\addplot+[no marks] {cos(deg(x))};
\addlegendentry{legend2}
\addplot+[forget plot,draw=none, no marks,
%highlight 1
highlight=1:4,
%highlight 2
highlight layer=axis ticks,
highlight style={red!20},
highlight=6:9
] coordinates {(0,0)};
%% NO LEGEND HERE !
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Welcome to tex.sx! Note that compilable MWEs are as useful in answers as they are in questions, at least to point out unambiguously where the changes have to happen. –  T. Verron Nov 24 at 12:43
    
I followed your advices and added MWEs, Thanks –  XL64 Nov 24 at 13:18
    
Hm, I don't understand: why did you have to pull a MWE from somewhere else? Your first version was using the MWE from the question, why didn't you just put back the parts (preamble, and parts of the document before and after the plot) that you removed? –  T. Verron Nov 24 at 13:32
    
Because I can't build this MWE... I get errors. –  XL64 Nov 24 at 13:40
    
Ah. That's a problem, because you cannot be sure that your answer is going to solve the same problem as the one from the OP, then. –  T. Verron Nov 24 at 13:50

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.