# Gridlines too long [duplicate]

When I produce a simple plot like so:

Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic,  GridLinesStyle -> Directive[Red]]


It looks like this:

Notice how the grid lines cross over the vertical axis on the left, going all the way through the first character of the tick labels. I see this regardless of StyleSheet used. Looks like a bug to me, but perhaps there's a way to fix this? Of course, ideally I'd also have the tick labels on the horizontal axis printed on top of the grid lines rather than the other way around, but that may be to reasonable a choice to ask for...

Edit:

Never mind the question on the too long gridlines; I found the answer (use PlotRangePadding -> 0). My question on having the gridlines in the background remains, however.

## marked as duplicate by Mr.Wizard♦ plotting StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Mar 7 '17 at 6:43

• TicksStyle -> Directive[Background -> Directive[Opacity[0.75], White]] increases the contrast between the tick labels and the grid lines. – Michael E2 Aug 22 '16 at 2:23
• @Michael E2: By the way, this is an excellent suggestion, which does pretty much exactly what I want. Thanks! – Pirx Aug 23 '16 at 15:41

The grid-lines are directly connected to the PlotRange that you use. Although you only plot from 0 to 2*Pi, Mathematica adds a little space around your plot. This little space, called PlotRangePadding is the source of this issue:

Plot[Sin[x], {x, 0, 2 Pi},
GridLines -> Automatic,
GridLinesStyle -> Directive[Red],
]


Of course, ideally I'd also have the tick labels on the horizontal axis printed on top of the grid lines rather than the other way around

Well, at least for me it seems that grid is indeed in the background which can be shown in a magnified screenshot. Of course, red grid-lines in combination with gray axes aren't the best design choice:

As a final note, if you could live for instance with gray grid-lines, the complete issue doesn't look so bad after all. Even if you extend your plot further to the left:

• Thanks, I found the answer while you were typing yours ;-) – Pirx Aug 22 '16 at 1:27
• P.S.: Still looks like a bug to me, or just the typical Wolfram sloppiness. It makes no sense whatsoever to have these grid lines extend beyond the axes and clash with the tick labels. By the way, is there a reason for the default non-zero setting of PlotRangePadding? What is it that this default helps with? Heck, why would I even be interested in having such an option available? – Pirx Aug 22 '16 at 1:35
• Usually it is a good thing to have some extra-space. Even in your example above, you can see that the curve is cropped on bottom and top, but a more obvious example would be ListPlot[RandomReal[{-1, 1}, 10], PlotRangePadding -> 0], where points are cut into half. So instead of always adjusting the plot-range, Mathematica adds a little space to make all of the function visible. – halirutan Aug 22 '16 at 1:40
• @Pirx What about that "axes thicks are not on top of the grid" part of your question? See my updated answer. The only real dilemma you have is that in a plot like yours you want the axis to cross at {0,0} which makes the extension of the grid to the left look weird. – halirutan Aug 22 '16 at 2:02
• @halirutan: I now see that you're right, the labels do seem on top of the lines. I only chose the red color for demonstration purposes, in most cases gray lines will be fine. Thanks! – Pirx Aug 22 '16 at 11:11

Adding a Frame cleans it up nicely:

Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic,
GridLinesStyle -> Directive[Red], Frame -> True]


A little more Frame work gives the appearance of a non-framed plot:

Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic,
GridLinesStyle -> Directive[Red], AxesStyle -> Opacity[0],
Frame -> {{True, False}, {True, False}},
FrameTicks -> {{Automatic, None}, {Automatic, None}}]


Another option, but with the x-axis at y=0:

ct = Table[{i, Row[{"   ", i}]}, {i, 0, 2 Pi, 1}];
Plot[Sin[x], {x, 0, 2 Pi}, GridLines -> Automatic,
GridLinesStyle -> Directive[Red], Ticks -> {ct, Automatic},