1

I have this code:

\begin{tikzpicture}[scale=0.75]
    \begin{axis}[axis lines=center,xmin=-1,xmax=9,ymin=-1,ymax=9,line width=0.4mm]
        \pgfplotsset{xlabel={$x$},ylabel={$y$}}
        \draw[fill=blue!40,line width=0mm] (5,-0.5) rectangle (8,0.5);
    \end{axis}
\end{tikzpicture}

Rectangle forward of the figure

I try adding on layer=axis background to the draw command, but unfortunately it does not work because it must be inside to the addplot command.

I do not want to put the rectangle in the tikzpicture environment, because that forces me to change the values of where it is. Is there any way to put a figure (rectangle, circle, etc.) within the axis environment and that is behind the axes?

Thanks!

1

Axis background seems to work, I also added a simple way to smuggle coordinates outside the axis environment, even though it is not needed here. BTW, depending on your plot, you may want to add set layers, cell picture=true.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[scale=0.75]
    \begin{axis}[axis lines=center,xmin=-1,xmax=9,ymin=-1,ymax=9,line
    width=0.4mm, axis background/.style={%
                preaction={
                    path picture={
                        \draw[fill=blue!40,line width=0mm] (axis cs:5,-0.5) rectangle (axis cs:8,0.5);
                    }}}]
        \pgfplotsset{xlabel={$x$},ylabel={$y$}}
        \coordinate (BL) at (5,-0.5);
        \coordinate (TR) at (8,0.5); % just to export the coordinates outside
%the axis, not needed here
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Thank you very much! That worked. I'm going to investigate about set layers, cell picture = true :). – manooooh Feb 2 '18 at 3:01

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.