Why are my plots displaying behind the axes?

49

13

I need to create graphs with light gray axes and black lines. All of my plots display behind the axes. How can I make the plots display over the axes?

Plot[2 x - 2, {x, -10, 10}, PlotRange -> {{-10, 10}, {-10, 10}},    
    PlotStyle -> Directive[Black, AbsoluteThickness[2]], ImageSize -> 300, 
    AxesStyle -> Directive[RGBColor[.8, .8, .8], AbsoluteThickness[2]], AspectRatio -> 1]

lvames

Posted 2013-06-13T04:00:57.947

Reputation: 431

1This post by Verbeia might also be of interest. – user1066 – 2013-06-13T09:45:02.967

Answers

55

The cure is undocumented, unfortunately now documented, as of version 10. Try adding Method -> {"AxesInFront" -> False}, like so:

Plot[2 x - 2, {x, -10, 10}, PlotRange -> {{-10, 10}, {-10, 10}}, 
     PlotStyle -> Directive[Black, AbsoluteThickness[2]], ImageSize -> 300, 
     AxesStyle -> Directive[RGBColor[.8, .8, .8], AbsoluteThickness[2]], 
     AspectRatio -> 1, Method -> {"AxesInFront" -> False}]

plot with axes in front

There is also a corresponding "FrameInFront" option for frames, and "GridLinesInFront" for grid lines.

J. M.'s ennui

Posted 2013-06-13T04:00:57.947

Reputation: 115 520

2

In version 10 it is documented on the Graphics documentation page under "Details and options".

– Alexey Popkov – 2015-05-31T08:11:28.337

Nice to know that it's finally documented. Thanks! – J. M.'s ennui – 2015-05-31T08:12:23.227

I wonder if something similar exist for the tick labels. It seems there's no reference for that in the documentation. – abcd – 2020-09-03T23:47:22.033

13

There can be another solution. FullGraphics can convert the axes to graphics primitives, then you can manipulate the order of layers by hand:

FullGraphics[
Plot[2 x - 2, {x, -10, 10}, PlotRange -> {{-10, 10}, {-10, 10}}, 
PlotStyle -> Directive[Black, AbsoluteThickness[2]], 
ImageSize -> 300, 
AxesStyle -> Directive[RGBColor[.8, .8, .8], AbsoluteThickness[2]],
AspectRatio -> 1]] /. Graphics[x_] :> Graphics[Reverse[x]]

Comment from rcollyer:

FullGraphics is spotty, so take care in its use.

mmjang

Posted 2013-06-13T04:00:57.947

Reputation: 2 533

AxisInFront is simple and works well. I'll give that a good testing for my project. Good to know there is another option, especially for my own knowledge. Thank you! – lvames – 2013-06-13T10:40:14.667

@lvames Glad I can help. – mmjang – 2013-06-13T10:58:04.743

7@lvames FullGraphics is spotty, so take care in its use. – rcollyer – 2013-06-13T16:20:48.947

4

I found this useful, though a little ugly.

Overlay[{
 ListPlot[{0,0},PlotRange->{{-10,10},{-10,10}}, ImageSize->300, AspectRatio->1,
          AxesStyle->Directive[RGBColor[.8,.8,.8], AbsoluteThickness[2]]],
 Plot[2 x-2,{x,-10,10}, PlotRange->{{-10,10},{-10,10}}, Axes->False, AspectRatio->1,      
      PlotStyle->Directive[Black,AbsoluteThickness[2]],ImageSize->300]
}]

But you cannot directly save the image, you can select the cell and save selection as image.

enter image description here

Reverse the list in Overlay will get the result where black line behind the axes.

HyperGroups

Posted 2013-06-13T04:00:57.947

Reputation: 7 917