Currently I have a plot of a complicated function with gridlines turned on. Is there a way to shade a checkerboard pattern of alternating colors between adjacent grid cells?

So far, I have tried passing a table of rectangles into Prolog. However, I couldn't figure out how to pass in the max/min dimensions of the plot. Moreover, this approach is ugly and cumbersome, especially if I later want to modify the location of the gridlines.

Even better, I would like to shade a custom repeating pattern, specifically the following one:

0 | 0
1 | 0

where 1 denotes {Pink, Opacity[0.1]} and 0 denotes a white background.


1 Answer 1


You could make a checkerboard with Mesh funtionality in ParametricPlot:

mesh =
 ParametricPlot[{v, u}, {u, -2, 6}, {v, 0, 20},
  MeshFunctions -> {#2 &, #1 &}, 
  MeshShading -> {{RGBColor[1, 0.9, 0.9], White}, {White, White}},
  Mesh -> {8, 20},
  BoundaryStyle -> None

enter image description here

Then set it as a background with Prolog:

Plot[5 Sinc[x], {x, 0, 20},
  PlotRange -> All,
  PlotStyle -> {Red, Thick},
  Prolog -> mesh[[1]]

enter image description here

This method is quite flexible as you have specific control over the MeshFunctions etc. For example with MeshFunctions -> {Log[Abs@#2] &, Sinc[#1] &} you get:

enter image description here


The method above as a function for ease of application.

Options[addCheckerboard] =
  {MeshFunctions -> {#1 &, #2 &}, 
   MeshShading   -> {{RGBColor[1, 0.9, 0.9], White}, {White, White}},
   BoundaryStyle -> None};

addCheckerboard[gr_Graphics, opts : OptionsPattern[ParametricPlot]] :=
 {⌊#⌋, ⌈#2⌉} & @@@ PlotRange[gr] /. {{x_, X_}, {y_, Y_}} :>
    Prolog ->
     ParametricPlot[{u, v}, {u, x, X}, {v, y, Y},
       Mesh -> ({X - x, Y - y} - 1),
       Evaluate @ Options @ addCheckerboard

You should now be able to apply this to any Graphics object as follows:

plot =
  Plot[Evaluate[Table[n^2*BesselJ[n, x], {n, 4}]], {x, 0, 10},
   AspectRatio -> Automatic];


enter image description here

(AspectRatio -> Automatic is included in the example but not necessary for functionality.)

  • You can change the fill color with MeshShading and the grid color with MeshStyle.
  • You can override the regular grid with different MeshFunctions as above.
  • $\begingroup$ Thank you. I would like to use this in the situation where the x range is known, but the y range is determined based on the range of the function. In other words, the range of u in the parametric plot is unknown (as well as the first argument of Mesh -> {8, 20}). Would there be an easy modification to accomplish this? $\endgroup$
    – pre-kidney
    Jan 28, 2015 at 18:36
  • $\begingroup$ There is one more issue. I need the gridlines to be placed at precisely the integers. It looks like yours are not - they are stretched to fit the range you have specified. $\endgroup$
    – pre-kidney
    Jan 28, 2015 at 18:39
  • $\begingroup$ @pre-kidney by the way I simply have to ask: what's with the name? :^) $\endgroup$
    – Mr.Wizard
    Jan 28, 2015 at 22:32
  • $\begingroup$ @pre-kidney Please see the update. $\endgroup$
    – Mr.Wizard
    Jan 28, 2015 at 23:55
  • $\begingroup$ This is wonderful! But the code is a bit mysterious to me and I'm trying to learn what it's doing. Would there be a way to run it all in a single Plot command, without having to reference the plot and look up its properties using PlotRange[gr]? Essentially, I would like to do this inline without having to reference the plot later. $\endgroup$
    – pre-kidney
    Jan 29, 2015 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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