4
$\begingroup$

I want to create a Grid or Plot (or equivalent) from the following Table:

Module[{size = 10}, Table[If[PrimeQ[a + b], True], {a, 1, size}, {b, 1, size}]]

(*
{{True, True, Null, True, Null, True, Null, Null, Null, True},
{True, Null, True, Null, True, Null, Null, Null, True, Null},
{Null, True, Null, True, Null, Null, Null, True, Null, True},
{True, Null, True, Null, Null, Null, True, Null, True, Null},
{Null, True, Null, Null, Null, True, Null, True, Null, Null},
{True, Null, Null, Null, True, Null, True, Null, Null, Null},
{Null, Null, Null, True, Null, True, Null, Null, Null, True},
{Null, Null, True, Null, True, Null, Null, Null, True, Null},
{Null, True, Null, True, Null, Null, Null, True, Null, True},
{True, Null, True, Null, Null, Null, True, Null, True, Null}}
*)

I want the output to have the following properties:

  1. The two 'axes' a and b are marked, and have ticks at integer values;
  2. There is a grid of lines showing integer intervals from both axes;
  3. At each intersection with Cartesian coordinates (a,b) where the Table output is True, there should be a Point; at Null points there should be nothing.

This seems like it should be really simple, but for some reason I can't quite get my head around the syntax...

How do I do it?

$\endgroup$
  • $\begingroup$ Many thanks for the excellent answers. I have chosen the most general one to mark as answered, but they are all both useful and instructive. $\endgroup$ – Richard Burke-Ward Jun 26 at 21:31
2
$\begingroup$

Inspired by @kglr and extending to arbitrary dimensions on the data, this may do what you want:

data = Module[{size = 11}, 
  Table[If[PrimeQ[a + b], True], {a, 1, size}, {b, 1, size}]];
With[{elements = Max[Dimensions[data]]}, 
 ListPlot[Position[data, True], 
  PlotStyle -> Directive[Black, PointSize[Large]], Frame -> True, 
  AspectRatio -> 1, 
  FrameTicks -> {{Range[elements], 
     Thread[{Range[elements], ""}]}, {Range@elements, 
     Thread[{Range[elements], ""}]}}, 
  GridLines -> {Range[elements], Range[elements]}, 
  PlotRange -> {{1, elements}, {1, elements}}, 
  PlotRangeClipping -> False]]

Shown with size set to 11 to show that it fits the generality.
enter image description here

| improve this answer | |
$\endgroup$
4
$\begingroup$

Visualized as squares rather than points

ArrayPlot[data,
 Frame -> True,
 FrameTicks -> {{Range@10, Range@10}, {Range@10, Range@10}},
 DataReversed -> True,
 PlotRangePadding -> None,
 Mesh -> All]

enter image description here

| improve this answer | |
$\endgroup$
2
$\begingroup$

How about BubbleChart?

tab = With[{n = 10}, PrimeQ @ Array[Plus, {n, n}]];

BubbleChart[Append[1] /@ Position[tab, True], GridLines -> (Range[1, #2] &)];

Labeled[%, {"A", "B"}, {Bottom, Left}]

enter image description here

Adding FrameTicks -> (Range[1, #2] &) gives:

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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