Representation a matrix as a colored square shape
Update 2020-01-07
At this point there is a Wolfram Function Repository (WFR) RandomScribble
.
Here is matrix representation with "midriff" random scribbles based WFR's RandomScribble
:
matrix =
{{1, -1, 1, 1, -1},
{-1, -1, -1, 1, 1},
{1, 1, 1, -1, 1},
{-1, 1, -1, 1, -1},
{1, 1, 1, -1, -1}};
SeedRandom[23];
Magnify[Grid[
matrix /. {-1 :> OrangeScribble[], 1 :> DarkGreenScribble[]},
Dividers -> All], 0.4]
(Note that this answer by @kglr has a different approach to making "midriff" scribbles. )
Definitions
Clear[OrangeScribble];
OrangeScribble[opts___] :=
ResourceFunction["RandomScribble"][opts,
"EnvelopeFunctions" -> Automatic, "NumberOfStrokes" -> 200,
"RotationAngle" -> Pi/4, ColorFunction -> "SiennaTones",
PlotStyle -> {Orange, AbsoluteThickness[3]}];
Clear[DarkGreenScribble];
DarkGreenScribble[opts___] :=
ResourceFunction["RandomScribble"][opts,
"EnvelopeFunctions" -> Automatic, "NumberOfStrokes" -> 200,
"RotationAngle" -> Pi/4, ColorFunction -> "AvocadoColors",
PlotStyle -> {Orange, AbsoluteThickness[3]}];
Original answer
matrix =
{{1, -1, 1, 1, -1},
{-1, -1, -1, 1, 1},
{1, 1, 1, -1, 1},
{-1, 1, -1, 1, -1},
{1, 1, 1, -1, -1}};
SeedRandom[23];
Grid[matrix /. {-1 :> OrangeScribble[], 1 :> DarkGreenScribble[]}, Dividers -> All]
Update
A problem: If I change the number of raws and columns, then the size of the shape goes very big. I wish to change my matrix to for example :10*10.
matrix2 = ArrayFlatten[Table[matrix, 2, 2]];
Magnify[Grid[
matrix2 /. {-1 :> OrangeScribble[], 1 :> DarkGreenScribble[]},
Dividers -> All], 0.4]
Definitions
Clear[RandomScribble];
Options[RandomScribble] =
Join[{AbsoluteThickness -> 2, ColorFunction -> ColorData[87]}, Options[Graphics]];
RandomScribble[nPoints_Integer, opts : OptionsPattern[]] := RandomScribble[nPoints, \[Pi]/4, opts];
RandomScribble[nPoints_Integer, dir_?NumericQ, opts : OptionsPattern[]] :=
Block[{r, absTh, colorFunc},
absTh = OptionValue[RandomScribble, AbsoluteThickness];
colorFunc = OptionValue[RandomScribble, ColorFunction];
If[! (NumericQ[absTh] && absTh > 0), Return[$Failed]];
r = RandomReal[{-1, 1}, {nPoints, 2}];
Graphics[{
AbsoluteThickness[absTh], colorFunc[0],
BezierCurve[Sort[r].RotationMatrix[dir]]
},
FilterRules[{opts}, Options[Graphics]]]
];
OrangeScribble[] := RandomScribble[RandomInteger[{160, 190}], \[Pi]/4, ColorFunction -> (Orange &), ImageSize -> Tiny];
DarkGreenScribble[] := RandomScribble[RandomInteger[{120, 180}], \[Pi]/4, ColorFunction -> (Darker[Green] &), ImageSize -> Tiny];
As @AntonAntonov says, use MatrixPlot
:
matrix = {{ 1, -1, 1, 1, -1},
{-1, -1, -1, 1, 1},
{ 1, 1, 1, -1, 1},
{-1, 1, -1, 1, -1},
{ 1, 1, 1, -1, -1}};
MatrixPlot[matrix,
ColorRules -> {-1 -> Orange, 1 -> Darker@Green},
Frame -> False]
I misunderstood the meaning of square color before, here is my new answer:
matrix = Array[RandomChoice[{-1, 1}] &, {10, 10}];
positionA = Position[matrix, 1];
positionB = Position[matrix, -1];
Grid[matrix, Frame -> All,
Background -> {None, None,
Join[Rule[#, Green] & /@ positionA,
Rule[#, Orange] & /@ positionB]},
ItemStyle -> {Automatic, Automatic,
Join[Rule[#, White] & /@ positionA,
Rule[#, Black] & /@ positionB]}]