5

This image is two tikzpictures side by side, each with a newsprint background (thanks to Background image for pgfplots chart).

I want them combined, so they appear on the same torn sheet of newsprint. The title should be centered there, too.

I've looked at answers to several questions about nesting tikzpictures but couldn't solve my problem.

The not quite minimal MWE follows. (Sorry for the details and the duplicated code. I thought it best to provide the actual plots I plan to use.)

enter image description here

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{calc,decorations.pathmorphing}

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

\usepackage{helvet}
\usepackage[eulergreek]{sansmath}

\newcommand{\images}{../images}

\begin{document}

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

\pgfplotstableread[col sep=comma]{
year, return, assets
2007, 23, 34.9
2008, 8.6, 37
2009, -27.3, 26
2010, 11, 27.4
2011, 21.4, 32
}\endowment

\newcommand{\plotsize}{width=7cm, height=6cm}

\tikzset{
pencildraw/.style={ %
    decorate,
    decoration={random steps,segment length=6pt,amplitude=3pt}
    }, 
font=\sffamily\sansmath
}
\begin{tikzpicture}
\path[clip,pencildraw] (-1.8,-1) rectangle (6,5.5);
\begin{axis}[ 
      title={\large Harvard Endowment},
      \plotsize,
      ylabel={Return}, 
      axis y line*=left,
      axis x line*=middle,
      xtick=data,
      xticklabel style={align=center},
      xticklabels={2007, 2008, 2009, 2010, 2011},
      yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
      nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
    ]
    \addplot table[x=year, y=return] {\endowment};

    \begin{pgfonlayer}{background}
      \node{\includegraphics{\images/newsprint}};
    \end{pgfonlayer}
\end{axis} 
\end{tikzpicture}

\begin{tikzpicture}
\path[clip,pencildraw] (-1.8,-1) rectangle (6,5.5);
\begin{axis}[ 
      ymin=0,
      ylabel={Assets}, 
      \plotsize,
      axis y line*=left,
      axis x line*=bottom,
      xtick=data,
      xticklabel style={align=center},
      xticklabels={2007, 2008, 2009, 2010, 2011},
      yticklabel={\$\pgfmathprintnumber{\tick}b},
      nodes near coords={\$\pgfmathprintnumber{\pgfplotspointmeta}b},
    ]
    \addplot table[x=year, y=assets] {\endowment};

    \begin{pgfonlayer}{background}
      \node{\includegraphics{\images/newsprint}};
    \end{pgfonlayer}
\end{axis} 
\end{tikzpicture}
\end{document}
  • You can place two axis environments in one tikzpicture. Check section 4.18 “Alignment Options” (pp. 280ff.) in the pgfplots manual. – Qrrbrbirlbel Jun 21 '13 at 2:17
9

To do this, you can put both axis environments into the same tikzpicture (not in a nested one) with a single large newsprint background. If you name the first axis using name=left axis, say, you can position the second one using at=(left axis.east), anchor=outer west (the outer refers to the anchor that takes all labels into account).

To place the title, it's easiest if you use a normal node outside the second axis environment. You can position it precisely in the middle of the two plots by using the calc library's coordinate calculation syntax ($(left axis.outer north east)!0.5!(right axis.outer north west)$):

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{calc,decorations.pathmorphing}

%Use the new axis label placement features
\pgfplotsset{compat=1.8}

\usepackage{helvet}
\usepackage[eulergreek]{sansmath}


\begin{document}

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

\pgfplotstableread[col sep=comma]{
year, return, assets
2007, 23, 34.9
2008, 8.6, 37
2009, -27.3, 26
2010, 11, 27.4
2011, 21.4, 32
}\endowment

\newcommand{\plotsize}{width=7cm, height=6cm}

\tikzset{
pencildraw/.style={ %
    decorate,
    decoration={random steps,segment length=6pt,amplitude=3pt}
    }, 
font=\sffamily\sansmath
}
\begin{tikzpicture}
\path[clip,pencildraw] (-2,-1) rectangle (15,6);
\begin{axis}[ 
      name=left axis,
      \plotsize,
      ylabel={Return}, 
      axis y line*=left,
      axis x line*=middle,
      xtick=data,
      xticklabel style={align=center},
      xticklabels={2007, 2008, 2009, 2010, 2011},
      yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
      nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
    ]
    \addplot table[x=year, y=return] {\endowment};

    \begin{pgfonlayer}{background}
      \node {\includegraphics[width=30cm]{newsprint}};
    \end{pgfonlayer}
\end{axis} 
\begin{axis}[
      name=right axis,
      at=(left axis.east),
      anchor=outer west,
      xshift=3em,
      ymin=0,
      ylabel={Assets}, 
      \plotsize,
      axis y line*=left,
      axis x line*=bottom,
      xtick=data,
      xticklabel style={align=center},
      xticklabels={2007, 2008, 2009, 2010, 2011},
      yticklabel={\$\pgfmathprintnumber{\tick}b},
      nodes near coords={\$\pgfmathprintnumber{\pgfplotspointmeta}b},
    ]
    \addplot table[x=year, y=assets] {\endowment};
\end{axis} 
\node [anchor=south, yshift=3ex] at ($(left axis.outer north east)!0.5!(right axis.outer north west)$)
            {\large Harvard Endowment};
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.