5
$\begingroup$

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.

$\endgroup$

1 Answer 1

8
$\begingroup$
{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]]]

enter image description here

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

enter image description here

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}}]

enter image description here

Show[ap, llp]

enter image description here

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}, 
  PlotRangePadding -> None];

Show[gr, llp]

enter image description here

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]} &), 
   PlotRangePadding -> None];

Show[bc, llp]

enter image description here

$\endgroup$
10
  • $\begingroup$ Doesn't this generate a rotated version of the desired graph? $\endgroup$ Mar 29, 2021 at 3:07
  • $\begingroup$ @DescartesBeforetheHorse, you are right. Updated with a fix. $\endgroup$
    – kglr
    Mar 29, 2021 at 3:28
  • $\begingroup$ 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. $\endgroup$ Mar 29, 2021 at 6:13
  • $\begingroup$ @DescartesBeforetheHorse, if the issue arises for ArrayPlot only, it could be related to the issue covered in this Q/A $\endgroup$
    – kglr
    Mar 29, 2021 at 6:42
  • 1
    $\begingroup$ @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]) $\endgroup$
    – kglr
    Mar 29, 2021 at 21:10

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.