45
$\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
    $\begingroup$ This post by Verbeia might also be of interest. $\endgroup$ – user1066 Jun 13 '13 at 9:45
52
$\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$
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$
  • $\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 '13 at 10:40
  • $\begingroup$ @lvames Glad I can help. $\endgroup$ – mmjang Jun 13 '13 at 10:58
  • 7
    $\begingroup$ @lvames FullGraphics is spotty, so take care in its use. $\endgroup$ – rcollyer Jun 13 '13 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, privacy policy and cookie policy

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