Friday 7 June 2013

Background image for pgfplots chart

Background image for pgfplots chart

Following up on the answer to proper tool for charts in tex I've built this picture to simulate/replace a scanned image from a newspaper:

I've tried several ways to put the background newsprint image behind the whole figure (axis labels and titles) as well as the chart, with no success.
When that's done, I'd like to have ragged edges, as in @Ipsens's answer to Torn page effect
Here's my MWE (not absolutely minimal, but small enough). The newsprint image is http://www.theblockforum.com/fun/lance/lancescollection/textures/paper2.jpg
\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{backgrounds}

%deal with warning message in log
\pgfplotsset{compat=1.8}

\newcommand{\mywidth}{10cm}
\newcommand{\myheight}{7cm}

%http://tex.stackexchange.com/questions/35872/multiline-coords
\pgfplotstableread[col sep=comma]{
name, footprint
Google search, 0.2
Movie download, 20
Daily newspaper, 170
}\carbonfootprints

\begin{document}
\begin{tikzpicture}[font=\sffamily]

\begin{pgfonlayer}{background}
   \includegraphics{newsprint}
\end{pgfonlayer}

\begin{axis}[
      xbar, xmin=0,
      tick style={draw=none},
      width=\mywidth, height=\myheight,
      enlarge y limits=0.5,
      xlabel={Estimated carbon footprint, g CO${}_2$},
      ytick=data,
      yticklabel style={align=right},
      yticklabels={
        Google\\ search,
        Movie\\ download,
        Daily\\ newspaper,
      },
      nodes near coords,
      nodes near coords align={horizontal},
    ]
    \addplot [fill=red] table [x=footprint, y expr=\coordindex]
             {\carbonfootprints};
\end{axis}

\end{tikzpicture}
\end{document}

No comments:

Post a Comment