使用 pgfplots 插入背景填充

使用 pgfplots 插入背景填充

我正在尝试在绘图中添加插图。我使用了此答案作为指南开始,但插图是透明的。axis background/.style={fill=white}向插图添加内容可以修复背景,但不能修复周围的轴标签。有没有办法可以将其扩展以覆盖整个区域?我认为从下图可以看出这个问题:

插图有问题

我没有使用该spy库,因为我正在使用更详细的数据文件作为插图。

\begin{figure} % CONTINUOUS SCAN INSET
\centering
\begin{tikzpicture}
\begin{axis}[
    width = 14cm,
    height = 8cm,
    title = {50 $\mu$m scintillator probe continuous scan},
    xlabel = {Position (mm)},
    ylabel = {Response (\% of Maximum)},
    axis lines = left,
    ymax = 110,
    ymin=0,
    xmin = 3,
]
\addplot[blue] table[x=x,y=y]{../../AS Data/ProcessedData/50um-profile.txt};
\coordinate (insetPosition) at (rel axis cs:0.35,0.15);
\end{axis}
\begin{axis}[at={(insetPosition)},anchor={outer south west},footnotesize,axis background/.style={fill=white},
    axis lines = left,
    ymax = 110,
    ymin=0,
    xmin = 19,
    xmax = 21,
    xtick = {19,19.4,...,21}
]
\addplot[blue] table[x=x,y=y]{../../AS Data/ProcessedData/50um-profile-subsection-small.txt};
\end{axis}
\end{tikzpicture}
\caption{Continuous scan through the field (inset).}
\label{}
\end{figure}

答案1

感谢 Stefan Pinnow链接相关解决方案. 在轴前声明新的 pgf 层:

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

使用以下方法将每个轴嵌套在适当的层内:

\begin{pgfonlayer}{background}

等,并将主层设置为包含白色矩形即可实现所需的结果:

\begin{pgfonlayer}{main}
    \fill [black!0] ([shift={(-2pt,-2pt)}] insetAxis.outer south west)
        rectangle    ([shift={(+5pt,+5pt)}] insetAxis.outer north east);
\end{pgfonlayer}

完整的 TeX 代码:

\begin{figure} % CONTINUOUS SCAN INSET

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

\centering
\begin{tikzpicture}
\begin{pgfonlayer}{background}
\begin{axis}[
    width = 14cm,
    height = 8cm,
    title = {50 $\mu$m scintillator probe continuous scan},
    xlabel = {Position (mm)},
    ylabel = {Response (\% of Maximum)},
    axis lines = left,
    ymax = 110,
    ymin=0,
    xmin = 3,
]
\addplot[blue] table[x=x,y=y]{../../AS Data/ProcessedData/50um-profile.txt};
\coordinate (insetPosition) at (rel axis cs:0.35,0.15);
\end{axis}
\end{pgfonlayer}

\begin{pgfonlayer}{foreground}
\begin{axis}[at={(insetPosition)},anchor={outer south west},footnotesize,axis background/.style={fill=white},
    axis lines = left,
    ymax = 110,
    ymin=0,
    xmin = 19,
    xmax = 21,
    xtick = {19,19.4,...,21},
    name = insetAxis
]
\addplot[blue] table[x=x,y=y]{../../AS Data/ProcessedData/50um-profile-subsection-small.txt};
\end{axis}
\end{pgfonlayer}

    \begin{pgfonlayer}{main}
        \fill [black!0] ([shift={(-2pt,-2pt)}] insetAxis.outer south west)
            rectangle    ([shift={(+5pt,+5pt)}] insetAxis.outer north east);
    \end{pgfonlayer}

\end{tikzpicture}
\caption{Continuous scan through the field (inset).}
\label{}
\end{figure}

相关内容