# How do I get rid of the overlap of gridlines and tick mark labels?

I wish to include the plot generated by the following Mathematica code

Plot[1/Root[-3 - 8 #1 Tan[a/2] - 3 Tan[a/2]^2
+ #1^4 (1 + Tan[a/2]^2) + #1^2 (-9 + 3 Tan[a/2]^2) &, 2],
{a, 0, 2 Pi},
AxesOrigin -> {0, 0},
Ticks -> {{0, Pi/6, Pi/3, Pi/2, 2 Pi/3, 5 Pi/6, Pi, 7 Pi/6, 4 Pi/3,
3 Pi/2, 5 Pi/3, 11 Pi/6, 2 Pi}, {0, 0.125, 0.25, 0.375, 0.5,
0.625, 0.75, 0.875, 1, 1.125, 1.25, 1.375, 1.5}},
GridLines -> {{Pi/6, Pi/3, Pi/2, 2 Pi/3, 5 Pi/6, Pi, 7 Pi/6, 4 Pi/3,
3 Pi/2, 5 Pi/3, 11 Pi/6, 2 Pi}, {0, 1/8, 2/8, 3/8, 4/8, 5/8, 6/8,
7/8, 1, 9/8, 10/8, 11/8, 12/8}},
GridLinesStyle -> Directive[Dashed]]


in an article I'm writing. I don't like how the GridLines overlap the axis labels however. I searched around a bit for a solution but I'm no Mathematica expert and I can't work it out. Does anyone have a solution?

-
One small remark, you could use Ticks -> {Range[0,2Pi,Pi/6],Range[0,1.5,0.125]} and the same idea for your GridLines - does the same but is clearer. –  Ronny Mar 28 '12 at 11:43
@Ronny thanks, as I said I'm not really very good at Mathematica. I tend (in things related to Mathematica) to learn the bare minimum required to get what I want :). –  Glen Wheeler Mar 28 '12 at 12:23
Just added the plot graphics so people can see what ails you. –  Yves Klett Mar 28 '12 at 13:24

Setting PlotRangePadding to zero is probably the easiest option, but as an alternative you could draw the grid lines by hand using ProLog:

ticks = {Range[0, 2 Pi, Pi/6], Range[0, 1.5, .125]};
Plot[1/Root[-3 - 8 #1 Tan[a/2] -
3 Tan[a/2]^2 + #1^4 (1 + Tan[a/2]^2) + #1^2 (-9 +
3 Tan[a/2]^2) &, 2], {a, 0, 2 Pi}, AxesOrigin -> {0, 0},
Ticks -> ticks,
Prolog ->
Style[{Gray, Dashed,
Line[{{#, 0}, {#, 1.4}}] & /@ Rest[ticks[[1]]],
Line[{{0, #}, {2 Pi + .2, #}}] & /@ Rest[ticks[[2]]]},
Antialiasing -> False]]


-
This is my favourite, since Prolog is something I should have thought of to use. I wasn't certain of the syntax but I did at least know it existed! About the axes: I really don't like the decimal point after the 1, so will write that out as before. I had no idea it would work for the radians though. Cheers! –  Glen Wheeler Mar 28 '12 at 12:30

Try

 Plot[1/Root[-3 - 8 #1 Tan[a/2] - 3 Tan[a/2]^2 + #1^4 (1 + Tan[a/2]^2) + #1^2 (-9 +
3 Tan[a/2]^2) &, 2], {a, 0, 2 Pi}, AxesOrigin -> {0, 0},
Frame -> {True, True, False, False},
FrameTicks -> {{{0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1,
1.125, 1.25, 1.375, 1.5}, None},
{{Pi/6, Pi/3, Pi/2, 2 Pi/3, 5 Pi/6, Pi, 7 Pi/6, 4 Pi/3,
3 Pi/2, 5 Pi/3, 11 Pi/6, 2 Pi}, None}},
GridLines -> {{0, Pi/6, Pi/3, Pi/2, 2 Pi/3, 5 Pi/6, Pi, 7 Pi/6,
4 Pi/3, 3 Pi/2, 5 Pi/3, 11 Pi/6, 2 Pi}, {0, 1/8, 2/8, 3/8, 4/8,
5/8, 6/8, 7/8, 1, 9/8, 10/8, 11/8, 12/8}},
GridLinesStyle -> Directive[Dashed]]


to get

-
Thanks, although I really didn't want to move the axes. –  Glen Wheeler Mar 28 '12 at 12:28
@GlenWheeler, you need to add PlotRangePadding->0; or, better yet, just use PlotRangePadding as in Verbeia's answer. –  kguler Mar 30 '12 at 9:08

An alternative to kguler's approach is to set PlotRangePadding to zero on the relevant sides.

Notice I've simplified your grid and tick specifications using Range. The y-axis ticks need to be machine-precision (1/8. increment) or they will actually be fractions. If you don't like the decimal point after the 0 and 1, then the full listing as in your question is needed for the ticks (but not the gridlines).

Plot[1/Root[-3 - 8 #1 Tan[a/2] -
3 Tan[a/2]^2 + #1^4 (1 + Tan[a/2]^2) + #1^2 (-9 +
3 Tan[a/2]^2) &, 2], {a, 0, 2 Pi}, AxesOrigin -> {0, 0},
Ticks -> {Range[0, 2 Pi, Pi/6], Range[0, 12/8, 1/8.]},
GridLines -> {Range[0, 2 Pi, Pi/6], Range[0, 12/8, 1/8]},
GridLinesStyle -> Directive[Dashed],
PlotRangePadding -> {{0, Scaled[0.02]}, {0, Scaled[0.02]}}]


-
Thanks for the answer, now I've learnt PlotRangePadding... had no idea what it did before (surely not designed to solve this problem). –  Glen Wheeler Mar 28 '12 at 12:29
Stick around, there are plenty of little-known corners of Mathematica's graphics functionality that get explored on this site! And a few German-speaking Aussies. –  Verbeia Mar 28 '12 at 12:33

Building on @Verbeia's solution, you may want to shorten the tick mark length by using the third parameter of Ticks. This leaves both a negative and positive tail for each tick (see pic 1, below):

Plot[1/Root[-3 - 8 #1 Tan[a/2] - 3 Tan[a/2]^2 + #1^4 (1 + Tan[a/2]^2) + #1^2 (-9 +
3 Tan[a/2]^2) &, 2], {a, 0, 2 Pi}, AxesOrigin -> {0, 0},
Ticks -> {Range[0, 2 Pi, Pi/6] /. {n_?NumericQ -> {n, n, 0.01}},
Range[0, 12/8, 1/8.] /. {n_?NumericQ -> {n, n, 0.01}}},
GridLines -> {Range[0, 2 Pi, Pi/6], Range[0, 12/8, 1/8]},
GridLinesStyle -> Directive[Dashed],
PlotRangePadding -> {{0, Scaled[0.02]}, {0, Scaled[0.02]}}]


Or you may want to get rid of ticks altogether, using ticks of zero length by using /.{n_?NumericQ -> {n, n, 0}} instead of /.{n_?NumericQ -> {n, n, 0.01}}.

-