1

I want to simply highlight the part of the chart (from 500-700). I used the following code for the highlighting setup:

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

% 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
    % end code to enable highlighting
    width=10cm,
    %width=3cm,
    height=5cm,
    enlargelimits=false,
    %width = 8.25 cm,
    %height = 6.25 cm,
    minor y tick num={1},
    minor x tick num={1},
    %yticklabel style={text width=5mm,align=right},
    every minor tick/.append style={ultra thin},
    minor grid style={gray!40, ultra thin},
    major grid style={gray!55, thin},
    ylabel shift = -.15 cm,
    xlabel shift = -.15 cm,
    grid = both,
    every axis/.append style={thin, tick style={thick}},
    label style = {font=\ssmall\sffamily}, 
    legend style={font=\ssmall\sffamily,anchor=north east, at={(1.015,1.00)}},
    legend columns = 1,
    every axis plot post/.append style={line width = 0.75pt,mark size = 2.2pt}
}

Then, I created the chart:

\begin{adjustbox}{width=\textwidth}
\begin{tikzpicture}

\begin{axis}[%
set layers,
separate axis lines,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
xmin=300,
xmax=700,
xlabel={Traffic load (Erlang)},
xmajorgrids,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=20,
ylabel={Average Request Blocking Probability ($\%$)},
ymajorgrids,
legend style={/tikz/every even column/.append style={column sep=2cm}, legend cell align=left,align=left,fill=none},
every x tick label/.append style={font = \ssmall\sansmath\sffamily}, every y tick label/.append style={font = \ssmall\sansmath\sffamily},
legend columns = 3,
legend to name= dc5
]
\addplot[highlight=500:700,color=mycolor1,dash pattern=on 1pt off 3pt on 3pt off 3pt,line width=0.7pt,mark size=2.2pt,mark=diamond,mark options={solid}]
  table[row sep=crcr]{%
300 0.00    \\
350 0.00    \\
400 0.00    \\
450 0.00    \\
500 0.00    \\
550 0.00    \\
600 1.97    \\
650 2.03    \\
700 3.62    \\
};
\addlegendentry{DC-TP};

%additional plots
\end{axis}
\end{tikzpicture}%
\end{adjustbox}

Without highlight option on the plot, everything works fine. When I add the:

highlight=500:700

it keeps crashing and this error shows up:

Package pgf Error: Sorry, the requested layer 'axis background' is not part of the layer list. Please verify that you provided \pgfsetlayers and that 'axis background' is part of this list.

Thank you for any help.

8
  • Nice to meet you all :)
    – m.aibin
    Jan 4, 2017 at 11:11
  • 1
    Your example works fine here (after defining the missing mycolor1 color, and replacing \ssmall with \small). I don't know, but my guess would be that you have an older version of pgfplots. If you add \pgfplotsversion right after \begin{document}, what does that print? Jan 4, 2017 at 11:31
  • Also that on layer code is now included in the official release so you don't need to jump those hoops.
    – percusse
    Jan 4, 2017 at 12:39
  • It shows 1.14 @TorbjørnT.
    – m.aibin
    Jan 4, 2017 at 17:21
  • Really? I have the same, and it works perfectly here. Can you turn your snippets into a complete, yet minimal example that demonstrates the problem? (I.e. code starting with \documentclass ending with \end{document}, and having all the necessary bits so that it can be compiled without modifying anything. But irrelevant should not be included, i.e. we don't need your whole thesis.) Jan 4, 2017 at 17:31

2 Answers 2

1

There is a conflict between using that highlighting code and legend to name, the code fails on the \ref for the legend, not the actual plot. I don't know how to fix that, so I can only suggest a different method for the highlighting:

\addplot [forget plot,fill=mycolor1,opacity=0.2] coordinates {(500,\pgfkeysvalueof{/pgfplots/ymax})(700,\pgfkeysvalueof{/pgfplots/ymax})} \closedcycle;

Add that as the first plot, forget plot means that it isn't included in the legend.

enter image description here

\documentclass{article}
\usepackage{pgfplots,sansmath}
\colorlet{mycolor1}{blue}
\let\ssmall\small
\pgfplotsset{
    compat=1.14,
    width=10cm,
    %width=3cm,
    height=5cm,
    enlargelimits=false,
    %width = 8.25 cm,
    %height = 6.25 cm,
    minor y tick num={1},
    minor x tick num={1},
    %yticklabel style={text width=5mm,align=right},
    every minor tick/.append style={ultra thin},
    minor grid style={gray!40, ultra thin},
    major grid style={gray!55, thin},
    ylabel shift = -.15 cm,
    xlabel shift = -.15 cm,
    grid = both,
    every axis/.append style={thin, tick style={thick}},
    label style = {font=\ssmall\sffamily}, 
    legend style={font=\ssmall\sffamily,anchor=north east, at={(1.015,1.00)}},
    legend columns = 1,
    every axis plot post/.append style={line width = 0.75pt,mark size = 2.2pt}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
set layers,
separate axis lines,
every outer x axis line/.append style={black},
every x tick label/.append style={font=\color{black}},
xmin=300,
xmax=700,
xlabel={Traffic load (Erlang)},
xmajorgrids,
every outer y axis line/.append style={black},
every y tick label/.append style={font=\color{black}},
ymin=0,
ymax=20,
ylabel={Average Request Blocking Probability ($\%$)},
ymajorgrids,
legend style={/tikz/every even column/.append style={column sep=2cm}, legend cell align=left,align=left,fill=none},
every x tick label/.append style={font = \ssmall\sansmath\sffamily}, every y tick label/.append style={font = \ssmall\sansmath\sffamily},
legend columns = 3,
legend to name= dc5
]

\addplot [forget plot,fill=mycolor1,opacity=0.2] coordinates {(500,\pgfkeysvalueof{/pgfplots/ymax})(700,\pgfkeysvalueof{/pgfplots/ymax})} \closedcycle;

\addplot[
color=mycolor1,dash pattern=on 1pt off 3pt on 3pt off 3pt,line width=0.7pt,mark size=2.2pt,mark=diamond,mark options={solid}]
  table[row sep=crcr]{%
300 0.00    \\
350 0.00    \\
400 0.00    \\
450 0.00    \\
500 0.00    \\
550 0.00    \\
600 1.97    \\
650 2.03    \\
700 3.62    \\
};
\addlegendentry{DC-TP};
\end{axis}
\end{tikzpicture}%

\ref{dc5}
\end{document}
0
1

As Torbjørn T. already stated in the comment below the question, the real problem is that the "stored" legend dc5 (with the option legend to name=dc5) cannot be referenced. As the error message states, that is, because there is no layer background in the legend created that way.

To overcome this issue you could add that layer to that "stored" legend, but you wouldn't get the desired result. If you want to see what would happen, just remove the line legend to name=dc5 (and when using my code also the lines forget plot and the "added" block).

So I present a similar solution as Torbjørn T. in his answer but still using the layers and thus not faking that the grid is still on top by using the opacity option (which usually causes trouble when printing such an image).

The key is to (also) "forget the plot" and drawing the legend image "newly" using the \addlegendimage command.

For more details on how it works, please have a look at the comments in the code. (Please note that I have reduced your code to an almost minimum.)

% used PGFPlots v1.14
\documentclass[border=5pt,varwidth]{standalone}
\usepackage{pgfplots}
    % needed for the `highlight' style
    \pgfkeys{
      /tikz/on layer/.code={
        \pgfonlayer{#1}\begingroup
        \aftergroup\endpgfonlayer
        \aftergroup\endgroup
      }
    }
    \pgfplotsset{
        compat=1.3,
        % -----
        % code to enable highlighting
        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
        % -----
        %
        width=10cm,
        height=5cm,
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            set layers=standard,    % <-- set layers to `standard'
            xmin=300,
            xmax=700,
            xlabel={Traffic load (Erlang)},
            xmajorgrids,
            ymin=0,
            ymax=20,
            ylabel={Average Request Blocking Probability ($\%$)},
            ymajorgrids,
            legend to name=dc5,
        ]
            \addplot [
                forget plot,    % <-- so it is ignored in the legend ...
                highlight=500:700,
                color=red,
                dash pattern=on 1pt off 3pt on 3pt off 3pt,
                mark=diamond,
                mark options={solid},
            ] table [row sep=crcr] {
                300 0.00    \\
                350 0.00    \\
                400 0.00    \\
                450 0.00    \\
                500 0.00    \\
                550 0.00    \\
                600 1.97    \\
                650 2.03    \\
                700 3.62    \\
            };
            % --- added ---
            % ... instead draw a "custom" legend image using the same options
            % as above, expect the "highlight" stuff (of course)
            \addlegendimage{
                color=red,
                dash pattern=on 1pt off 3pt on 3pt off 3pt,
                mark=diamond,
                mark options={solid},
            }
            % -------------
            \addlegendentry{DC-TP};
        \end{axis}
    \end{tikzpicture}

    \ref{dc5}
\end{document}

image showing the result of above code

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .