7
$\begingroup$

I have a data:

data = {"pieces" -> {"...X,...X,X..X,XXXX,XX.X", ".XX.,XXXX,..X.,..XX,..X.", 
   "...X.,XXXXX,XX..X,....X", ".XXX,.XXX,.XX.,.X..,XX..", 
   "....X,...XX,XXXXX,XXXX.,XXXX.", ".XXX.,XXXXX,XXXX.,..XXX", 
   "XX.,.X.,.XX", "..X.,.XXX,XXXX,X.XX,..X.", ".XXX,XX.X,...X", 
   "XX...,XXXXX,XXXXX,XXXX.,...X.", "...XX,XXXXX,XXXX.,.XXX.,..X..", 
   ".X.X.,.XXX.,XXXXX,XXXXX,..X..", "XX...,.X...,XXX..,.XXXX", 
   "...X,..XX,XXXX,.X..", ".X..,XX..,.XX.,XXXX,XX..", 
   ".X..,XXXX,X.XX,XXX.,XX..", "...XX,XXXXX,..XX.,..XX.", 
   ".X...,XXXX.,XXXXX,.XXXX,...XX", "XXX..,.X...,XXX..,.XXXX,.X..."}, 
 "map" -> {"00222202", "01102001", "02010000", "21022101", "22102001",
    "02102000", "21200210", "22000110", "21102000", "00222200"}, 
 "level" -> 45, "modu" -> "3"}

And I define:

PiecePlot[piece_] := 
 MatrixPlot[
  ToExpression /@ 
   Characters /@ 
    StringSplit[StringReplace[piece, {"." -> "0", "X" -> "1"}], ","], 
  Mesh -> All, FrameStyle -> Opacity[0], 
  FrameTicksStyle -> Opacity[1], FrameTicks -> All]

Then evaluate:

Show[#, ImageSize -> Tiny] & /@ PiecePlot /@ ("pieces" /. data)

And I get:

enter image description here

But I find the grid size of 3*3 MatrixPlot is larger than 5*5 one's. I want something like:

         *****
   ***   *****
 { ***,  ***** }
   ***   *****
         *****

I mean, every gird with the same size. So how to do it?

$\endgroup$
  • $\begingroup$ Have you seen MatrixPlot documentation for options? $\endgroup$ – Kuba Jan 13 '16 at 13:42
  • $\begingroup$ @Kuba I read it carefully, but I couldn't find a option for controling that. $\endgroup$ – Sayakiss Jan 13 '16 at 13:44
  • $\begingroup$ Ok, so you want not a grid size but each grid item in every plot to be the same size? $\endgroup$ – Kuba Jan 13 '16 at 13:46
  • $\begingroup$ @Kuba Yes. if 3*3 matrix plot result a 30 pixels * 30 pixels image and I want 5*5 one result 50 pixels * 50 pixels image. $\endgroup$ – Sayakiss Jan 13 '16 at 13:49
  • $\begingroup$ Well, not really, you probably don't wont "ticks" aread to be scaled with the content. $\endgroup$ – Kuba Jan 13 '16 at 13:55
7
$\begingroup$

If you specify the size of the padding around the plot explicitly, calculating the right size for the plot is simple:

piecePlot[piece_] := Module[{mat, padding = 20, gridSize = 30},
  mat = StringCases[StringSplit[piece, ","], {"X" -> 1, "." -> 0}];
  MatrixPlot[mat, Mesh -> All, FrameStyle -> Opacity[0], 
   FrameTicksStyle -> Opacity[1], FrameTicks -> All,
   ImagePadding -> padding, 
   ImageSize -> (gridSize*Reverse[Dimensions[mat]] + 2*padding)]]

Where the only real change from your version are the last two options: ImagePadding -> padding, ImageSize -> (gridSize*Reverse[Dimensions[mat]] + 2*padding). I've simplified the string->matrix conversion a little.

piecePlot /@ ("pieces" /. data) // Multicolumn

enter image description here

$\endgroup$

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.