10
$\begingroup$

This question already has an answer here:

When I produce a simple plot like so:

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

It looks like this:

enter image description here

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.

$\endgroup$

marked as duplicate by Mr.Wizard plotting Mar 7 '17 at 6:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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], 
  PlotRangePadding -> 0
]

Mathematica graphics

As for your other question

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:

enter image description here

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:

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks, I found the answer while you were typing yours ;-) $\endgroup$ – Pirx Aug 22 '16 at 1:27
  • $\begingroup$ 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? $\endgroup$ – Pirx Aug 22 '16 at 1:35
  • $\begingroup$ 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. $\endgroup$ – halirutan Aug 22 '16 at 1:40
  • 1
    $\begingroup$ @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. $\endgroup$ – halirutan Aug 22 '16 at 2:02
  • 1
    $\begingroup$ @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! $\endgroup$ – Pirx Aug 22 '16 at 11:11
4
$\begingroup$

Adding a Frame cleans it up nicely:

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

enter image description here

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}}]

enter image description here

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}, 
 PlotRangePadding -> {0, 0.1}]

enter image description here

$\endgroup$
  • $\begingroup$ Yep, that works, thanks, but what if I don't want a frame? $\endgroup$ – Pirx Aug 22 '16 at 1:46
  • $\begingroup$ @Pirx I updated with some more frame settings $\endgroup$ – Young Aug 22 '16 at 1:50
  • $\begingroup$ Nice, but I'm not so easy to satisfy: I really may want that x-axis with its labels and ticks shown at y=0. Sorry ;-) $\endgroup$ – Pirx Aug 22 '16 at 1:52
  • $\begingroup$ @Pirx updated ... I'm stubborn =) $\endgroup$ – Young Aug 22 '16 at 2:17
  • $\begingroup$ That's a solution, yes. In this case I', drawing my axis manually. There's really nothing to quibble about your approach. However, I would expect Mathematica to give me such reasonable behavior by default, not me having to write the code for it myself. $\endgroup$ – Pirx Aug 22 '16 at 11:18

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