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$
1
  • 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$ Commented Jan 26, 2019 at 19:02

1 Answer 1

6
$\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$
3
  • $\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$ Commented Jan 27, 2019 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$ Commented Jan 27, 2019 at 13:23
  • $\begingroup$ @andr314 It seems that just cb[[1]] also does the job. What's the difference? $\endgroup$ Commented Jan 29, 2019 at 16:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.