6
$\begingroup$

I created a two grids like this

height = 125;
cb = MatrixPlot[
  Table[Mod[i + j, 2], {i, 1, 2}, {j, 1, 8}]
  , ColorFunction -> "BlueGreenYellow"
  , ImageSize -> {Automatic, 2*height}
  ]

enter image description here

p1 = MatrixPlot[
      Table[1, {i, 1, 1}, {j, 1, 2}]
      , Mesh -> True
      , ColorFunction -> ColorData["DarkRainbow"]
      , ImageSize -> {Automatic, height}
      ]

enter image description here

If I combine the two with Inset, I get

Graphics[{First@cb, Inset[p1, {0, 0}]}]

enter image description here

What should I do if I want to cover the top-left two squares of the yellow/green grid with the red grid?

(I know I could redraw the larger grid with the color of some squares changed. But I want to know how to do it with Inset.)


Update, after trying to play with the parameters of Inset, I have found some appropriate parameters that works. But this takes a lot trials.

Is there a way to keep images with their "absolute" size when using Inset?

Manipulate[ Graphics[{First@cb,    Inset[p1, {x, y}, {Left, Bottom}, {Automatic, w}]},   ImageSize -> ImageDimensions[cb]], {{x, -0.1}, -0.5,   0.5}, {{y, 0.8}, 0.0, 1.0}, {{w, 1.4}, 0.5, 2}]

enter image description here

$\endgroup$
  • 3
    $\begingroup$ Inset can take up to five arguments. The three you don't use are ones the give control over the size and placement of the inset in the main graphics panel. Read the full documentation for Inset to find out how to the additional arguments. $\endgroup$ – m_goldberg Jan 26 '19 at 19:02
5
$\begingroup$

Maybe not an answer

I think that Inset[] is not what you need.

This is what is happening when one resizes a graphic with some Inset[] without the fourth argument :

enter image description here

If you need a layout that doesn't change when resizing, you can use the 4 arguments form, but I find it a way easier not to use Inset[]at all :.

Like this :

height = 125;
cb = MatrixPlot[
  Table[Mod[i + j, 2], {i, 1, 2}, {j, 1, 8}]
  , ColorFunction -> "BlueGreenYellow"
  , ImageSize -> {Automatic, 2*height}
  ];  

p1 = MatrixPlot[
  Table[1, {i, 1, 1}, {j, 1, 2}]
  , Mesh -> True
  , ColorFunction -> ColorData["DarkRainbow"]
  , ImageSize -> {Automatic, height}
  ];

Graphics[{cb[[1, {1, 2}]], Translate[p1[[1, {1, 2}]], {0, 2}]}, Frame -> True]  

enter image description here

$\endgroup$
  • $\begingroup$ Can you explain a bit of the the last line of the code. I know what Translate does, but what is cb[[1,{1,2}]]? $\endgroup$ – ablmf Jan 27 '19 at 13:18
  • $\begingroup$ cb[[1,{1,2}]] is equivalent to cb[[1,1;;2]] ,to Take[cb[[1]],2] ... : it simply takes the first element at the first level, and then the 2 first elements at the second level. No subtelties. I confess I should have used a more common syntax. $\endgroup$ – andre314 Jan 27 '19 at 13:23
  • $\begingroup$ @andr314 It seems that just cb[[1]] also does the job. What's the difference? $\endgroup$ – ablmf Jan 29 '19 at 16:58

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.