# Plotting list of points as a checkerboard

I have a list of 2-tuples list. I would like to plot these tuples as colored-in squares like a checkerboard (except not checkered--the dark squares would only appear at the squares whose position correspond to some point in list.)

Additionally, I do not want the graph to have an overlayed grid.

The only way I know how to do something like this is with ListPlot by setting PlotMarkers -> "\[FilledSquare]". But this seems to create problems with offset that make me wonder if there is a better aproach.

{nrows, ncols} = {20, 30};
samplesize = 90;

list = RandomSample[Tuples@Range@{ncols, nrows}, samplesize];


### ListPlot

You can post-process ListPlot output to replace Points with Rectangles:

llp = ListPlot[list, PlotStyle -> Directive[Red, PointSize[Medium]]]


Show[llp /. p_Point :> {{Black, Rectangle[# - 1/2] & /@ p[[1]]}, p},
Frame -> True, GridLines -> {Range@30 - 1/2, Range@20 - 1/2}]


### ArrayPlot

You can construct a SparseArray from list and use it with ArrayPlot:

data = SparseArray[Reverse /@ list -> 1, {nrows, ncols}];

ap = ArrayPlot[Reverse @ data,
Frame -> True, FrameTicks -> Automatic,
Mesh -> All,
DataReversed -> False,
DataRange -> {{1, 30}, {1, 20}}]


Show[ap, llp]


### Graphics

Alternatively, you can construct Rectangle primitives from list and use Graphics:

gr = Graphics[{Black, Rectangle[# - 1/2] & /@ list}, Frame -> True,
GridLines -> {Range[30] - 1/2, Range[20] - 1/2},

Show[gr, llp]


### BubbleChart

Append 1 to each pair in list and use the resulting data with BubbleChart with custom ChartElementFunction:

bc = BubbleChart[Append[1] /@ list, AspectRatio -> 2/3,
GridLines -> {Range@30 - 1/2, Range@20 - 1/2},
Method -> {"GridLinesInFront" -> True},
ChartElementFunction -> ({Black, Rectangle[Mean /@ # - 1/2]} &),

Show[bc, llp]


• Doesn't this generate a rotated version of the desired graph? Commented Mar 29, 2021 at 3:07
• @DescartesBeforetheHorse, you are right. Updated with a fix.
– kglr
Commented Mar 29, 2021 at 3:28
• For some reason, if you create a plot this way and export it as a PDF, it does not export the same graphic shown in Mathematica. The squares are replaced with bubbles. Commented Mar 29, 2021 at 6:13
• @DescartesBeforetheHorse, if the issue arises for ArrayPlot only, it could be related to the issue covered in this Q/A
– kglr
Commented Mar 29, 2021 at 6:42
• @Moo, I thought the issue was with the PDF export of ap (without the red points) . For the PDF export of Show[ap, llp] it is possible to have red points cover the black squares depending on the image size (because of the setting PointSize[Medium])
– kglr
Commented Mar 29, 2021 at 21:10