# Background fill with pgfplots inset

I am trying to add an inset to a plot. I used this answer as a guide to get started, but the inset plot is transparent. Adding axis background/.style={fill=white} to the inset plot fixes the background, but not in the surrounding axis labels. Is there a way I can extend this to cover the whole region? I think the issue is clear from the image below:

I am not using the spy library as I am using a more detailed data file for the inset.

\begin{figure} % CONTINUOUS SCAN INSET
\centering
\begin{tikzpicture}
\begin{axis}[
width = 14cm,
height = 8cm,
title = {50 $\mu$m scintillator probe continuous scan},
xlabel = {Position (mm)},
ylabel = {Response (\% of Maximum)},
axis lines = left,
ymax = 110,
ymin=0,
xmin = 3,
]
\coordinate (insetPosition) at (rel axis cs:0.35,0.15);
\end{axis}
\begin{axis}[at={(insetPosition)},anchor={outer south west},footnotesize,axis background/.style={fill=white},
axis lines = left,
ymax = 110,
ymin=0,
xmin = 19,
xmax = 21,
xtick = {19,19.4,...,21}
]
\end{axis}
\end{tikzpicture}
\caption{Continuous scan through the field (inset).}
\label{}
\end{figure}


• Welcome to TeX.SX. Although the question handles a totally different topic, I think my answer given at tex.stackexchange.com/a/358296/95441 should answer your question, right? – Stefan Pinnow May 2 '17 at 2:24
• @StefanPinnow yes, that does work for me, thanks! I'll write the working code up as an answer to this question. – Nuclear_Wizard May 2 '17 at 2:51

Thanks Stefan Pinnow for linking a relevant solution. Declaring new pgf layers before the axes:

\pgfdeclarelayer{background} \pgfdeclarelayer{foreground} \pgfsetlayers{background,main,foreground}

Nesting each axis inside the appropriate layer using:

\begin{pgfonlayer}{background}


etc and setting the main layer to include a white rectangle achieves the desired result:

\begin{pgfonlayer}{main}
\fill [black!0] ([shift={(-2pt,-2pt)}] insetAxis.outer south west)
rectangle    ([shift={(+5pt,+5pt)}] insetAxis.outer north east);
\end{pgfonlayer}


Complete TeX code:

\begin{figure} % CONTINUOUS SCAN INSET

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\centering
\begin{tikzpicture}
\begin{pgfonlayer}{background}
\begin{axis}[
width = 14cm,
height = 8cm,
title = {50 $\mu$m scintillator probe continuous scan},
xlabel = {Position (mm)},
ylabel = {Response (\% of Maximum)},
axis lines = left,
ymax = 110,
ymin=0,
xmin = 3,
]
\coordinate (insetPosition) at (rel axis cs:0.35,0.15);
\end{axis}
\end{pgfonlayer}

\begin{pgfonlayer}{foreground}
\begin{axis}[at={(insetPosition)},anchor={outer south west},footnotesize,axis background/.style={fill=white},
axis lines = left,
ymax = 110,
ymin=0,
xmin = 19,
xmax = 21,
xtick = {19,19.4,...,21},
name = insetAxis
]
\end{axis}
\end{pgfonlayer}

\begin{pgfonlayer}{main}
\fill [black!0] ([shift={(-2pt,-2pt)}] insetAxis.outer south west)
rectangle    ([shift={(+5pt,+5pt)}] insetAxis.outer north east);
\end{pgfonlayer}

\end{tikzpicture}
\caption{Continuous scan through the field (inset).}
\label{}
\end{figure}

• Great that it helped you. But instead of answering your own question with "my solution" you could have asked me, if I want to answer it. Or at least it would have been nice if you would have upvoted my answer, if it helped you ... – Stefan Pinnow May 2 '17 at 3:20
• @StefanPinnow Sorry, you're right. I assumed that if you wanted to write it up, you would have done so, rather than comment. My bad. – Nuclear_Wizard May 3 '17 at 1:08