这幅图是两张tikzpicture
并排的图片,每张都有一张新闻纸背景(感谢pgfplots 图表的背景图像)。
我想将它们合并起来,这样它们就会出现在同一张撕碎的报纸上。标题也应该居中。
我查看了有关嵌套的几个问题的答案,tikzpicture
但无法解决我的问题。
接下来是不是很简单的 MWE。(抱歉,代码重复了,细节也重复了。我觉得最好提供我计划使用的实际图表。)
\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}
答案1
为此,您可以将两个axis
环境放入同一个环境中tikzpicture
(而不是嵌套环境中),并使用单个大型新闻纸背景。如果您使用 命名第一个轴name=left axis
,则可以使用 定位第二个轴at=(left axis.east), anchor=outer west
(outer
指的是考虑所有标签的锚点)。
要放置标题,最简单的方法是使用第二个环境之外的普通节点。您可以使用库的坐标计算语法axis
将其精确定位在两个图的中间:calc
($(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}