# Plot Command With Clean Grid Lines

I am trying to emulate this plot using Mathematica. Using the following command, I cannot get the grid points correct.

  Plot[{x, x^2, x^3}, {x, -4, 4}, Frame -> True, FrameTicks -> All,
FrameTicksStyle -> {{Black, Blue}, {Red, Green}},
GridLines -> {Range[-4, 4], Range[-25, 25]}]


This results in: Is there something that can be done to get it to look like the first plot (which was done using Microsoft Equation)?

• You can use the third argument of Range to control the distance between gridlines and you can use GridLinesStyle to control their appearance. – C. E. Jan 8 '16 at 7:56
• For the internal ticks this may be of help – Ymareth Jan 8 '16 at 8:42

## 2 Answers

If all you need to do is make the gridlines look better, just change your plot command to have these options GridLines -> {Range[-4, 4, .5], Range[-32, 32, 4]},GridLinesStyle -> LightGray. But if you want to fully reproduce that plot, see below.

Something like this is not an exact reproduction

Plot[{x, x^2, x^3}, {x, -4, 4},
PlotRange -> {-33, 33},
Frame -> True,
Ticks -> {LinTicks, LinTicks},
FrameTicks -> All,
FrameTicksStyle -> {{Black, Blue}, {Red, Green}},
GridLines -> {Range[-4, 4, .5], Range[-32, 32, 4]},
GridLinesStyle -> LightGray,
ImageSize -> 600,
FrameStyle -> ColorData,
AxesStyle -> Directive[ColorData, Arrowheads[0.03]]] But man, would be nice if we could have tick marks on the axes as well as on the frame. According to this post this is impossible without using Overlay (and I hate using Overlay). There is a neat solution using extraAxisPlot from this post

baseplot = Plot[{x, x^2, x^3}, {x, -4, 4},
PlotRange -> {-33, 33},
Frame -> True,
FrameTicks -> All,
FrameTicksStyle -> {{Black, Blue}, {Red, Green}},
GridLines -> {Range[-4, 4, .5], Range[-32, 32, 4]},
GridLinesStyle -> LightGray,
ImageSize -> 600,
FrameStyle -> ColorData,
Axes -> False];

extraAxisPlot[baseplot, {{-3.8, -30}, {3.8, 30}}, {-3.8, -30},
"AxisType" -> "Both",
"AxisOptions" -> {AxesStyle ->
Directive[ColorData, Arrowheads[0.03]],
LabelStyle -> {FontOpacity -> 0}}] Edit As Nasser says it is hard to put the tick labels inside the frame - you have to use Heike's solution, but then that Epilog will override the Epilog that shows the axes so I need to give another option to extraAxisPlot

baseplot = Plot[{x, x^2, x^3}, {x, -4.2, 4.2},
PlotRange -> {-34, 34},
Frame -> True,
FrameTicks -> All,
FrameTicksStyle -> (FontOpacity -> 0),
GridLines -> {Range[-4, 4, .5], Range[-32, 32, 4]},
GridLinesStyle -> LightGray,
ImageSize -> 600,
FrameStyle -> ColorData,
Axes -> False];

Show[extraAxisPlot[baseplot, {{-3.8, -30}, {3.8, 30}}, {-3.8, -30},
"AxisType" -> "Both",
"AxisOptions" -> {AxesStyle ->
Directive[ColorData, Arrowheads[0.03]],
LabelStyle -> {FontOpacity -> 0}},
"ExtraEpilog" -> {Table[
Text[i, {i, -34}, {0, -1}, BaseStyle -> ColorData], {i,
Range[-4, 4, 2]}],
Table[
Text[i, {i, 34}, {0, 1}, BaseStyle -> ColorData], {i,
Range[-4, 4, 2]}],
Table[
Text[i, {-4.2, i}, {-.5, 0}, BaseStyle -> ColorData], {i,
Range[-30, 30, 10]}],
Table[
Text[i, {4.2, i}, {.5, 0}, BaseStyle -> ColorData], {i,
Range[-30, 30, 10]}]}]] maybe this is a start. Putting the ticks inside is kind'a hard. (ps. the yellow background, is from Mathematica itself. I have to accept yellow color in place of pausing when opening any new notebook or window. So I took the yellow color. Mathematica graphic card bug on windows) glx = Table[
If [Mod[i, 2] ==
0, {i, {Blue, Thickness[0.005]}}, {i, {Thin, Blue}}], {i, -4, 4, .5}];
gly = Table[
If [Mod[i, 16] ==
0, {i, {Blue, Thickness[0.005]}}, {i, {Thin, Blue}}], {i, -32, 32, 4}];
Plot[{x, x^2, x^3}, {x, -4, 4},
Frame -> True,
FrameTicksStyle -> Blue,
GridLines -> {glx, gly},
PlotRange -> {{-4.5, 4.5}, {-36, 36}},
ImageSize -> 500,
Axes -> False,
Epilog -> {
{Thick, Arrowheads[Small], Arrow[{{-3, 0}, {3, 0}}]},
{Thick, Arrowheads[Small], Arrow[{{0, -30}, {0, 30}}]},
Text[Style[Style["y"], Italic], {-.1, 28}],
Text[Style["x", Italic], {3, -2}]
},
PlotStyle -> {FontFamily -> "Times"},
FrameTicks -> {{Range[-32, 32, 16], Range[-32, 32, 16]}, {Range[-4, 4, 2],
Range[-4, 4, 2]}}
]

• Could this be related to your yellow tint? link – AxelF Jan 8 '16 at 12:36
• @AxelF exactly. After making that "fix" in the question there, my Mathematica got inflicted with the dreaded Formatting Notebook Contents problem each time I open any new notebook or do anything. So I removed the graphic card update and went back to the yellow Mathematica, but now without having to suffer with Formatting Notebook Contents. 10.3.1 on windows 7, 64 bit. – Nasser Jan 8 '16 at 19:33