5
$\begingroup$

enter image description hereI have a 2-D table with numbers 1,2,3,4 and 5 populated in it in a continuous form. From it, I like to create a region plot with five regions separated in this space. Is there any easy way to achieve this?

matrix = {{5,5,5,5,5,5,5,5,5,3,3,3,3,3,3,3,1,1,1},{5,5,5,5,5,5,5,5,5,3,3,3,3,3,3,1,1,1,1},{5,5,5,5,5,5,5,5,5,3,3,3,3,3,1,1,1,1,1},{5,5,5,5,5,5,5,5,3,3,3,3,3,1,1,1,1,1,1},{5,5,5,5,5,5,5,3,3,3,3,3,1,1,1,1,1,1,1},{5,5,5,5,5,5,3,3,3,3,3,1,1,1,1,1,1,1,1},{5,5,5,5,5,5,3,3,3,3,1,1,1,1,1,1,1,1,1},{5,5,5,5,5,3,3,3,2,1,1,1,1,1,1,1,1,1,1},{5,5,5,5,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1},{5,5,5,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1,1},{5,5,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1},{5,3,3,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{3,3,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{3,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{2,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,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,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}}
$\endgroup$
  • $\begingroup$ does this give something close to what you have in mind : ListDensityPlot[Reverse@matrix, InterpolationOrder -> 0]? $\endgroup$ – kglr Nov 29 '20 at 1:18
  • $\begingroup$ I cannot use color. I need to achieve this goal without using colors, but only boundary to separate the regions. $\endgroup$ – Monire Jalili Nov 29 '20 at 1:20
  • $\begingroup$ or ListContourPlot[Reverse@matrix, Contours -> (Union[Flatten@matrix] - 1/2), ContourShading -> None]? $\endgroup$ – kglr Nov 29 '20 at 1:20
  • $\begingroup$ This is much closer to what I want. Can we remove the colors and make it transparent? $\endgroup$ – Monire Jalili Nov 29 '20 at 1:22
4
$\begingroup$
ListContourPlot[matrix, Contours -> (Union[Flatten@matrix] - 1/2), 
 ContourShading -> None, ScalingFunctions -> {None, "Reverse"}]

enter image description here

Update: " a way to make the lines on the contour plot show the step shape as in the real table":

contours = ComponentMeasurements[matrix, "Contours"];

Graphics[Values @ contours]

enter image description here

ArrayPlot[matrix, ColorFunction -> "Rainbow", 
 Epilog -> {Thick, Values @ ComponentMeasurements[matrix, "Contours"]}, 
 FrameTicks -> All]

enter image description here

$\endgroup$
  • $\begingroup$ Is there a way to make the lines on the contour plot show the step shape as in the real table? @kglr $\endgroup$ – Monire Jalili Nov 30 '20 at 20:51
  • $\begingroup$ @Monire, please see the update. $\endgroup$ – kglr Nov 30 '20 at 21:02
  • 1
    $\begingroup$ @MonireJalili Maybe this variant is helpful: HighlightImage[Colorize[matrix, ImageSize -> Medium], {Black, Values@ComponentMeasurements[matrix, "Contours"]}] $\endgroup$ – Michael E2 Dec 1 '20 at 17:54
2
$\begingroup$

Edit

matrix = Array[RandomChoice[Range[5]] &, {10, 10}]; Table[
 RegionPlot[RegionUnion[Rectangle /@ Position[matrix, i]], 
  PlotPoints -> 180, BoundaryStyle -> None, 
  PlotStyle -> RandomColor[]], {i, 1, 5}]

enter image description here

Or

matrix = Array[RandomChoice[Range[5]] &, {10, 10}];
Table[Graphics[{EdgeForm[Blue], FaceForm[RandomColor[]], 
   Rectangle /@ Position[matrix, i]}], {i, 1, 5}]

enter image description here

Original

Representation a matrix as a colored square shape

Maybe like this.

matrix = Array[RandomChoice[Range[5]] &, {10, 10}];
position1 = Position[matrix, 1];
position2 = Position[matrix, 2];
position3 = Position[matrix, 3];
position4 = Position[matrix, 4];
position5 = Position[matrix, 5];
background = {None, None, 
   Join[Rule[#, Green] & /@ position1, Rule[#, Orange] & /@ position2,
     Rule[#, Cyan] & /@ position3, Rule[#, Yellow] & /@ position4, 
    Rule[#, White] & /@ position5]};
itemstyle = {Automatic, Automatic};
Grid[matrix, Frame -> All, Background -> background, 
 ItemStyle -> itemstyle]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you! This is a very cool solution. However, I am reserving the color to show a third dimension in my data. So I cannot use color. I need to simply use boundaries and borders to separate the regions from each other. $\endgroup$ – Monire Jalili Nov 29 '20 at 0:06
  • $\begingroup$ @MonireJalili My updated can solve your problem? $\endgroup$ – cvgmt Nov 29 '20 at 0:30
  • $\begingroup$ No, unfortunately, it doesn't solve my problem. I added an image in my original post to make my question a bit more clear. $\endgroup$ – Monire Jalili Nov 29 '20 at 0:46
  • $\begingroup$ @MonireJalili Please post your matrix instead of picture. $\endgroup$ – cvgmt Nov 29 '20 at 0:55
  • $\begingroup$ Sure, I just did it. Does it work this way? $\endgroup$ – Monire Jalili Nov 29 '20 at 1:03

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.