49
$\begingroup$

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]

$\endgroup$
1
  • 1
    $\begingroup$ This post by Verbeia might also be of interest. $\endgroup$
    – user1066
    Jun 13, 2013 at 9:45

3 Answers 3

57
$\begingroup$

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.

$\endgroup$
3
  • 2
    $\begingroup$ In version 10 it is documented on the Graphics documentation page under "Details and options". $\endgroup$ May 31, 2015 at 8:11
  • $\begingroup$ Nice to know that it's finally documented. Thanks! $\endgroup$ May 31, 2015 at 8:12
  • $\begingroup$ I wonder if something similar exist for the tick labels. It seems there's no reference for that in the documentation. $\endgroup$
    – abcd
    Sep 3, 2020 at 23:47
13
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ 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! $\endgroup$
    – lvames
    Jun 13, 2013 at 10:40
  • $\begingroup$ @lvames Glad I can help. $\endgroup$
    – mmjang
    Jun 13, 2013 at 10:58
  • 7
    $\begingroup$ @lvames FullGraphics is spotty, so take care in its use. $\endgroup$
    – rcollyer
    Jun 13, 2013 at 16:20
4
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.