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 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?

enter image description here

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

share|improve this question
    
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
add comment

2 Answers

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}
\addplot {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[background color=orange!20,use background]
\begin{axis}
\addplot+[only marks] {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\end{document}

The result:

enter image description here

share|improve this answer
add comment

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}
\addplot {sin(deg(x))};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
\addplot+[only marks] {sin(deg(x))};
\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.

share|improve this answer
add comment

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.