user1096734
user1096734

Reputation:

How do I get rid of gray boundaries for ArrayPlot in Mathematica?

I have the following plot.

lst={{1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 
  0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 
  0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 0, 0, 0, 
  1}, {1, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1}};
ArrayPlot[lst, Mesh -> All, 
 MeshStyle -> Directive[AbsoluteThickness[3.], Gray, Opacity[0.1]]]

enter image description here

But it does not look as I expected, in which I want the grey boundaries/grids for the black squares to be overshadowed by the color of these black squares. Only show the gray boundaries/grids of the white squares.

Upvotes: 4

Views: 621

Answers (2)

Mike Honeychurch
Mike Honeychurch

Reputation: 1683

How about just ditching grid lines and mesh lines and using Epilog and Line?

ArrayPlot[lst, Mesh -> False, Frame -> False, 
 Epilog -> {GrayLevel[0.5], AbsoluteThickness[1], 
   Line@Table[{{2 + i, 8}, {2 + i, 1}}, {i, 0, 5}], 
   Line@Table[{{1, 2 + i}, {8, 2 + i}}, {i, 0, 5}]}]

enter image description here

This is obviously specific to this list of data but is straight forward to generalize to data where you have "x" unit black "perimeter" and "y" times "y" white square (i.e. a y+2x list of rows and columns).

gridArrayPlot[mat_?MatrixQ] := Module[{dim = First@Dimensions@mat, 
  white = Length@Cases[mat, {__, 0 .., __}], black, left, right, grid},

  black = (dim - white)/2;
  left = black + 1;
  right = dim - black;
  grid = white - 2;

  ArrayPlot[mat, Mesh -> False, Frame -> False, 
   Epilog -> {GrayLevel[0.5], AbsoluteThickness[1], 
     Line@Table[{{left + i, right}, {left + i, black}}, {i, 0, grid}],
      Line@Table[{{black, left + i}, {right, left + i}}, {i, 0, grid}]}]

  ]

Upvotes: 3

abcd
abcd

Reputation: 42225

This isn't something that can be easily solved using built in options (AFAIK). You can define a custom function that plots the gridlines only at those rows and columns that you need and masks the others. Here is my solution:

gridArrayPlot[mat_?MatrixQ, options___] := Module[{dim = Dimensions@mat},
  Show[
    ArrayPlot[mat, Mesh -> ({Range[#1 - 1], Range[#2 - 1]} & @@ dim), options],
    ArrayPlot[mat, Mesh -> ({{0, #1}, {0, #2}} & @@ dim), 
       ColorRules -> {0 -> Directive[Opacity@0, White]}, 
       options /. Opacity[_] :> Opacity[1] /. (RGBColor[___] | GrayLevel[_]) :> White
    ]
  ]
]

The above solution first plots an ArrayPlot, drawing the mesh everywhere except at the outer boundaries and overlays a second ArrayPlot with the White cells set to be transparent, and draws White mesh lines on the outer boundary (to mask the bits sticking out from the previous plot).

You can call the above function as

gridArrayPlot[lst,MeshStyle -> Directive[AbsoluteThickness[3.],Gray,Opacity[0.1]]]

and the output is:

enter image description here

Upvotes: 4

Related Questions