49

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]

rm -rf
  • 88,781
  • 21
  • 293
  • 472
lvames
  • 431
  • 4
  • 4

3 Answers3

57

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 missing motivation
  • 124,525
  • 11
  • 401
  • 574
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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
mmjang
  • 2,583
  • 20
  • 26
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.

Kuba
  • 136,707
  • 13
  • 279
  • 740
HyperGroups
  • 8,619
  • 1
  • 26
  • 63