I have this code:

    \begin{axis}[axis lines=center,xmin=-1,xmax=9,ymin=-1,ymax=9,line width=0.4mm]
        \draw[fill=blue!40,line width=0mm] (5,-0.5) rectangle (8,0.5);

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?



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.

    \begin{axis}[axis lines=center,xmin=-1,xmax=9,ymin=-1,ymax=9,line
    width=0.4mm, axis background/.style={%
                    path picture={
                        \draw[fill=blue!40,line width=0mm] (axis cs:5,-0.5) rectangle (axis cs:8,0.5);
        \coordinate (BL) at (5,-0.5);
        \coordinate (TR) at (8,0.5); % just to export the coordinates outside
%the axis, not needed here

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.