pgfplots 图表的背景图像

pgfplots 图表的背景图像

跟进答案适合 tex 图表的工具我制作了这张图片来模拟/替换报纸上的扫描图像:

在此处输入图片描述

我尝试了几种方法将背景新闻纸图像放在整个图形(轴标签和标题)以及图表的后面,但都没有成功。

完成后,我希望有参差不齐的边缘,就像@Ipsens 回答的那样 撕页效果

这是我的 MWE(不是绝对最小,但足够小)。新闻纸图像是 http://www.theblockforum.com/fun/lance/lancescollection/textures/paper2.jpg

编辑: 使用 TikZ 在图像上绘图可能会有帮助,但我无法编译那里的示例。

\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}

%https://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}

答案1

pencildraw这个想法是使用益普生s 回答在环境前做一个剪辑axisaxis环境之后\node用一个层来包含背景图像background

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\usetikzlibrary{calc,decorations.pathmorphing}

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

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

\definecolor{paper}{RGB}{239,227,157}

\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

\tikzset{
pencildraw/.style={ %
    decorate,
    decoration={random steps,segment length=2pt,amplitude=1pt}
    } %
}

\begin{document}
\begin{tikzpicture}[font=\sffamily]
\path[clip,pencildraw] (-2.5,-40pt) rectangle (9,6);
\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};
\begin{pgfonlayer}{background}
\node {\includegraphics{papiro}};
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容