How to get rid of gray borders for ArrayPlot in Mathematica?

I have the following chart.

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 this does not look the way I expected, in which I want the gray borders / grids for the black squares to be clouded by the color of these black squares. Show only gray borders / grids of white squares.

+4
source share
2 answers

This is not something that can be easily solved with the built-in options (AFAIK). You can define a custom function that displays grid lines only in the rows and columns that you need, and masks 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 displays a ArrayPlot , drawing a grid everywhere except for the outer borders, and overlays a second ArrayPlot with White cells that should be transparent, and draws t23> grid lines on the outer (to mask the bits sticking out of the previous graph).

You can call the above function as

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

and output:

enter image description here

+4
source

How about just mesh grooves 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 data list, but directly to summarize the data, where you have an β€œx” unit black β€œperimeter” and β€œy” times β€œy” a white square (that is, a list of rows y + 2x 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}]}] ] 
+3
source

Source: https://habr.com/ru/post/1388747/


All Articles