1
$\begingroup$

I have a matrix of values (potentially quite big), which I have decoded into values of -1, 0 and 1 based on some rules. I want to use the MatrixPlot to see the pattern.

My issue is that the MatrixPlot automatically assigns the integer values corresponding to the position in the matrix in my case. However, instead of the (integer) positions, I want to use my own grid (xrow and ycolumn) of the same size on the axes. I have tried to use FrameTicks, but clearly it is a wrong command as it pushed my x and y values to the corner.

rules = {-1 -> Red, 0 -> White, 1 -> Green}; 
RegWhich = {{-1, 0, 1,1},{0, 0, 0,-1}, {0,0,1,1}};
xrow = Range[0.1, 0.12, 0.01]; 
ycolumn = Range[0.01, 0.04, 0.01];
MatrixPlot[RegWhich, ColorRules -> rules, Frame -> True, Mesh -> True,
      MeshStyle -> Black, 
     PlotLegends -> SwatchLegend[rules[[All, 2]], rules[[All, 1]]]]

Thanks in advance!

$\endgroup$
1
$\begingroup$

Add the option FrameTicks:

yticks = MapIndexed[{#2[[1]], #} &, xrow];
xticks = MapIndexed[{#2[[1]], #} &,  ycolumn];
MatrixPlot[RegWhich, ColorRules -> rules, 
 FrameTicks -> {{yticks, yticks}, {xticks, xticks}}, Mesh -> True, 
 MeshStyle -> Black, 
 PlotLegends -> SwatchLegend[rules[[All, 2]], rules[[All, 1]]]]

enter image description here

Update: If you want the first row at the bottom of the plot add the option DataReversed -> True to get

enter image description here

Alternatively, you can Reverse the input matrix rows and use DataRange:

MatrixPlot[Reverse@RegWhich, ColorRules -> rules, 
 DataRange -> {{.01, .04}, {.1, .12}},
 FrameTicks -> {{xrow, xrow}, {ycolumn, ycolumn}}, Mesh -> True, 
 MeshStyle -> Black, 
 PlotLegends -> SwatchLegend[rules[[All, 2]], rules[[All, 1]]]]

same picture

$\endgroup$
  • $\begingroup$ Thanks a lot. Another quick question, how can I have the reverse order of the rows (as in, with 0.1 on the bottom corner)? $\endgroup$ – Natalia K Feb 7 at 10:50
  • $\begingroup$ @NataliaK, please see the update. $\endgroup$ – kglr Feb 7 at 20:25

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.