# How to have a special grid as background of a plot?

I want to have a grid background for the function (2/5)*x+3/5 but with one small detail. I want the coordinates of the points to be in the centres of the background squares, not on their corners. I am playing with Bresenham's line algorithm so I want to see nearest to the centre of which squares a line goes through.

• I'm not sure what do you want but this: mathematica.stackexchange.com/q/37460/5478 is related for sure.
– Kuba
Jan 24, 2014 at 10:51
• I want to shift the GridLines by 0.5 to the right and down, so that the point (1;1) for example is exactly in the middle of a square of a grid instead of on a corner. Jan 24, 2014 at 10:55
• Something like Plot[(2/5)*x + 3/5, {x, 0, 20}, GridLines -> {Range[.5, 20, 1], Range[.5, 20, 1]}, AspectRatio -> .5, ImageSize -> 500, Prolog -> RegionPlot[ Floor[(2/5)*x + 3/5] - .0 <= y <= Ceiling[(2/5)*x + 3/5] + .0, {x, 0, 20}, {y, 0, 10}, Axes -> False, AspectRatio -> .5, PlotStyle -> Opacity[.2]][[1]]] (This is just to get some idea of what you're after, not any real solution).
– ciao
Jan 24, 2014 at 11:12
• Yes, @rasher, this is exactly what I want, please post it as an answer. Jan 24, 2014 at 11:14
• @AlexPopov- OK, but as I said, it was a proof-of-concept kind of thing, you'll probably want to tweak it!
– ciao
Jan 24, 2014 at 11:15

As requested:

Plot[(2/5)*x + 3/5, {x, 0, 20},
GridLines -> {Range[.5, 20, 1], Range[.5, 20, 1]}, AspectRatio -> .5,
ImageSize -> 500,
Prolog ->
RegionPlot[
Floor[(2/5)*x + 3/5] - .0 <= y <= Ceiling[(2/5)*x + 3/5] + .0, {x,
0, 20}, {y, 0, 10}, Axes -> False, AspectRatio -> .5,
PlotStyle -> Opacity[.2]][[1]]]


I'm sure there are more elegant ways...

• I posted an extension of this as an answer below. If you would prefer that I make it an edit to your answer I can do that instead. Jan 26, 2014 at 8:01
• Good lord no, like I said in my comment to the OP, it was a proof-of-concept that ended up being useful to them. You've turned it into something actually usable for generalized cases, certainly worthy of it's own answer and credit!
– ciao
Jan 26, 2014 at 8:10
• Okay, cool. :-) Jan 26, 2014 at 8:21

Here is an attempt to package rasher's method into something a bit more easily reused:

SetAttributes[plotWithGrid, HoldAll]

plotWithGrid[fn_, {v_, x_, X_}, opts : OptionsPattern[Plot]] :=
Module[{gf, plot, fill},
gf[lo_, hi_] := Range[Floor[lo, 1/2], Ceiling[hi, 1/2]];
plot = Plot[fn, {v, x, X}, GridLines -> {gf, gf}, opts];
fill = PlotRange[plot] /. {_, {y_, Y_}} :>
RegionPlot[⌊fn⌋ ≤ yy ≤ ⌈fn⌉,
{v,  x - 0.1 (X - x), X + 0.1 (X - x)},
{yy, y - 0.1 (Y - y), Y + 0.1 (Y - y)},
Axes -> False,
Frame -> False,
PlotStyle -> Opacity[.2],
PlotPoints -> 120
];
Show[plot, Prolog -> fill[[1]]]
]


Test:

plotWithGrid[7 Sinc[x], {x, 0, 20}, PlotRange -> All, AspectRatio -> Automatic]


I couldn't find a general setting for shifting the grindlines to I used the option for specifying manual gridlines and used this: GridLines -> {{0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5}, {-0.5, -1.5, -2.5, -3.5, -4.5, - 5.5}}.

• Try {Range[.5, 11.5, .5], Reverse@Range[-5.5, -.5, .5]}
– Kuba
Jan 24, 2014 at 11:26