# Background color in pgfplots that extends beyond the axis of the plot

I am interested in adding a background-color to my pgfplots plot that extends beyond the axis of the plot. See the image bellow for clarification; I am interested in adding a background color such as the plot in the image.

How would you achieve this?

Example from: The pgfplots manual (Christian Feuersänger, 2012, p. 20)

-
Welcome to TeX.sx! This is actually done by TikZ (the PGF manual has the same background in its code examples). Take a look into the backgrounds library in the PGF manual. –  Qrrbrbirlbel Mar 9 '13 at 6:41

As Qrrbrbirlbel suggested, you might use the backgrounds library from TikZ. The example I provide you uses as predefined color the same one adopted in the pgfmanual and in the pgfplots documentation, but it can be easily changed via a specific key.

The code:

\documentclass[a4paper,11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.7}
\usetikzlibrary{backgrounds}
% background color definition from pgfmanual-en-macros.tex
\definecolor{graphicbackground}{rgb}{0.96,0.96,0.8}
% key to change color
\pgfkeys{/tikz/.cd,
background color/.initial=graphicbackground,
background color/.get=\backcol,
background color/.store in=\backcol,
}
\tikzset{background rectangle/.style={
fill=\backcol,
},
use background/.style={
show background rectangle
}
}

\begin{document}
\begin{tikzpicture}[use background]
\begin{axis}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[background color=orange!20,use background]
\begin{axis}
\end{axis}
\end{tikzpicture}
\end{document}


The result:

-

Another possibility would be to create plots and figures in separate files with standalone and include them into main document through \includegraphics but with a color box providing a background color.

standalone provides options tikz and border. First one places every tikzpicture in a separate page. This way you can have a file with all plots/tikzfigures and select them with page=... option in \includegraphics command. border option enlarges figure with an outside margin of certain length. So, processing next file you will get a pdf file with two pages. Each page will contain one plot enlarged 2mm.

% file: 101603b.tex
\documentclass[tikz,border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.7}

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


Next file is the main document where graphics are included. Command \myincludegraphics adds a \colorbox of backgroundcolor. You can change it with \colorlet or include the background color as a parameter in \myincludegraphics definition.

%file: 101603.tex
\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}
\usepackage{xcolor}

\definecolor{backgroundcolor}{RGB}{253,245,238}

\newcommand{\myincludegraphics}[2][]{%
\begingroup\setlength{\fboxsep}{0pt}%
\colorbox{backgroundcolor}{\includegraphics[#1]{#2}}%
\endgroup}

\begin{document}

\myincludegraphics[page=1]{101603b}

\colorlet{backgroundcolor}{red!20}
\myincludegraphics[page=2]{101603b}

\end{document}


The result is similar to the one shown in Claudio's answer.

-