3

I have a PDF file and I want to use a subset of this image as a background for a Tikz axis:

\begin{tikzpicture}
    \begin{axis}[
        grid=both,
        xmin=-30, xmax=30,
        ymin=0, ymax=45,
        set layers]
        \addplot[thick, color=blue, on layer=axis background]
        graphics[xmin=-180,ymin=-90,xmax=180,ymax=90] {borders.pdf};
    \end{axis}
\end{tikzpicture}

Unfortunately, this does not work as the image is shown in the background:

enter image description here

On a standalone, the background is only visible on the side of the axis, but on a normal document, I can see the whole image. If I remove on layer=axis background, the image is cropped as expected, but then it's on the foreground, which I do not want (it's above the grid lines for instance).

I would like the background to stop at the axis boundary, is it possible?


Whole code if necessary (can use any image for the background):

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    grid=both,
    xmin=-30, xmax=30,
    ymin=0, ymax=45,
    clip=true,
    set layers]
    \addplot[thick, color=blue, on layer=axis background]
    graphics[xmin=-180,ymin=-90,xmax=180,ymax=90] {borders.pdf};
  \end{axis}
\end{tikzpicture}
\end{document}
  • If you remove on layer=axis background and add axis on top to the axis options, does that do what you're after? – Torbjørn T. Jul 2 '18 at 10:35
  • @TorbjørnT. Almost, the background is ok but now if I plot something on top it's below the grid lines. – Holt Jul 2 '18 at 10:37
  • @TorbjørnT. It works if I add on layer=axis foreground to the other plots. I'll just have to crop these manually but I can deal with this! – Holt Jul 2 '18 at 10:41
5

Add clip mode=individual to the axis options, then clipping will work for the image, even with on layer=axis background.

This is actually mentioned in the description of on layer in the pgfplots manual:

Note that if you have two plots with different values of on layer, you may also want to enable clip mode=clip individual or to deactivate clipping altogether using clip=false. Clipping options need to be provided as option to the axis, not to the plot. The technical background is that clip paths needs to be replicated for the layer on which the drawing is supposed to happen – otherwise they will be applied to the wrong layer.

(section 4.27.3 Changing the Layer of Graphical Elements, page 411, in the manual for pgfplots version 1.16, dated 2018/03/28.)

enter image description here

\documentclass{article}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    grid=both,
    grid style={blue}, % just to make it obvious with the given image
    xmin=-30, xmax=30,
    ymin=0, ymax=45,
    clip=true,
    set layers,
    clip mode=individual % <-- add this
]
    \addplot[thick, color=blue, on layer=axis background]
    graphics[xmin=-180,ymin=-90,xmax=180,ymax=90] {example-image};


\addplot [very thick,red] {rand*30+20};
  \end{axis}
\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.