8
$\begingroup$

I am trying to place axes into ArrayPlot. I would like to get something that looks like this:

enter image description here

So in addition to the Frame and FrameTicks I would like to place Axes with specific AxesOrigin into ArrayPlot. Here is my code so far:

array = Join[Join[{0., 0.}, #] & /@ RandomReal[1, {8, 8}], 
ConstantArray[0., {2, 10}]];

ArrayPlot[array, Mesh -> True, Frame -> True, 
FrameTicks -> {Table[i, {i, 1, 10}], Table[i, {i, 1, 10}]}, 
Axes -> True, AxesStyle -> Red, AxesOrigin -> {2., 2.}, Ticks -> Automatic, 
TicksStyle -> Red]

enter image description here

I could not get option Ticks to work. I will be grateful for any suggestion.

$\endgroup$
1

1 Answer 1

7
$\begingroup$

Update: Making the two methods in the original answer into functions:

The first method:

ClearAll[aP]
aP = Module[{dt = #, ao = #2, dim = Dimensions[Transpose @ #] + #2, 
          dt2 = ArrayPad[#, {{0, #2[[2]]}, {#2[[1]], 0}}], tcks, inset}, 
        tcks = {Table[{i + .5, i + 1}, {i, ao[[1]], dim[[1]] - 1}], 
            Table[{i + .5, dim[[2]] - i}, {i, ao[[2]], dim[[2]] - 1}]};
        inset = ArrayPlot[dt2, AxesOrigin -> ao, Ticks -> tcks, ##3, 
            Axes -> True, Frame -> False, Mesh -> All, 
            AxesStyle -> Directive[Thick, Red], 
            TicksStyle -> Directive[Thick, Red, FontColor -> Black]];
        ArrayPlot[ConstantArray[0, Reverse@dim], 
          Epilog -> Inset[inset, {0, 0}, {0, 0}, Scaled[1]], ##3, 
          FrameTicks -> ({{#2, #2}, {#, #}} & @@ Range@dim)]] &;

Examples:

SeedRandom[1]
data = RandomReal[1, {7, 9}];

Row[Labeled[Panel@aP[data, #, ImageSize -> 1 -> 25], 
   Row[{"AxesOrigin -> ", #}], Top] & /@ {{2, 2}, {1, 3}, {3, 2}}, Spacer[10]]

enter image description here

The second method:

ClearAll[epilogF]
epilogF[data_, ao_] := Module[{dim = Dimensions[Transpose @ data] + ao}, 
  {Table[{Text[i + 1, # - {0, .25}], Red, Thick, 
       Line[{Offset[{0, 3}, #], #}]} & @ {i + .5, ao[[2]]},
     {i, ao[[1]], dim[[1]] - 1}], 
   Table[{Text[dim[[2]] - i, # - {.25, 0}], Red, Thick, 
       Line[{Offset[{3, 0}, #], #}]} & @ {ao[[1]], i + .5},
     {i, ao[[2]], dim[[2]] - 1}]}];

Examples:

SeedRandom[1]

data = RandomReal[1, {9, 7}];

Row[Labeled[Panel @ ArrayPlot[ArrayPad[data, {{0, #[[2]]}, {#[[1]], 0}}], 
        ImageSize -> 1 -> 25, 
        Mesh -> All, 
        GridLines -> (List /@ #), 
        GridLinesStyle -> Directive[Red, Thick], 
        Method -> {"GridLinesInFront" -> True},
        Frame -> True, 
        FrameTicks -> Automatic, 
        Epilog -> epilogF[data, #]], 
      Row[{"AxesOrigin -> ", #}], Top] & /@ {{2, 2}, {1, 3}, {3, 2}}, 
   Spacer[10]]

enter image description here

Original answer:

SeedRandom[1]
data = RandomReal[1, {8, 8}];

ao = {2, 2};

dims = Dimensions[Transpose @ data] + ao;

ticks = {Table[{i + .5, i + 1}, {i, ao[[1]], dims[[1]]}], 
   Table[{i + .5, dims[[2]] - i}, {i, ao[[2]], dims[[2]]}]};

ap = ArrayPlot[ArrayPad[data, {{0, ao[[2]]}, {ao[[1]], 0}}],
   AxesOrigin -> ao, Axes -> True, Frame -> False, Mesh -> All, 
   AxesStyle -> Directive[Thick, Red], 
   TicksStyle -> Directive[Thick, Red, FontColor -> Black],
   Ticks -> ticks]

You can use Inset[ap, {0, 0}, {0, 0}, Scaled[1]] as Epilog in an empty ArrayPlot to get the desired result:

ArrayPlot[ConstantArray[0, Reverse @ dims], 
 Epilog -> Inset[ap, {0, 0}, {0, 0}, Scaled[1]],
 FrameTicks -> Automatic]

enter image description here

An alternative approach: add ticks and tick labels as Epilog directly:

epilog = Join[
   Table[{Text[i + 1, #], Red, Thick, 
       Line[{Offset[{0, 3}, # + {0, .5}], # + {0, .5}}]} & @
          {i + .5, ao[[2]] - .5}, {i, ao[[1]], dims[[1]] - 1}], 
   Table[{Text[dims[[2]] - i, #], Red, Thick, 
       Line[{Offset[{3, 0}, # + {.25, 0}], # + {.25, 0}}]} &@
          {ao[[1]] - .25, i + .5}, {i, ao[[2]], dims[[2]] - 1}]];

ArrayPlot[ArrayPad[data, {{0, ao[[2]]}, {ao[[1]], 0}}],
  Mesh -> All, 
  GridLines -> (List /@ ao), 
  GridLinesStyle -> Directive[Red, Thick], 
  Method -> {"GridLinesInFront" -> True}, 
  Frame -> True, 
  FrameTicks -> Automatic, 
  Epilog -> epilog]

enter image description here

$\endgroup$

Your Answer

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

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