向 Tikz Axis 添加半透明背景图像

向 Tikz Axis 添加半透明背景图像

我有一堆包含线图的图形,我想向这些图形添加背景图像,以显示与数据相比的几何形状。我想知道的是:如何向轴添加半透明的背景图像,该图像与轴的相对坐标有关?换句话说,我希望图像宽度等于轴的宽度。

以下是 MWE:

\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{graphicx}           % Add graphics
\usepackage{mathpazo}           % Change math and rm fonts to palatino
\usepackage{transparent}        % For changing image opacity
\usepackage{tikz}               % For drawing figures
\usetikzlibrary{calc}           % For making complex coordinate calculations
\usepackage{pgfplots}           % For generating plots
    \pgfplotsset{compat=newest} % Use newest version of pgfplots
\usepackage{mwe}
\usepackage{filecontents}
\begin{filecontents*}{test.data}
Y,U,V,W
130,-3.704216e-02,3.019000e-03,3.200443e-01
170,6.075245e-03,-2.507332e-02,-1.83164e-01
210,6.075245e-03,-2.507332e-02,-1.83164e-01
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \pgfplotsset{every axis/.append style={width=2.5in,height=2.5in}}
    \pgfplotsset{every axis/.append style={xmin=129.5,xmax=221,xtick={120,140,160,180,200,220}}}
    \pgfplotsset{every axis legend/.append style={at={(0,1.03)},anchor=south west,nodes=right}}
    \begin{axis}[name=topplot,clip mode=individual,
        axis background/.style={fill=white},
        ymin=-0.6,ymax=0.6,
        tick scale binop=\times,
        minor tick num=4,
        legend columns=1,
        ytick pos=both,
        ylabel={Velocity, [m/s]},
        ytick={-0.5,-0.25,0,0.25,0.5},
        y tick label style={
            /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
            /tikz/.cd}]

        \node[draw=none,anchor=south west,opacity=0.5] at (rel axis cs:0,0) {\includegraphics[width=1.75in]{example-image-a}};          
        \addplot+[blue,solid,mark=o]
            table[x=Y,y=U,col sep=comma] {test.data};
            \addlegendentry{u}
        \addplot+[green,solid,mark=square]
            table[x=Y,y=V,col sep=comma] {test.data};
            \addlegendentry{v}
        \addplot+[red,solid,mark=triangle]
            table[x=Y,y=W,col sep=comma]    {test.data};
            \addlegendentry{w}
    \end{axis}
\end{tikzpicture}
\end{document}

编辑:every nth point从代码中删除命令。

答案1

编辑:

对于axis任意宽度,您可以计算宽度并存储该值,如中所述这个答案。将该方法与新命令(我称之为\bgimage)相结合,您可以插入宽度等于轴宽度的背景图像。

在下面的例子中,我复制了右侧的图,但将其缩小以适当显示图像比例。此外,不透明度应由 TikZ 处理,因此我删除了该transparent包。

\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{graphicx}           % Add graphics
\usepackage{mathpazo}           % Change math and rm fonts to palatino
\usepackage{tikz}               % For drawing figures
\usetikzlibrary{calc}           % For making complex coordinate calculations
\usepackage{pgfplots}           % For generating plots
    \pgfplotsset{compat=newest} % Use newest version of pgfplots
\usepackage{mwe}
\usepackage{filecontents}
\begin{filecontents*}{test.data}
Y,U,V,W
130,-3.704216e-02,3.019000e-03,3.200443e-01
170,6.075245e-03,-2.507332e-02,-1.83164e-01
210,6.075245e-03,-2.507332e-02,-1.83164e-01
\end{filecontents*}

\pgfkeys{/tikz/savenumber/.code 2 args={\global\edef#1{#2}}}
\providecommand{\findlen}{%
\path
  let
  \p1 = ($(rel axis cs:0,0)$),
  \p2 = ($(rel axis cs:1,0)$),
  \n1 = {veclen(\x1-\x2,\y1-\y2)}
  in
  coordinate(p1) at (\p1)
  coordinate(p2) at (\p2)
  [savenumber={\n}{\n1}]
  ;
  }
\providecommand{\bgimage}[1]{%
\findlen
        \node[draw=none,anchor=south west,opacity=0.3,inner sep=0] at (rel axis cs:0,0) {\includegraphics[width=\n]{#1}}
}
\begin{document}
\begin{tikzpicture}
    \pgfplotsset{every axis/.append style={width=2.5in,height=2.5in,scale only axis}}
    \pgfplotsset{every axis/.append style={xmin=129.5,xmax=221,xtick={120,140,160,180,200,220}}}
    \pgfplotsset{every axis legend/.append style={at={(0,1.03)},anchor=south west,nodes=right}}
    \begin{axis}[name=topplot,clip mode=individual,
        axis background/.style={fill=white},
        ymin=-0.6,ymax=0.6,
        tick scale binop=\times,
        minor tick num=4,
        legend columns=1,
        ytick pos=both,
        ylabel={Velocity, [m/s]},
        ytick={-0.5,-0.25,0,0.25,0.5},
        y tick label style={
            /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
            /tikz/.cd}]
        \bgimage{example-image-a};
        \addplot+[blue,solid,mark=o]
            table[x=Y,y=U,col sep=comma] {test.data};
            \addlegendentry{u}
        \addplot+[green,solid,mark=square]
            table[x=Y,y=V,col sep=comma] {test.data};
            \addlegendentry{v}
        \addplot+[red,solid,mark=triangle]
            table[x=Y,y=W,col sep=comma]    {test.data};
            \addlegendentry{w}
    \end{axis}
\begin{scope}[xshift=3.2in]
    \begin{axis}[width=2in,height=2in,clip mode=individual,
        axis background/.style={fill=white},
        ymin=-0.6,ymax=0.6,
        tick scale binop=\times,
        minor tick num=4,
        legend columns=1,
        ytick pos=both,
        ylabel={Velocity, [m/s]},
        ytick={-0.5,-0.25,0,0.25,0.5},
        y tick label style={
            /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
            /tikz/.cd}]
        \bgimage{example-image-a};
        \addplot+[blue,solid,mark=o]
            table[x=Y,y=U,col sep=comma] {test.data};
            \addlegendentry{u}
        \addplot+[green,solid,mark=square]
            table[x=Y,y=V,col sep=comma] {test.data};
            \addlegendentry{v}
        \addplot+[red,solid,mark=triangle]
            table[x=Y,y=W,col sep=comma]    {test.data};
            \addlegendentry{w}
    \end{axis}

\end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

原始答案

添加inner sep=0pt到图像节点将删除图像和锚点之间的空间(在本例中为south west(rel axis cs:0,0))。

为了使图像和绘图宽度相同,请添加scale only axistikzset。这将使轴具有声明的宽度,而不是整个绘图(包括标签等)。

\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{graphicx}           % Add graphics
\usepackage{mathpazo}           % Change math and rm fonts to palatino
\usepackage{transparent}        % For changing image opacity
\usepackage{tikz}               % For drawing figures
\usetikzlibrary{calc}           % For making complex coordinate calculations
\usepackage{pgfplots}           % For generating plots
    \pgfplotsset{compat=newest} % Use newest version of pgfplots
\usepackage{mwe}
\usepackage{filecontents}
\begin{filecontents*}{test.data}
Y,U,V,W
130,-3.704216e-02,3.019000e-03,3.200443e-01
170,6.075245e-03,-2.507332e-02,-1.83164e-01
210,6.075245e-03,-2.507332e-02,-1.83164e-01
\end{filecontents*}

\begin{document}
\begin{tikzpicture}
    \pgfplotsset{every axis plot/.append style={each nth point=30}}
    \pgfplotsset{every axis/.append style={width=2.5in,height=2.5in,scale only axis}}
    \pgfplotsset{every axis/.append style={xmin=129.5,xmax=221,xtick={120,140,160,180,200,220}}}
    \pgfplotsset{every axis legend/.append style={at={(0,1.03)},anchor=south west,nodes=right}}
    \begin{axis}[name=topplot,clip mode=individual,
        axis background/.style={fill=white},
        ymin=-0.6,ymax=0.6,
        tick scale binop=\times,
        minor tick num=4,
        legend columns=1,
        ytick pos=both,
        ylabel={Velocity, [m/s]},
        ytick={-0.5,-0.25,0,0.25,0.5},
        y tick label style={
            /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=2,
            /tikz/.cd}]

        \node[draw=none,anchor=south west,opacity=0.5,inner sep=0pt] at (rel axis cs:0,0) {\includegraphics[width=2.5in]{example-image-a}};          
        \addplot+[blue,solid,mark=o]
            table[x=Y,y=U,col sep=comma] {test.data};
            \addlegendentry{u}
        \addplot+[green,solid,mark=square]
            table[x=Y,y=V,col sep=comma] {test.data};
            \addlegendentry{v}
        \addplot+[red,solid,mark=triangle]
            table[x=Y,y=W,col sep=comma]    {test.data};
            \addlegendentry{w}
    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容